How to use Gmail as your SMTP server – JAVA

Hi Here is an interesting code to send an email using gmail smtp server.

This asks for the login and password of any gmail accound and uses gmail smtp to send emails…


package com.sunil.mail;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class SimpleJavaMail {

private Session mailSession;
protected PasswordAuthentication authentication;
private String user = null;
private String password = null;
private Properties props;

public SimpleJavaMail(){
props = new Properties();
props.setProperty(“mail.transport.protocol”, “smtp”);
props.setProperty(“”, “”);
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.port”, “465”);
props.put(“mail.smtp.socketFactory.port”, “465”);
props.put(“mail.smtp.socketFactory.class”, “”);
props.put(“mail.smtp.socketFactory.fallback”, “false”);
props.put(“mail.user”, “”);
props.put(“mail.password”, “xxxxx”);
props.setProperty(“mail.smtp.quitwait”, “false”);

public void initMail(String user, String password) {
this.user = user;
this.password = password;
mailSession = createMailSession();

private Session createMailSession() {
Session session = null;
try {
session = Session.getDefaultInstance(props,    new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
} catch (Exception e) {
return session;

public synchronized void sendMail(String subject, String body, String sender, String recipients)
throws Exception{
String host;
String mailUserName = (String) getProps().get(“mail.user”);
String mailPassword = (String) getProps().get(“mail.password”);
host = (String) getProps().get(“”);
host = host.toLowerCase();
initMail(mailUserName, mailPassword);
MimeMessage message = new MimeMessage(mailSession);
message.setSender(new InternetAddress(sender));
message.setContent(body, “text/plain”);
if (recipients.indexOf(‘,’) > 0){
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

public Properties getProps() {
return props;

public void setProps(Properties props) {
this.props = props;

public static void main(String[] args) {
String subject = “Test mail…”;
String body = “Hi Buddy…”;
String sender = “”;
String recipients = “”;
SimpleJavaMail mailJava = new SimpleJavaMail();
mailJava.sendMail(subject, body, sender, recipients);
} catch (Exception e) {
System.out.println(“Mail sent!”);


  1. No trackbacks yet.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: