Thursday, November 15, 2012

Send email in Java with gmail SMTP

Send email in Java with gmail SMTP example.

MailUtil.java
  1. package mailtest;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.util.Properties;  
  5. import java.util.logging.Level;  
  6. import java.util.logging.Logger;  
  7. import javax.mail.Authenticator;  
  8. import javax.mail.Message;  
  9. import javax.mail.MessagingException;  
  10. import javax.mail.PasswordAuthentication;  
  11. import javax.mail.Session;  
  12. import javax.mail.Transport;  
  13. import javax.mail.internet.InternetAddress;  
  14. import javax.mail.internet.MimeMessage;  
  15.   
  16. public class MailUtil {  
  17.   
  18.     private String SMTP_HOST = "smtp.gmail.com";  
  19.     private String FROM_ADDRESS = "yourname@gmail.com";  
  20.     private String PASSWORD = "yourpassword";  
  21.     private String FROM_NAME = "Sameera";  
  22.   
  23.     public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, String message) {  
  24.         try {  
  25.             Properties props = new Properties();  
  26.             props.put("mail.smtp.host", SMTP_HOST);  
  27.             props.put("mail.smtp.auth""true");  
  28.             props.put("mail.debug""false");  
  29.             props.put("mail.smtp.ssl.enable""true");  
  30.   
  31.             Session session = Session.getInstance(props, new SocialAuth());  
  32.             Message msg = new MimeMessage(session);  
  33.   
  34.             InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME);  
  35.             msg.setFrom(from);  
  36.   
  37.             InternetAddress[] toAddresses = new InternetAddress[recipients.length];  
  38.             for (int i = 0; i < recipients.length; i++) {  
  39.                 toAddresses[i] = new InternetAddress(recipients[i]);  
  40.             }  
  41.             msg.setRecipients(Message.RecipientType.TO, toAddresses);  
  42.   
  43.   
  44.             InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length];  
  45.             for (int j = 0; j < bccRecipients.length; j++) {  
  46.                 bccAddresses[j] = new InternetAddress(bccRecipients[j]);  
  47.             }  
  48.             msg.setRecipients(Message.RecipientType.BCC, bccAddresses);  
  49.   
  50.             msg.setSubject(subject);  
  51.             msg.setContent(message, "text/plain");  
  52.             Transport.send(msg);  
  53.             return true;  
  54.         } catch (UnsupportedEncodingException ex) {  
  55.             Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);  
  56.             return false;  
  57.   
  58.         } catch (MessagingException ex) {  
  59.             Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);  
  60.             return false;  
  61.         }  
  62.     }  
  63.   
  64.     class SocialAuth extends Authenticator {  
  65.   
  66.         @Override  
  67.         protected PasswordAuthentication getPasswordAuthentication() {  
  68.   
  69.             return new PasswordAuthentication(FROM_ADDRESS, PASSWORD);  
  70.   
  71.         }  
  72.     }  
  73. }  


Main.java

  1. package mailtest;  
  2.   
  3. public class Main {  
  4.   
  5.     public static void main(String[] args) {  
  6.         String[] recipients = new String[]{"youremail@yahoo.com"};  
  7.         String[] bccRecipients = new String[]{"youremail@gmail.com"};  
  8.         String subject = "Hi this is test Mail";  
  9.         String messageBody = "Test Mail from codesstore.blogspot.com";  
  10.   
  11.         new MailUtil().sendMail(recipients, bccRecipients, subject, messageBody);  
  12.   
  13.     }  
  14. }  
  15.    
Note: mail.jar  is required.

Can add multiple recipients and BCC recipients 
  1. String[] recipients = new String[]{"youremail@yahoo.com","youremail2@yahoo.com"};  

Instead of msg.setContent(message, "text/plain") you can use msg.setContent(message, "text/html") then you can add html tags to message body.

1 comment:

  1. This is an informative article. Thanks for this post. This is a beautiful and informative post. Keep it up dear. Black magic specialist in india - Love Samrat

    ReplyDelete