Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.tectonica.gae;
  
  import java.util.Date;
  import java.util.List;
  
Utility class for sending emails using GAE JavaMail implementation (which is different - in several aspects - from Oracle's) without the hassle of manually assembling Multipart data. Implemented using the Builder pattern for easy invocation.

Examples:

 // 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();
 

Author(s):
Zach Melamed
 
 public class GaeEmailSender
 {
this email-address must be listed in the GAE application Permissions area
 
 //	private static final String DEFAULT_FROM = "Example <" + System.getProperties().get("com.google.appengine.application.id")
 //			+ "@appspot.gserviceaccount.com>";
 	private static String defaultFrom = null// "Example <jack@example.com>";
 
 	private static String defaultReplyTo = null// "noreply@example.com";
 
 	public static void setDefaultFrom(String defaultFrom)
 	{
 		. = defaultFrom;
 	}
 
 	public static void setDefaultReplyTo(String defaultReplyTo)
 	{
 		. = defaultReplyTo;
 	}
 
 	private static class Attachment
 	{
 		byte[] data;
 
 		Attachment(String filenameString mimeTypebyte[] data)
 		{
 			this. = filename;
 			this. = mimeType;
 			this. = data;
 		}
 	}
 
 	private MimeMessage msg = new MimeMessage(getGaeSession());;
 	private String mText;
 	private String mHtml;
 	private List<AttachmentmAttachments = new ArrayList<>();
 
 	public static GaeEmailSender subject(String subjectthrows MessagingException
 	{
 		GaeEmailSender sender = new GaeEmailSender();
 		sender.msg.setSubject(subject"UTF-8");
 	}
 
 	{
 		.setFrom(new InternetAddress(from));
 		return this;
 	}
 
 	{
 		.setReplyTo(InternetAddress.parse(replyTo.replaceAll(";"",")));
 		return this;
 	}
 
	{
		.addRecipients(.., InternetAddress.parse(to.replaceAll(";"",")));
		return this;
	}
	{
		.addRecipients(.., InternetAddress.parse(cc.replaceAll(";"",")));
		return this;
	}
	{
		.addRecipients(.., InternetAddress.parse(bcc.replaceAll(";"",")));
		return this;
	}
	{
		 = text;
		return this;
	}
	{
		 = html;
		return this;
	}
	public GaeEmailSender attach(String filenameString mimeTypebyte[] datathrows MessagingException
	{
		.add(new Attachment(filenamemimeTypedata));
		return this;
	}
	public void send() throws MessagingException
	{
		if ( == null &&  == 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 ( != null)
		for (Attachment attachment : )
		{
			BodyPart attachPart = new MimeBodyPart();
			attachPart.setFileName(attachment.filename);
			attachPart.setDataHandler(new DataHandler(new ByteArrayDataSource(attachment.dataattachment.mimeType)));
			attachPart.setDisposition("attachment");
			content.addBodyPart(attachPart);
		}
		.setContent(content);
		Transport.send();
	}
	{
		MimeBodyPart text = new MimeBodyPart();
		text.setText( == null ? " " : );
		return text;
	}
	{
		MimeBodyPart html = new MimeBodyPart();
		html.setContent("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(propsnull);
	}
New to GrepCode? Check out our FAQ X