I like to record details that will put a software developer on fast track. Its also helps starters to learn programming. Here, I am starting with a simple program that could send an email programatically using your gmail account.
First, you need mail.jar so that you can make use of Java Mail API. If you are using J2EE, then by all means you would be having it. Otherwise, you can always obtain mail.jar here.
Second, you need to have the program as listed under. Please replace YourEmailID@gmail.com with your gmail id and gmailPassword with your correponding password. If you don't have a gmail account, then get it here.
Then, compile the program. If you don't have the Java installed already, install it. You can find instructions here. Use the following command to compile the program.
javac -classpath mail.jar;. GMail.java
You can place mail.jar any where you want, but you need to provide absolute path. Run it by..
java -classpath mail.jar;. GMail
You can download this program here.
Also, the program is listed here..
import java.util.*;
import javax.mail.Session;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.Address;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
/*
* A Simple program which sends a email from your gmail account.
* 1. Supply the properties to identify the mail server
* 2. Create an authenticated session with your maill id and password and with above properties
* 3. Create an email message with the above session, sender, email subject and email body
* 4. Send the message on Transport
*/
public class GMail {
public static void main(String args[]) {
try {
/*
* These properties are required to get connected to a mail server
*/
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.debug", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
/*
* Create an authenticated session which is required for mail server
* to grant access to the information that only accessible for the authenticated user
*
* You need to provide your gmail id and passworh here..
*/
Session session = getSession(props, "YourEmailID@gmail.com", "gmailPassword");
String mailTo="dara_ramesh@yahoo.com";
String mailFrom ="DeveloperDigest";
String mailSubject = "Simple GMail Test";
String mailBody="This email message is programatically generated as described in http://DeveloperDigest.blogspot.com blog.";
/*
* Send an email message.
*/
Message msg = getMessage(session, mailTo, mailFrom, mailSubject, mailBody);
if(msg != null) {
Transport.send(msg);
}
System.out.println("Message successfully sent.");
} catch (Exception ex) {
System.out.println(ex);
}
System.exit(0);
}
public static Message getMessage(Session session, String mailTo, String mailFrom, String mailSubject, String mailBody) {
Message msg = null;
try {
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(mailFrom, mailFrom));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo,false));
msg.setSubject(mailSubject);
msg.setText(mailBody);
msg.setSentDate(new Date());
} catch (Exception ex) {
ex.printStackTrace();
msg = null;
}
return msg;
}
public static Session getSession(Properties props, final String userName, final String password) {
System.out.println("Creating Session...\n");
Session session = Session.getDefaultInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("Authentication is being done");
return new PasswordAuthentication(userName, password);
}
});
session.setDebug(false);
System.out.println("Session Created..\n");
return session;
}
}
No comments:
Post a Comment