Thursday, July 28, 2011

Sending Email in Java

It is not so complicated to create your own email sending application using Java by using the JavaMail API. To send an email, we need SMTP server and its credentials. Since I faced a lot of problems regarding the sending of an email and finally I got the working code. Yes, may be the modified source code may also work which may ultimately be simpler than this.

In this article, my focus will be on the problem solution, so I have used the SMTP server of google. Google is very good because it is providing a lot facilities to its users like if you have google account you have everything!

Now we start creating a sample application which is responsible for sending an email. To achieve this, we need the following properties to be set:
1) SMTP server
2) Server credentials
3) Port Number

We need to create Session object for which we need Properties object where we can set the properties we require and Authenticator object for authentication which needs the server credentials.

A sample program which is working for jdk 1.6 is presented here:

import javax.mail.PasswordAuthentication;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

* This class is responsible for sending emails.
* @author krishna
public class EmailSender {

//gmail SMTP server
private String SMTPServer = "";
//SMTP server username, in our case, it is the sender also
private String from = "abc";
//SMTP password
private String password = "xyz";

* Constructor
public EmailSender() {
* Sends a message email
* @param to To address
* @param subject Subject
* @param content message content
* @return result:true or false
* @throws MessagingException
public boolean SendMail(String to, String subject, String content)
throws MessagingException {

try {

//Create Session
Properties props = new Properties();
props.put("", this.SMTPServer);
//Port 465 is SSL port for gmail smtp server
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);

//Create Message
Message message = new MimeMessage(session);

//Set Addresses
Address fromAddress = new InternetAddress("this.from);
Address toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);

} catch (Exception ex) {
return true;

I think the above mentioned code works fine for you too. The only change you need is change the SMTP server credentials and google users can use their email id and password as credential. This user name will be from email address too. We can call the SendMail method of EmailSender object. The SendMail method takes the email address to whom we are going to send the message, subject text and content text.

This covers the basic of email sending which only consists of sending text messages only; but we can do a lot of extensions on this like attaching files, text rendering etc.

Thanks for reading this !

No comments:

Post a Comment