/************************************************************************* * * * EJBCA Community: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package org.ejbca.util.mail; import java.util.Date; import java.util.List; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; 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 org.apache.log4j.Logger; import org.ejbca.config.MailConfiguration; import org.ejbca.core.ejb.ServiceLocator; /** * Simple wrapper for JavaMail. * * @version $Id: MailSender.java 26387 2017-08-22 14:14:36Z mikekushner $ */ public class MailSender { private static final Logger log = Logger.getLogger(MailSender.class); // Some constants to make it easier to read the client code public final static List NO_TO = null; //List public final static List NO_CC = null; //List public final static List NO_ATTACHMENTS = null; //List /** * Helper method for sending mail using the mail service configured in mail.properties. * * @param fromAddress The "From" address * @param toList List of addresses that will end up in the "To"-field or null to disable * @param ccList List of addresses that will end up in the "Cc"-field or null to disable * @param subject The email subject * @param content The text message body * @param attachments List of files and objects to attach to the email or null to disable multipart messages * @throws MailException if the message could not be successfully handed over to JavaMail */ public static void sendMailOrThrow(String fromAddress, List toList, List ccList, String subject, String content, List attachments) throws MailException { if (!sendMail(fromAddress, toList, ccList, subject, content, attachments)) { throw new MailException("Failed to hand over email to JavaMail."); } } /** * Helper method for sending mail using the mail service configured in mail.properties. * * @param fromAddress The "From" address * @param toList List of addresses that will end up in the "To"-field or null to disable * @param ccList List of addresses that will end up in the "Cc"-field or null to disable * @param subject The email subject * @param content The text message body * @param attachments List of files and objects to attach to the email or null to disable multipart messages * @return true if the message was successfully handed over to JavaMail */ public static boolean sendMail(String fromAddress, List toList, List ccList, String subject, String content, List attachments) { Session mailSession = ServiceLocator.getInstance().getMailSession(MailConfiguration.getMailJndiName()); // It would be good if we could set mail session properties, but it seems not possible // See https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html // mail.smtp.timeout // mail.smtp.connectiontimeout // mail.smtp.writetimeout Message msg = new MimeMessage(mailSession); try { if (log.isDebugEnabled()) { log.debug("from: " + fromAddress); } msg.setFrom(new InternetAddress(fromAddress)); boolean atLeastOneRecipient = false; if (toList != null) { for (int i=0; i