Sending Mail to Gmail Account via SSL

package org.mail;

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;

public class ConfigureAndSendMail {

private Properties properties = System.getProperties();

private String mailTo = “from@gmail.com”; //Recipient’s email ID needs to be mentioned
private String mailFrom = “to@xyz.com”; //Sender’s email ID needs to be mentioned
private String hostName = “smtp.gmail.com”; //Host name of the gmail server
private String port = “465”; //Communication Port for SSL communication
private String subject=”Default Subject”; //Variable for storing the mail subject
private String message=”This is the default message”; //Variable for storing the message body
private String userName=null; //Variable for storing the user name
private String password=null; //Variable for storing the password
private Session session = null; //Variable for storing the session instance

/**
* Setter method for setting mailTo variable
* @param mailTo
* @return this
*/
public ConfigureAndSendMail setMailTo(String mailTo)
{
this.mailTo=mailTo;
return this;
}

/**
* Setter method for setting mailFrom variable
* @param mailFrom
* @return this
*/
public ConfigureAndSendMail setMailFrom(String mailFrom)
{
this.mailFrom=mailFrom;
return this;
}

/**
* Setter method for setting hostName variable
* @param hostName
* @return this
*/
public ConfigureAndSendMail setHost(String hostName)
{
this.hostName=hostName;
return this;
}

/**
* Setter method for setting port variable
* @param port
* @return this
*/
public ConfigureAndSendMail setPort(String port)
{
this.port=port;
return this;
}

/**
* Setter method for setting subject variable
* @param subject
* @return this
*/
public ConfigureAndSendMail setMailSubject(String subject)
{
this.subject=subject;
return this;
}

/**
* Setter method for setting message variable
* @param message
* @return this
*/
public ConfigureAndSendMail enterMessage(String message)
{
this.message=message;
return this;
}

/**
* Setter method for setting user name variable
* @param userName
* @return this
*/
public ConfigureAndSendMail setUserName(String userName)
{
this.userName=userName;
return this;
}

/**
* Setter method for setting password variable
* @param password
* @return this
*/
public ConfigureAndSendMail setPassword(String password)
{
this.password=password;
return this;
}

/**
* Method to set the following mail properties:
* 1.)mail.smtp.host
* 2.)mail.smtp.socketFactory.port
* 3.)mail.smtp.socketFactory.class
* 4.)mail.smtp.auth
* 5.)mail.smtp.port
* All the properties will be set to default if not
* explicitly set by the user using the setter methods
*/
private void setMailProperties()
{
properties.put(“mail.smtp.host”, hostName);
properties.put(“mail.smtp.socketFactory.port”, port);
properties.put(“mail.smtp.socketFactory.class”,
“javax.net.ssl.SSLSocketFactory”);
properties.put(“mail.smtp.auth”, “true”);
properties.put(“mail.smtp.port”, port);
}

/**
* Method to establish a session after authenticating the User
* Requires to atleast set the User Name and Password
* using the setUserName and setPassword method
* @return this
*/
public ConfigureAndSendMail establishSession()
{
final String userName=this.userName;
final String password=this.password;

setMailProperties();
session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,password);
}
});

return this;
}

/**
* Method to send the email with all the configured fields
* @return true/false
*/
public boolean sendEmail()
{
try
{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(mailFrom));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));

// Set Subject: header field
message.setSubject(this.subject);

// Now set the actual message
message.setText(this.message);

// Send message
Transport.send(message);

System.out.println(“Sent message successfully….”);

return true;
}
catch (MessagingException mex)
{
System.out.println(“Failed to send message….”);
mex.printStackTrace();
return false;
}
}
}

//Main Class invokign the ConfigureAndSendMail Class

package org.mail;

import javax.mail.MessagingException;

public class MainMailClass {

/**
* @param args
* @throws MessagingException
*/
public static void main(String[] args) throws MessagingException {

new ConfigureAndSendMail().setUserName(“account@gmail.com”)
.setPassword(“account-password”)
.setMailTo(“to@xyz.com”)
.establishSession()
.sendEmail();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s