Saturday, 8 February 2014

JAVA Source code for Sending email with attachment


import com.sun.xml.internal.ws.util.ByteArrayDataSource;
import java.io.File;
import java.security.Security;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;


public class SendMailWithAttachments {

    public static String subject;
    public static String body;
    public static String recipients;
    public static String send = "";
    public static String passwd = "";

    public static synchronized void sendMail(String subject, String body, String recipients, String sender, String password, String fileAttachment)
            throws Exception {
        send = sender;
        passwd = password;
        System.out.println("Sender name>>>>>>>>"+send);
        System.out.println("Password>>>>>>>>>>>"+passwd);
        if (subject.equals("") || body.equals("") || recipients.equals("")) {
            System.out.println("Error in sending mail");
        } else {
            String mailhost = "smtp.gmail.com";
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.host", mailhost);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smwd);tp.quitwait", "false");

            Session session = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() {

                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(send, passwd);

                        }
                    });


            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));

            message.setSender(new InternetAddress(send));
            message.setSubject(subject);

            message.setDataHandler(handler);

            if (!fileAttachment.equals("")) {
                DataSource source = new FileDataSource(fileAttachment);
                message.setDataHandler(new DataHandler(source));
                message.setFileName(new File(fileAttachment).getName());
            }
            if (recipients.indexOf(',') > 0) {
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
            } else {
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
            }

            Transport.send(message);
     
        }
    }


    }
}

NB: for proper working of this code include mail.jar in library and make sure that the antivirus and firewall of your system is off..Otherwise an Exception named SSLHandShakeException will arise.

No comments:

Post a Comment