Sunday, 12 August 2012

JavaMail API – Sending email via Gmail SMTP

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Multipart;

public class SendMailSSL {
    public static void main(String[] args) {
            
        Properties props = new Properties();
                props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.user", "abc@gmail.com");
                props.put("mail.smtp.password", "password");

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
                
                String[] to = {"xxxx@yahoo.com"}; // added this line

                
        Session session = Session.getDefaultInstance(props,null);
                MimeMessage message = new MimeMessage(session);
                try{
                message.setFrom(new InternetAddress("abc@gmail.com"));
                }catch(Exception e){}
                InternetAddress[] toAddress = new InternetAddress[to.length];
                
                for( int i=0; i < to.length; i++ ) { // changed from a while loop
                    try{
                toAddress[i] = new InternetAddress(to[i]);
                    }catch(Exception e){}
                }System.out.println(Message.RecipientType.TO);
                for( int i=0; i < toAddress.length; i++) { // changed from a while loop
                   try{
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
                }catch(Exception e){}
                }
               try{
    message.setSubject("sending from gmail");
    message.setText("Hello Its me");
    
     String bodyText = "This is a important message with attachment";
     String filename = "D:/new.txt";
     MimeBodyPart messagePart = new MimeBodyPart();
            messagePart.setText(bodyText);

            //
            // Set the email attachment file
            //
            MimeBodyPart attachmentPart = new MimeBodyPart();
            FileDataSource fileDataSource = new FileDataSource(filename) {
              
               public String getContentType() {
                    return "application/octet-stream";
               }
           };
          attachmentPart.setDataHandler(new DataHandler(fileDataSource));
           attachmentPart.setFileName(filename);

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messagePart);
            multipart.addBodyPart(attachmentPart);

            message.setContent(multipart);
    
    
    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.gmail.com", "abc@gmail.com", "password");
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    
                }catch(Exception e){}
        System.out.println("Done");
    }
}

No comments:

Post a Comment