Showing posts with label Email with attachments. Show all posts
Showing posts with label Email with attachments. Show all posts

Wednesday, January 30, 2013

Sample JAVA Automatic Mail Sending Program

import java.util.Properties;
import java.util.Scanner;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;

import javax.mail.internet.*;
import javax.activation.*;

public class MainMail {
    public static void main(String[] args) throws FileNotFoundException {
        String mailContent = "", str = "";
        //String kbcsAddress = "yourmail@gmail.com";
        String ossdAddress = "yourmail1@gmail.com";
        String Error = "";
        // Reading paths from a file
        String brochure1 = "";
        String brochure2 = "";
        String regform = "";
        String bodytext = "";
        String emailids = "";
        String[] paths = new String[5];
        String path = "/home/reniguntla/workspace2/MailApplication/inputfiles/paths.txt";
        Scanner scfile = new Scanner(new File(path));
        int index=0;
        while (scfile.hasNext()) {
            paths[index++] = scfile.nextLine().trim();
               
        }
        brochure1 = paths[3];
        brochure2 = paths[4];
        regform = paths[2];
        bodytext = paths[1];
        emailids = paths[0];
               
        int i = 1;
        Properties props = new Properties();
       
         props.put("mail.smtp.host", "smtp.gmail.com");
         props.put("mail.smtp.socketFactory.class",
         "javax.net.ssl.SSLSocketFactory");
             
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(
                                "yourmail@gmail.com", "password123");
                    }
                });

        try {
            InternetAddress[] replyAddressList = {
                    //new InternetAddress(kbcsAddress),
                    new InternetAddress(ossdAddress) };
            try {
                FileReader rdr = new FileReader(bodytext);
                BufferedReader brdr = new BufferedReader(rdr);
                while ((str = brdr.readLine()) != null)
                    mailContent += str + "\n";
            } catch (Exception e) {
                e.printStackTrace();
            }

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("predictanumaan@gmail.com"));
            message.setSubject("Anumaan-standalone-0.2 release From CDAC Mumbai");
            message.setText(mailContent);
            message.setReplyTo(replyAddressList);

            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(mailContent, "text/html");
            mbp1.setContent(mailContent, "text/html");

            //MimeBodyPart mbp2 = new MimeBodyPart();
            //FileDataSource fds = new FileDataSource(brochure1);
            //mbp2.setDataHandler(new DataHandler(fds));
            //mbp2.setFileName(fds.getName());

            //MimeBodyPart mbp3 = new MimeBodyPart();
            //FileDataSource fds1 = new FileDataSource(brochure2);
            //mbp3.setDataHandler(new DataHandler(fds1));
            //mbp3.setFileName(fds1.getName());

            //MimeBodyPart mbp4 = new MimeBodyPart();
            //FileDataSource fds2 = new FileDataSource(regform);
            //mbp4.setDataHandler(new DataHandler(fds2));
            //mbp4.setFileName(fds2.getName());

            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            //mp.addBodyPart(mbp2);
            //mp.addBodyPart(mbp3);
            //mp.addBodyPart(mbp4);
            message.setContent(mp);

            try {
                FileReader rdr = new FileReader(emailids);
                BufferedReader brdr = new BufferedReader(rdr);
                while ((str = brdr.readLine()) != null) {
                    str = str.trim();
                    try {
                        message.setRecipients(Message.RecipientType.TO,
                                InternetAddress.parse(str));
                        Transport.send(message);
                    } catch (Exception e) {
                        Error += str + "\n";
                        e.printStackTrace();
                    }
                    System.out.println(i + ".Sent To:: " + str);
                    i++;
                }
                System.out.println("Not Sent To:----------\n\n" + Error);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}