How to send emails using JavaMail

  • 08 February 2017
  • ADM


How to send emails using JavaMail - images/logos/java.jpg


This article will present a simple example how to send emails using JavaMail.


To get the JavaMail, download the latest version and attach it to the project.

For Maven users you only need to add it as a dependency:



package com.admfactory.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
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;

public class JavaMailSender {
    private String host;
    private String port;

    private boolean debug;

    private Properties props;

    public JavaMailSender(String host, String port) { = host;
	this.port = port;

	/** Get a Properties object */
	props = System.getProperties();

    public Properties addProxy(String host, String port) {
	props.setProperty("proxySet", "true");
	props.setProperty("socksProxyHost", host);
	props.setProperty("socksProxyPort", port);
	return props;

    public boolean send(String from, String password, String to, String cc, String title, String text) {
	boolean result = false;
	try {

	    props.setProperty("mail.smtp.port", this.port);
	    props.put("mail.smtp.auth", "true");
	    props.put("mail.debug", "true");
	    props.put("", "pop3");
	    props.put("mail.transport.protocol", "smtp");
	    props.put("mail.smtp.starttls.enable", "true");

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


	    MimeMessage msg = new MimeMessage(session);
	    msg.setFrom(new InternetAddress(from));

	    InternetAddress[] addressTO = { new InternetAddress(to) };
	    msg.setRecipients(Message.RecipientType.TO, addressTO);

	    InternetAddress[] addressCC = { new InternetAddress(cc) };
	    msg.setRecipients(Message.RecipientType.CC, addressCC);

	    InternetAddress addressFROM = new InternetAddress(from);

	    msg.setSentDate(new Date());


	    result = true;
	} catch (Exception ex) {
	    result = false;

	return result;

    public boolean isDebug() {
	return debug;

    public void setDebug(boolean debug) {
	this.debug = debug;

    public static void main(String[] args) {

	System.out.println("Send emails example using JavaMail");
	String host = "";
	String port = "587";

	JavaMailSender sender = new JavaMailSender(host, port);

	/** Activate this line for proxy authentication and change the settings with your details */
	// sender.addProxy("", "8080");

	/** Activate this line if you need to see more details */
	//	 sender.setDebug(true);

	String from = "";
	String password = "password";

	String to = "";
	String cc = "";

	String subject = "test";
	String msg = "Message sent using JavaMail.";

	boolean b = sender.send(from, password, to, cc, subject, msg);
	if (b) {
	    System.out.println("Message sent successfully.");
	} else {
	    System.out.println("Message failed.");

If the application will be behind a proxy server activate the proxy settings and provide the right details:

sender.addProxy("", "8080");


If you go with current setup the output should be similar with:

Send emails example using JavaMail

DEBUG: JavaMail version 1.4.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/
Message sent successfully.

If you activate the debug, by uncommenting the line, sender.setDebug(true); you will see a bigger output with a lot of useful information.