brett@3riverdev.com 260.349.5732

Send email from a Java app with Heroku and SendGrid

Posted on March 19 , 2013

This evening, I sat down to integrate email notifications in a client’s JAX-RS web application.  Literally 5 minutes later, it was implemented, tested, and pushed.  Thanks to Heroku and its SendGrid add-on, it’s ridiculously easy.

In your POM, add the following dependencies (javamail):

    javax.activation
	activation
    1.1


    javax.mail
    mail
    1.4

Then simply use this static util class to send email:

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtil {

    private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
    private static final String SMTP_AUTH_USER = System.getenv("SENDGRID_USERNAME");
    private static final String SMTP_AUTH_PWD  = System.getenv("SENDGRID_PASSWORD");

    public static void send(String fromEmail, String toEmail, String subject, String htmlContent) throws Exception{
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.port", 587);
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getDefaultInstance(props, auth);
        // uncomment for debugging infos to stdout
        // mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);

        Multipart multipart = new MimeMultipart("alternative");

        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setContent(htmlContent, "text/html");
        multipart.addBodyPart(bodyPart);

        message.setContent(multipart);
        message.setFrom(new InternetAddress(fromEmail));
        message.setSubject(subject);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));

        transport.connect();
        transport.sendMessage(message,
        message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }

    private static class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
}

That’s it — easy!

Leave a Reply

Your email address will not be published. Required fields are marked *