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.security.Security;
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(){
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props = new Properties();
props.setProperty(“mail.transport.protocol”, “smtp”);
props.setProperty(“mail.host”, “smtp.gmail.com”);
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”, “javax.net.ssl.SSLSocketFactory”);
props.put(“mail.smtp.socketFactory.fallback”, “false”);
props.put(“mail.user”, “xxxxxxx@gmail.com”);
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) {
e.printStackTrace();
}
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(“mail.host”);
host = host.toLowerCase();
initMail(mailUserName, mailPassword);
MimeMessage message = new MimeMessage(mailSession);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setContent(body, “text/plain”);
if (recipients.indexOf(‘,’) > 0){
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
Transport.send(message);
}

public Properties getProps() {
return props;
}

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

public static void main(String[] args) {
try{
String subject = “Test mail…”;
String body = “Hi Buddy…”;
String sender = “noname@noname.com”;
String recipients = “sunilxxxx@gmail.com”;
SimpleJavaMail mailJava = new SimpleJavaMail();
mailJava.sendMail(subject, body, sender, recipients);
} catch (Exception e) {
Logger.getLogger(SimpleJavaMail.class.getName()).error(e);
e.printStackTrace();
}
System.out.println(“Mail sent!”);
}

}

Advertisements
  1. No trackbacks yet.

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

%d bloggers like this: