Java send smtp mail using Gmail

2 minute read

Today I wanted to make a mail notification mechanism for a project of mine. After some web searching I stumbled upon a 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=""></a>
 * <p>If you get {@link}: , try
 * ping 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() {

                    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

        // if it is html change multipart information
        if (isHtmlBody) {
            message.setContent(bodyText, CONTENT_CHARSET);
        } else {




    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("", "");
        proper.put("mail.smtp.socketFactory.port", "465");
        proper.put("mail.smtp.auth", "true");
        proper.put("mail.smtp.port", "465");

        // if you do not want to use authentication
        //proper.put("", "");
        //proper.put("mail.smtp.socketFactory.port", "25");
        //proper.put("mail.smtp.socketFactory.class", "");
        //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


Leave a Comment