Wednesday, October 24, 2012

Java Mail

For Windows users only
Pre-Requisites:
  • jdk1.6.0_11 ( works with previous versions of JDK)

 Installed location:
  • jdk1.6.0_11 ->   C:\Program Files\Java\jdk1.6.0_11\
  • mail.jar ->   D:\apache-tomcat-6.0.18\lib\mail-1.3.3.jar
Environment Variables:
Enter Variable name and Variable value which is mentioned below.
Variable name:  JAVA_HOME
Variable value:  C:\Program Files\Java\jdk1.6.0_11

Variable name:  PATH
Variable value:  C:\Program Files\Java\jdk1.6.0_11\bin

Variable name:  CLASSPATH
Variable value:  .;D:\apache-tomcat-6.0.18\lib\mail-1.3.3.jar

Note
There first character in classpath should be a period (.) 

Download Links:
  1. Click here to download mail-1.3.3.jar
  2. javamail.default.address.map
  3. javamail.address.map
  4. javamail.providers
  5. javamail.default.providers
Steps to run the below example program
Before running the below example program make sure that mail.jar is in the classpath. Using the below program you can send mail only from Gmail, but the receiving mail ID can be anything. If you want to send mail from some other mail providers, then change theSMTP_HOST_NAME and SMTP_PORT accordingly to mail providers. Change theemailMsgTxt, emailSubjectTxt, emailFromAddress according to your need.
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(" yourMaidID", "yourPassword");
}
Replace "yourMaidID" with from address (i.e. the mail ID from where email should go) and replace " yourPassword" with the password of the corresponding mail ID.
While running the below program you may encounter some errors in console which is given below. This is just a warning. By default our program will look for javamail.providers, javamail.address.map, javamail.default.address.map, javamail.default.providers in "C:\Program Files\Java\jre6\lib\" folder. So to avoid this warnings you just create four files inside "C:\Program Files\Java\jre6\lib\" with the below names. Make sure that the extension of the file is not in .txt format.
(little workaround to make sure the extension is not of type .txt - Click My Computer ->Tools -> Folder Options -> View -> Advanced settings ->
Uncheck the check box "Hide extensions for known file types". Now navigate to "C:\Program Files\Java\jre6\lib\" and check for the file extensions.)
Warnings
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre6\lib\javamail.providers (The system cannot find the file specified)
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre6\lib\javamail.address.map (The system cannot find the file specified)

Simple java program to send mail
  1. /** 
  2.  * SendMailThroughJava.java 
  3.  */  
  4. package com.javaworkspace.mail;  
  5.   
  6. import java.security.Security;  
  7. import java.util.Properties;  
  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. /** 
  17.  * @author www.javaworkspace.com 
  18.  */  
  19. public class SendMailThroughJava {  
  20.   
  21.     private static final String SMTP_HOST_NAME = "smtp.gmail.com";  
  22.     private static final String SMTP_PORT = "465";  
  23.     private static final String emailMsgTxt = "Welcome to www.javaworkspace.com";  
  24.     private static final String emailSubjectTxt = "A test mail from www.javaworkspace.com";  
  25.     private static final String emailFromAddress = "javaworkspace@gmail.com";  
  26.     private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";  
  27.     private static final String[] sendTo = { "javaworkspace@hotmail.com" };  
  28.   
  29.     public static void main(String args[]) throws Exception {  
  30.   
  31.         Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());  
  32.         SendMailThroughJava sendMailThroughJava = new SendMailThroughJava();  
  33.         sendMailThroughJava.sendSSLMessage(sendTo, emailSubjectTxt,  
  34.                 emailMsgTxt, emailFromAddress);  
  35.         System.out.println("Sucessfully sent mail to all Users");  
  36.     }  
  37.   
  38.     /** 
  39.      * @param recipients 
  40.      * @param subject 
  41.      * @param message 
  42.      * @param from 
  43.      * @throws MessagingException 
  44.      */  
  45.     public void sendSSLMessage(String recipients[], String subject,  
  46.             String message, String from) throws MessagingException {  
  47.         boolean debug = true;  
  48.   
  49.         Properties props = new Properties();  
  50.         props.put("mail.smtp.host", SMTP_HOST_NAME);  
  51.         props.put("mail.smtp.auth""true");  
  52.         props.put("mail.debug""true");  
  53.         props.put("mail.smtp.port", SMTP_PORT);  
  54.         props.put("mail.smtp.socketFactory.port", SMTP_PORT);  
  55.         props.put("mail.smtp.socketFactory.class", SSL_FACTORY);  
  56.         props.put("mail.smtp.socketFactory.fallback""false");  
  57.   
  58.         Session session = Session.getDefaultInstance(props,  
  59.                 new javax.mail.Authenticator() {  
  60.                     protected PasswordAuthentication getPasswordAuthentication() {  
  61.                         return new PasswordAuthentication("yourMaidID",  
  62.                                 "yourPassword");  
  63.                     }  
  64.                 });  
  65.   
  66.         session.setDebug(debug);  
  67.   
  68.         Message msg = new MimeMessage(session);  
  69.         InternetAddress addressFrom = new InternetAddress(from);  
  70.         msg.setFrom(addressFrom);  
  71.   
  72.         InternetAddress[] addressTo = new InternetAddress[recipients.length];  
  73.         for (int i = 0; i < recipients.length; i++) {  
  74.             addressTo[i] = new InternetAddress(recipients[i]);  
  75.         }  
  76.         msg.setRecipients(Message.RecipientType.TO, addressTo);  
  77.   
  78.         // Setting the Subject and Content Type  
  79.         msg.setSubject(subject);  
  80.         msg.setContent(message, "text/plain");  
  81.         Transport.send(msg);  
  82.     }  
  83. }  

No comments:

Post a Comment