Last active
February 7, 2020 14:26
-
-
Save zach-m/acf186ff8ae9d0cc04ff to your computer and use it in GitHub Desktop.
Utility class to use JavaMail without the hassle of Multipart assembling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Authenticator; | |
import javax.mail.BodyPart; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.PasswordAuthentication; | |
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 EmailSender | |
{ | |
private static final String USERNAME = "[email protected]"; // change accordingly | |
private static final String PASSWORD = "********"; // change accordingly | |
private static final String DEFAULT_FROM = "Johnny <[email protected]>"; | |
private static final String DEFAULT_REPLY_TO = "[email protected]"; | |
/** | |
* 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(props, new Authenticator() | |
{ | |
protected PasswordAuthentication getPasswordAuthentication() | |
{ | |
return new PasswordAuthentication(USERNAME, PASSWORD); | |
} | |
}); | |
return session; | |
} | |
private MimeMessage msg = new MimeMessage(getSession());; | |
private String mText; | |
private String mHtml; | |
private List<Attachment> mAttachments = new ArrayList<>(); | |
// TODO: support embedded attachments (using "related" multipart) | |
// TODO: allow user to pass InputStream instead of byte array | |
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 EmailSender subject(String subject) throws MessagingException | |
{ | |
EmailSender sender = new EmailSender(); | |
sender.msg.setSubject(subject, "UTF-8"); | |
return sender.from(DEFAULT_FROM).replyTo(DEFAULT_REPLY_TO); | |
} | |
public EmailSender from(String from) throws AddressException, MessagingException | |
{ | |
msg.setFrom(new InternetAddress(from)); | |
return this; | |
} | |
public EmailSender replyTo(String replyTo) throws AddressException, MessagingException | |
{ | |
msg.setReplyTo(InternetAddress.parse(replyTo.replaceAll(";", ","))); | |
return this; | |
} | |
public EmailSender to(String to) throws AddressException, MessagingException | |
{ | |
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to.replaceAll(";", ","))); | |
return this; | |
} | |
public EmailSender cc(String cc) throws AddressException, MessagingException | |
{ | |
msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(cc.replaceAll(";", ","))); | |
return this; | |
} | |
public EmailSender bcc(String bcc) throws AddressException, MessagingException | |
{ | |
msg.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc.replaceAll(";", ","))); | |
return this; | |
} | |
public EmailSender text(String text) throws MessagingException | |
{ | |
mText = text; | |
return this; | |
} | |
public EmailSender html(String html) throws MessagingException | |
{ | |
mHtml = html; | |
return this; | |
} | |
public EmailSender 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 cover; | |
boolean usingAlternative = false; | |
boolean hasAttachments = mAttachments.size() > 0; | |
if (mText != null && mHtml == null) | |
{ | |
// TEXT ONLY | |
cover = new MimeMultipart("mixed"); | |
cover.addBodyPart(textPart()); | |
} | |
else if (mText == null && mHtml != null) | |
{ | |
// HTML ONLY | |
cover = new MimeMultipart("mixed"); | |
cover.addBodyPart(htmlPart()); | |
} | |
else | |
{ | |
// HTML + TEXT | |
cover = new MimeMultipart("alternative"); | |
cover.addBodyPart(textPart()); | |
cover.addBodyPart(htmlPart()); | |
usingAlternative = true; | |
} | |
MimeMultipart content = cover; | |
if (usingAlternative && hasAttachments) | |
{ | |
content = new MimeMultipart("mixed"); | |
content.addBodyPart(toBodyPart(cover)); | |
} | |
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 toBodyPart(MimeMultipart cover) throws MessagingException | |
{ | |
MimeBodyPart wrap = new MimeBodyPart(); | |
wrap.setContent(cover); | |
return wrap; | |
} | |
private MimeBodyPart textPart() throws MessagingException | |
{ | |
MimeBodyPart text = new MimeBodyPart(); | |
text.setText(mText); | |
return text; | |
} | |
private MimeBodyPart htmlPart() throws MessagingException | |
{ | |
MimeBodyPart html = new MimeBodyPart(); | |
html.setContent(mHtml, "text/html; charset=utf-8"); | |
return html; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
... | |
<dependencies> | |
<dependency> | |
<groupId>javax.mail</groupId> | |
<artifactId>mail</artifactId> | |
<version>1.4.7</version> | |
</dependency> | |
... | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
</configuration> | |
</plugin> | |
... | |
</plugins> | |
</build> | |
... | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// send an email with TEXT-only body | |
EmailSender.subject("Hello").to("[email protected]").cc("[email protected]").text("body as text").send(); | |
// send an email with HTML + TEXT version of the body | |
EmailSender.subject("Hello").to("[email protected]").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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome