Java send smtp mail using Gmail
Today I wanted to make a mail notification mechanism for a project of mine. After some web searching I stumbled upon a mkyong.com tutorial where he uses SMTP with Java Mail API to send emails via Gmail. I modified the code in some places to make it more general and API friendly. Now I can send emails easily using different authentication and encryption methods (TLS,SSL)! I hope you find it useful!
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;
/**
*
* Simple SMTP mail class built using the example from: <a
* href="http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example">mkyong.com</a>
*
* <p>If you get {@link java.net.UnknownHostException}: smtp.gmail.com , try
* ping smtp.gmail.com and make sure you got a response (able to access). Often
* times, your connection may block by your firewall or proxy behind.</p>
*/
public class MailSender {
private String userName;
private String password;
private Properties props;
private final static String CONTENT_CHARSET = "text/html; charset=utf-8";
public MailSender(Properties props) {
this.userName = (String) props.get("userName");
this.password = (String) props.get("password");
this.props = props;
}
public void sendMail(String subject, String bodyText) throws MessagingException {
sendMail(subject, bodyText, false);
}
public void sendMail(String subject, String bodyText, boolean isHtmlBody) throws MessagingException {
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message message = new MimeMessage(session);
// set from (sender) address
message.setFrom(new InternetAddress((String) props.get("fromAddress")));
// set recipients addresses (may be more than one)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse((String) props.get("recipientsAddresses")));
// set message subject
message.setSubject(subject);
// if it is html change multipart information
if (isHtmlBody) {
message.setContent(bodyText, CONTENT_CHARSET);
} else {
message.setText(bodyText);
}
Transport.send(message);
System.out.println("Done");
}
public static void main(String[] args) throws MessagingException {
Properties proper = new Properties();
// put credentials
proper.put("userName", "user");
proper.put("password", "password");
// put server information
proper.put("mail.smtp.host", "smtp.gmail.com");
proper.put("mail.smtp.socketFactory.port", "465");
proper.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
proper.put("mail.smtp.auth", "true");
proper.put("mail.smtp.port", "465");
// if you do not want to use authentication
//proper.put("mail.smtp.host", "192.168.1.13");
//proper.put("mail.smtp.socketFactory.port", "25");
//proper.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//proper.put("mail.smtp.connectiontimeout",30000);
//proper.put("mail.smtp.auth", "false");
//proper.put("mail.smtp.port", "25");
// put sender information
proper.put("fromAddress", "[email protected]");
// put recipient information
proper.put("recipientsAddresses", "[email protected]");
new MailSender(proper).sendMail("Bob", "Some text body for bob");
}
}
And if you are using maven you can add the dependency to Java Mail API with the following code to your pom.xml. For me, the latest version is 1.4.6
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.6</version>
</dependency>
Comments