Skip to content

Instantly share code, notes, and snippets.

@phatduckk
Created July 29, 2011 06:42
Show Gist options
  • Save phatduckk/1113329 to your computer and use it in GitHub Desktop.
Save phatduckk/1113329 to your computer and use it in GitHub Desktop.
import org.apache.commons.lang.StringUtils;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class SendGridExample {
private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
private static final String SMTP_AUTH_USER = "USERNAME-NOT-EMAIL";
private static final String SMTP_AUTH_PWD = "***PASSWORD***";
private static final String TEXT_HTML = "text/html";
private static String PORT = "465";
private static String STARTTLS = "true";
private static String AUTH = "true";
private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
private boolean debug = false;
public static void main(String[] args) throws EmailException {
final SendGridExample example = new SendGridExample();
HashMap<String, String> map = new HashMap<String, String>();
map.put("X-MyHeader", "blabla");
example.send("[email protected]", "[email protected]", "subject", "text", "<b>html</b>", map);
}
public SendGridExample() {
}
public SendGridExample(boolean debug) {
this.debug = debug;
}
public void send(String to, String subject, String text) throws EmailException {
send(to, null, subject, text, null, new HashMap<String, String>());
}
public void send(
String to, String subject, String text, String html) throws EmailException {
send(to, null, subject, text, html, new HashMap<String, String>());
}
public void send(
String to, String from, String subject, String text, String html,
HashMap<String, String> headers) throws EmailException {
Session mailSession = getMailSession();
MimeMessage message = new MimeMessage(mailSession);
try {
Multipart multipart = new MimeMultipart("alternative");
if (StringUtils.isNotBlank(text)) {
BodyPart textPart = new MimeBodyPart();
textPart.setText(text + "\n");
multipart.addBodyPart(textPart);
}
if (StringUtils.isNotBlank(html)) {
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, TEXT_HTML);
multipart.addBodyPart(htmlPart);
}
final InternetAddress fromAddress = from != null
? new InternetAddress(from)
: new InternetAddress("[email protected]");
if (!headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
message.addHeader(entry.getKey(), entry.getValue());
}
}
message.setSubject(subject, "utf8");
message.setFrom(fromAddress);
message.addRecipient(Message.RecipientType.TO, getToAddress(to));
message.setContent(multipart);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
Transport transport = mailSession.getTransport("smtp");
transport.connect(SMTP_HOST_NAME, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (MessagingException e) {
throw new EmailException("Couldn't send message to: " + to, e);
}
}
private Session getMailSession() {
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.user", SMTP_AUTH_USER);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props, null);
if (debug) {
session.setDebug(true);
}
return session;
}
private InternetAddress getToAddress(String to) throws AddressException {
return (debug)
? new InternetAddress("[email protected]")
: new InternetAddress(to);
}
private class EmailException extends Exception {
public EmailException(String s, Throwable throwable) {
super(s, throwable);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment