For Windows users only
Pre-Requisites:
|
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:
- Click here to download mail-1.3.3.jar
- javamail.default.address.map
- javamail.address.map
- javamail.providers
- 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");
}
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.)
(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)
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
- /**
- * SendMailThroughJava.java
- */
- package com.javaworkspace.mail;
- import java.security.Security;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- /**
- * @author www.javaworkspace.com
- */
- public class SendMailThroughJava {
- private static final String SMTP_HOST_NAME = "smtp.gmail.com";
- private static final String SMTP_PORT = "465";
- private static final String emailMsgTxt = "Welcome to www.javaworkspace.com";
- private static final String emailSubjectTxt = "A test mail from www.javaworkspace.com";
- private static final String emailFromAddress = "javaworkspace@gmail.com";
- private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
- private static final String[] sendTo = { "javaworkspace@hotmail.com" };
- public static void main(String args[]) throws Exception {
- Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
- SendMailThroughJava sendMailThroughJava = new SendMailThroughJava();
- sendMailThroughJava.sendSSLMessage(sendTo, emailSubjectTxt,
- emailMsgTxt, emailFromAddress);
- System.out.println("Sucessfully sent mail to all Users");
- }
- /**
- * @param recipients
- * @param subject
- * @param message
- * @param from
- * @throws MessagingException
- */
- public void sendSSLMessage(String recipients[], String subject,
- String message, String from) throws MessagingException {
- boolean debug = true;
- Properties props = new Properties();
- props.put("mail.smtp.host", SMTP_HOST_NAME);
- props.put("mail.smtp.auth", "true");
- props.put("mail.debug", "true");
- props.put("mail.smtp.port", SMTP_PORT);
- props.put("mail.smtp.socketFactory.port", SMTP_PORT);
- props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
- props.put("mail.smtp.socketFactory.fallback", "false");
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("yourMaidID",
- "yourPassword");
- }
- });
- session.setDebug(debug);
- Message msg = new MimeMessage(session);
- InternetAddress addressFrom = new InternetAddress(from);
- msg.setFrom(addressFrom);
- InternetAddress[] addressTo = new InternetAddress[recipients.length];
- for (int i = 0; i < recipients.length; i++) {
- addressTo[i] = new InternetAddress(recipients[i]);
- }
- msg.setRecipients(Message.RecipientType.TO, addressTo);
- // Setting the Subject and Content Type
- msg.setSubject(subject);
- msg.setContent(message, "text/plain");
- Transport.send(msg);
- }
- }
No comments:
Post a Comment