Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2014 Zach Melamed
   * 
   * Latest version available online at https://github.com/zach-m/tectonica-commons
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.tectonica.util;
 
 import java.util.Date;
 import java.util.List;
 
Utility class to use JavaMail without the hassle of manually assembling Multipart data. Implemented using the Builder pattern for easy invocation.

Examples:

 // send an email with TEXT-only body
 EmailSender.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
 EmailSender.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
 EmailSender.subject("..").to("..").html("..").attach("my.pdf", "application/pdf", pdfBytes).attach("me.png", "image/png", pngBytes).send();
 

Author(s):
Zach Melamed
 
 public class EmailSender
 {
 	private static final String USERNAME = "example@gmail.com"// change accordingly
 	private static final String PASSWORD = "********"// change accordingly
 	private static final String DEFAULT_FROM = "Zach <example@gmail.com>";
 	private static final String DEFAULT_REPLY_TO = "noreply@gmail.com";

fill this in with your own SMTP parameters
 
 	private static Session getSession()
 	{
 		Properties props = new Properties();
 		props.put("mail.smtp.auth""true");
 		props.put("mail.smtp.starttls.enable""true");
 		props.put("mail.smtp.host""smtp.gmail.com");
 		props.put("mail.smtp.port""587");
 
 		Session session = Session.getInstance(propsnew Authenticator()
 		{
 			{
 			}
 		});
 
 		return session;
 	}
 
 	private MimeMessage msg = new MimeMessage(getSession());;
 	private String mText;
 	private String mHtml;
 	private List<AttachmentmAttachments = new ArrayList<>();
 
 	// TODO: support embedded attachments (using "related" multipart)
 	// TODO: allow user to pass InputStream instead of byte array
 	private static class Attachment
 	{
		byte[] data;
		Attachment(String filenameString mimeTypebyte[] data)
		{
			this. = filename;
			this. = mimeType;
			this. = data;
		}
	}
	public static EmailSender subject(String subjectthrows MessagingException
	{
		EmailSender sender = new EmailSender();
		sender.msg.setSubject(subject"UTF-8");
	}
	{
		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;
	}
	public EmailSender text(String textthrows MessagingException
	{
		 = text;
		return this;
	}
	public EmailSender html(String htmlthrows MessagingException
	{
		 = html;
		return this;
	}
	public EmailSender 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");
		boolean usingAlternative = false;
		boolean hasAttachments = .size() > 0;
		if ( != null &&  == null)
		{
			// TEXT ONLY
			cover = new MimeMultipart("mixed");
		}
		else if ( == null &&  != null)
		{
			// HTML ONLY
			cover = new MimeMultipart("mixed");
		}
		else
		{
			// HTML + TEXT
			cover = new MimeMultipart("alternative");
			usingAlternative = true;
		}
		MimeMultipart content = cover;
		if (usingAlternative && hasAttachments)
		{
			content = new MimeMultipart("mixed");
			content.addBodyPart(toBodyPart(cover));
		}
		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 wrap = new MimeBodyPart();
		wrap.setContent(cover);
		return wrap;
	}
	{
		MimeBodyPart text = new MimeBodyPart();
		return text;
	}
	{
		MimeBodyPart html = new MimeBodyPart();
		html.setContent("text/html; charset=utf-8");
		return html;
	}
New to GrepCode? Check out our FAQ X