com.crabshue.commons.mail.MailSender Maven / Gradle / Ivy
package com.crabshue.commons.mail;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.mail.exceptions.MailErrorContext;
import com.crabshue.commons.mail.exceptions.MailErrorType;
/**
* Utility for sending emails.
*
*/
public class MailSender {
/**
* The mail configuration
*/
private Properties configuration;
/**
* MailSender constructor.
*
* @param configuration The MailSender configuration.
*/
public MailSender(final Properties configuration) {
this.configuration = configuration;
}
/**
* Send email
*
* @param from Sender email address.
* @param to Recipients email addresses.
* @param cc Carbon Copy email addresses.
* @param bcc Black Carbon Copy email addresses.
* @param subject Subject of the message.
* @param isHtmlBody Whether the content is HTML or plain text.
* @param body The content of the message.
* @param attachments The attachments of the message.
*/
public void sendMail(final String from,
final String username,
final String password,
final Collection to,
final Collection cc,
final Collection bcc,
final String subject,
final String body,
final boolean isHtmlBody,
final Map attachments) {
// Get session instance with given configuration
final Session session = Session.getInstance(configuration,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
final MimeMessage message = new MimeMessage(session);
final MimeBodyPart messageBodyPart = new MimeBodyPart();
message.setFrom(new InternetAddress(from));
for (final String recipient : to) {
message.setRecipients(Message.RecipientType.TO, recipient);
}
for (final String recipient : cc) {
message.setRecipients(Message.RecipientType.CC, recipient);
}
for (final String recipient : bcc) {
message.setRecipients(Message.RecipientType.BCC, recipient);
}
message.setSubject(subject);
if (isHtmlBody) {
messageBodyPart.setContent(body, "text/html; charset=utf-8");
} else {
messageBodyPart.setText(body, StandardCharsets.UTF_8.name());
}
// Create a multipart message
final Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// add attachment(s)
addAttachments(attachments, multipart);
message.setContent(multipart);
Transport.send(message);
} catch (final MessagingException e) {
throw new RuntimeException(e);
}
}
private void addAttachments(final Map attachments, final Multipart multipart) {
for (final Map.Entry filenameToFilePath : attachments.entrySet()) {
final MimeBodyPart attachmentsBodyPart = new MimeBodyPart();
final String filename = filenameToFilePath.getKey();
final String filePath = filenameToFilePath.getValue();
final DataSource source = new FileDataSource(filePath);
try {
attachmentsBodyPart.setDataHandler(new DataHandler(source));
attachmentsBodyPart.setFileName(filename);
multipart.addBodyPart(attachmentsBodyPart);
} catch (final MessagingException e) {
throw new SystemException(MailErrorType.EMAIL_MESSAGE_ERROR, "cannot create body part with attachments", e)
.addContextValue(MailErrorContext.FILES, attachments);
}
}
}
}