Skip to content

Instantly share code, notes, and snippets.

@zach-m
Last active August 29, 2015 14:06
Show Gist options
  • Save zach-m/d3547b589d49bdc7893e to your computer and use it in GitHub Desktop.
Save zach-m/d3547b589d49bdc7893e to your computer and use it in GitHub Desktop.
Wrapper for sending emails using GAE JavaMail implementation (which is different - in several aspects - from Oracle's)
package com.tectonica.util;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
public class GaeEmailSender
{
/**
* the FROM email-address must be listed in the GAE application Permissions area
*/
private static final String DEFAULT_FROM = "Johnny <john@example.com>";
// private static final String DEFAULT_FROM = "Johnny <" + System.getProperties().get("com.google.appengine.application.id")
// + "@appspot.gserviceaccount.com>";
private static final String DEFAULT_REPLY_TO = "noreply@example.com";
private MimeMessage msg = new MimeMessage(getGaeSession());;
private String mText;
private String mHtml;
private List<Attachment> mAttachments = new ArrayList<>();
private static class Attachment
{
String filename;
String mimeType;
byte[] data;
Attachment(String filename, String mimeType, byte[] data)
{
this.filename = filename;
this.mimeType = mimeType;
this.data = data;
}
}
public static GaeEmailSender subject(String subject) throws MessagingException
{
GaeEmailSender sender = new GaeEmailSender();
sender.msg.setSubject(subject, "UTF-8");
return sender.from(DEFAULT_FROM).replyTo(DEFAULT_REPLY_TO);
}
public GaeEmailSender from(String from) throws AddressException, MessagingException
{
msg.setFrom(new InternetAddress(from));
return this;
}
public GaeEmailSender replyTo(String replyTo) throws AddressException, MessagingException
{
msg.setReplyTo(InternetAddress.parse(replyTo.replaceAll(";", ",")));
return this;
}
public GaeEmailSender to(String to) throws AddressException, MessagingException
{
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to.replaceAll(";", ",")));
return this;
}
public GaeEmailSender cc(String cc) throws AddressException, MessagingException
{
msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(cc.replaceAll(";", ",")));
return this;
}
public GaeEmailSender bcc(String bcc) throws AddressException, MessagingException
{
msg.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc.replaceAll(";", ",")));
return this;
}
public GaeEmailSender text(String text) throws MessagingException
{
mText = text;
return this;
}
public GaeEmailSender html(String html) throws MessagingException
{
mHtml = html;
return this;
}
public GaeEmailSender attach(String filename, String mimeType, byte[] data) throws MessagingException
{
mAttachments.add(new Attachment(filename, mimeType, data));
return this;
}
public void send() throws MessagingException
{
if (mText == null && mHtml == null)
throw new NullPointerException("At least one context has to be provided: Text or Html");
MimeMultipart content = new MimeMultipart();
content.addBodyPart(textPart()); // plain text is always looked for by GAE's JavaMail
if (mHtml != null)
content.addBodyPart(htmlPart());
for (Attachment attachment : mAttachments)
{
BodyPart attachPart = new MimeBodyPart();
attachPart.setFileName(attachment.filename);
attachPart.setDataHandler(new DataHandler(new ByteArrayDataSource(attachment.data, attachment.mimeType)));
attachPart.setDisposition("attachment");
content.addBodyPart(attachPart);
}
msg.setContent(content);
msg.setSentDate(new Date());
Transport.send(msg);
}
private MimeBodyPart textPart() throws MessagingException
{
MimeBodyPart text = new MimeBodyPart();
text.setText(mText == null ? " " : mText);
return text;
}
private MimeBodyPart htmlPart() throws MessagingException
{
MimeBodyPart html = new MimeBodyPart();
html.setContent(mHtml, "text/html; charset=utf-8");
return html;
}
/**
* returns an "empty" session, filled automatically by GAE
*/
private static Session getGaeSession()
{
Properties props = new Properties();
return Session.getDefaultInstance(props, null);
}
}
// send an email with TEXT-only body
GaeEmailSender.subject("Hello").to("a@y.com").cc("b@y.com").text("body as text").send();
// send an email with HTML + TEXT version of the body
GaeEmailSender.subject("Hello").to("x@y.com").text("body as text").html("<html><body>body as html</body></html>").send();
// send an email with HTML body + two attachments
GaeEmailSender.subject("..").to("..").html("..").attach("my.pdf", "application/pdf", pdfBytes).attach("me.png", "image/png", pngBytes).send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment