|
|
@ -65,470 +65,476 @@ import javax.mail.internet.MimeMultipart; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MailEngine { |
|
|
|
public class MailEngine { |
|
|
|
|
|
|
|
|
|
|
|
public static Session getSession() { |
|
|
|
public static Session getSession() { |
|
|
|
return getSession(false); |
|
|
|
return getSession(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Session getSession(Account account) { |
|
|
|
public static Session getSession(Account account) { |
|
|
|
Properties properties = _getProperties(account); |
|
|
|
Properties properties = _getProperties(account); |
|
|
|
|
|
|
|
|
|
|
|
Session session = Session.getInstance(properties); |
|
|
|
Session session = Session.getInstance(properties); |
|
|
|
|
|
|
|
|
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
session.setDebug(true); |
|
|
|
session.setDebug(true); |
|
|
|
|
|
|
|
|
|
|
|
session.getProperties().list(System.out); |
|
|
|
session.getProperties().list(System.out); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return session; |
|
|
|
return session; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Session getSession(boolean cache) { |
|
|
|
public static Session getSession(boolean cache) { |
|
|
|
Session session = null; |
|
|
|
Session session = null; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
session = MailServiceUtil.getSession(); |
|
|
|
session = MailServiceUtil.getSession(); |
|
|
|
} catch (SystemException se) { |
|
|
|
} catch (SystemException se) { |
|
|
|
if (_log.isWarnEnabled()) { |
|
|
|
if (_log.isWarnEnabled()) { |
|
|
|
_log.warn(se, se); |
|
|
|
_log.warn(se, se); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
session = InfrastructureUtil.getMailSession(); |
|
|
|
session = InfrastructureUtil.getMailSession(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
session.setDebug(true); |
|
|
|
session.setDebug(true); |
|
|
|
|
|
|
|
|
|
|
|
session.getProperties().list(System.out); |
|
|
|
session.getProperties().list(System.out); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return session; |
|
|
|
return session; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(byte[] bytes) throws MailEngineException { |
|
|
|
public static void send(byte[] bytes) throws MailEngineException { |
|
|
|
try { |
|
|
|
try { |
|
|
|
Session session = getSession(); |
|
|
|
Session session = getSession(); |
|
|
|
|
|
|
|
|
|
|
|
Message message = new MimeMessage(session, new UnsyncByteArrayInputStream(bytes)); |
|
|
|
Message message = new MimeMessage(session, new UnsyncByteArrayInputStream(bytes)); |
|
|
|
|
|
|
|
|
|
|
|
_send(session, message, null, _BATCH_SIZE); |
|
|
|
_send(session, message, null, _BATCH_SIZE); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
throw new MailEngineException(e); |
|
|
|
throw new MailEngineException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress to, String subject, String body) |
|
|
|
public static void send(InternetAddress from, InternetAddress to, String subject, String body) |
|
|
|
throws MailEngineException { |
|
|
|
throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, new InternetAddress[] {to}, null, null, subject, body, false, null, null, null); |
|
|
|
send(from, new InternetAddress[] { to }, null, null, subject, body, false, null, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress to, String subject, String body, boolean htmlFormat) |
|
|
|
public static void send(InternetAddress from, InternetAddress to, String subject, String body, |
|
|
|
throws MailEngineException { |
|
|
|
boolean htmlFormat) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, new InternetAddress[] {to}, null, null, subject, body, htmlFormat, null, null, null); |
|
|
|
send(from, new InternetAddress[] { to }, null, null, subject, body, htmlFormat, null, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, |
|
|
|
InternetAddress[] bulkAddresses, String subject, String body, boolean htmlFormat, InternetAddress[] replyTo, |
|
|
|
InternetAddress[] bcc, InternetAddress[] bulkAddresses, String subject, String body, |
|
|
|
String messageId, String inReplyTo) throws MailEngineException { |
|
|
|
boolean htmlFormat, InternetAddress[] replyTo, String messageId, String inReplyTo) |
|
|
|
|
|
|
|
throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, to, cc, bcc, bulkAddresses, subject, body, htmlFormat, replyTo, messageId, inReplyTo, null); |
|
|
|
send(from, to, cc, bcc, bulkAddresses, subject, body, htmlFormat, replyTo, messageId, inReplyTo, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, |
|
|
|
InternetAddress[] bulkAddresses, String subject, String body, boolean htmlFormat, InternetAddress[] replyTo, |
|
|
|
InternetAddress[] bcc, InternetAddress[] bulkAddresses, String subject, String body, |
|
|
|
String messageId, String inReplyTo, List<FileAttachment> fileAttachments) throws MailEngineException { |
|
|
|
boolean htmlFormat, InternetAddress[] replyTo, String messageId, String inReplyTo, |
|
|
|
|
|
|
|
List<FileAttachment> fileAttachments) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, to, cc, bcc, bulkAddresses, subject, body, htmlFormat, replyTo, messageId, inReplyTo, fileAttachments, |
|
|
|
send(from, to, cc, bcc, bulkAddresses, subject, body, htmlFormat, replyTo, messageId, inReplyTo, |
|
|
|
null); |
|
|
|
fileAttachments, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, |
|
|
|
InternetAddress[] bulkAddresses, String subject, String body, boolean htmlFormat, InternetAddress[] replyTo, |
|
|
|
InternetAddress[] bcc, InternetAddress[] bulkAddresses, String subject, String body, |
|
|
|
String messageId, String inReplyTo, List<FileAttachment> fileAttachments, SMTPAccount smtpAccount) |
|
|
|
boolean htmlFormat, InternetAddress[] replyTo, String messageId, String inReplyTo, |
|
|
|
throws MailEngineException { |
|
|
|
List<FileAttachment> fileAttachments, SMTPAccount smtpAccount) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
long startTime = System.currentTimeMillis(); |
|
|
|
long startTime = System.currentTimeMillis(); |
|
|
|
|
|
|
|
|
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
_log.debug("From: " + from); |
|
|
|
_log.debug("From: " + from); |
|
|
|
_log.debug("To: " + Arrays.toString(to)); |
|
|
|
_log.debug("To: " + Arrays.toString(to)); |
|
|
|
_log.debug("CC: " + Arrays.toString(cc)); |
|
|
|
_log.debug("CC: " + Arrays.toString(cc)); |
|
|
|
_log.debug("BCC: " + Arrays.toString(bcc)); |
|
|
|
_log.debug("BCC: " + Arrays.toString(bcc)); |
|
|
|
_log.debug("List Addresses: " + Arrays.toString(bulkAddresses)); |
|
|
|
_log.debug("List Addresses: " + Arrays.toString(bulkAddresses)); |
|
|
|
_log.debug("Subject: " + subject); |
|
|
|
_log.debug("Subject: " + subject); |
|
|
|
_log.debug("Body: " + body); |
|
|
|
_log.debug("Body: " + body); |
|
|
|
_log.debug("HTML Format: " + htmlFormat); |
|
|
|
_log.debug("HTML Format: " + htmlFormat); |
|
|
|
_log.debug("Reply to: " + Arrays.toString(replyTo)); |
|
|
|
_log.debug("Reply to: " + Arrays.toString(replyTo)); |
|
|
|
_log.debug("Message ID: " + messageId); |
|
|
|
_log.debug("Message ID: " + messageId); |
|
|
|
_log.debug("In Reply To: " + inReplyTo); |
|
|
|
_log.debug("In Reply To: " + inReplyTo); |
|
|
|
|
|
|
|
|
|
|
|
if ((fileAttachments != null) && _log.isDebugEnabled()) { |
|
|
|
if ((fileAttachments != null) && _log.isDebugEnabled()) { |
|
|
|
for (int i = 0; i < fileAttachments.size(); i++) { |
|
|
|
for (int i = 0; i < fileAttachments.size(); i++) { |
|
|
|
|
|
|
|
|
|
|
|
FileAttachment fileAttachment = fileAttachments.get(i); |
|
|
|
FileAttachment fileAttachment = fileAttachments.get(i); |
|
|
|
if (fileAttachment instanceof it.tref.liferay.portos.mailmanager.shared.model.FileAttachment) { |
|
|
|
if (fileAttachment instanceof it.tref.liferay.portos.mailmanager.shared.model.FileAttachment) { |
|
|
|
((it.tref.liferay.portos.mailmanager.shared.model.FileAttachment) fileAttachment).generateFile(); |
|
|
|
((it.tref.liferay.portos.mailmanager.shared.model.FileAttachment) fileAttachment) |
|
|
|
} |
|
|
|
.generateFile(); |
|
|
|
File file = fileAttachment.getFile(); |
|
|
|
} |
|
|
|
|
|
|
|
File file = fileAttachment.getFile(); |
|
|
|
|
|
|
|
|
|
|
|
if (file == null) { |
|
|
|
if (file == null) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_log.debug("Attachment " + i + " file " + file.getAbsolutePath() + " and file name " |
|
|
|
_log.debug("Attachment " + i + " file " + file.getAbsolutePath() + " and file name " |
|
|
|
+ fileAttachment.getFileName()); |
|
|
|
+ fileAttachment.getFileName()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
InternetAddressUtil.validateAddress(from); |
|
|
|
InternetAddressUtil.validateAddress(from); |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(to)) { |
|
|
|
if (ArrayUtil.isNotEmpty(to)) { |
|
|
|
InternetAddressUtil.validateAddresses(to); |
|
|
|
InternetAddressUtil.validateAddresses(to); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(cc)) { |
|
|
|
if (ArrayUtil.isNotEmpty(cc)) { |
|
|
|
InternetAddressUtil.validateAddresses(cc); |
|
|
|
InternetAddressUtil.validateAddresses(cc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(bcc)) { |
|
|
|
if (ArrayUtil.isNotEmpty(bcc)) { |
|
|
|
InternetAddressUtil.validateAddresses(bcc); |
|
|
|
InternetAddressUtil.validateAddresses(bcc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(replyTo)) { |
|
|
|
if (ArrayUtil.isNotEmpty(replyTo)) { |
|
|
|
InternetAddressUtil.validateAddresses(replyTo); |
|
|
|
InternetAddressUtil.validateAddresses(replyTo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(bulkAddresses)) { |
|
|
|
if (ArrayUtil.isNotEmpty(bulkAddresses)) { |
|
|
|
InternetAddressUtil.validateAddresses(bulkAddresses); |
|
|
|
InternetAddressUtil.validateAddresses(bulkAddresses); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Session session = null; |
|
|
|
Session session = null; |
|
|
|
|
|
|
|
|
|
|
|
if (smtpAccount == null) { |
|
|
|
if (smtpAccount == null) { |
|
|
|
session = getSession(); |
|
|
|
session = getSession(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
session = getSession(smtpAccount); |
|
|
|
session = getSession(smtpAccount); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Message message = new LiferayMimeMessage(session); |
|
|
|
Message message = new LiferayMimeMessage(session); |
|
|
|
|
|
|
|
|
|
|
|
message.addHeader("X-Auto-Response-Suppress", "AutoReply, DR, NDR, NRN, OOF, RN"); |
|
|
|
message.addHeader("X-Auto-Response-Suppress", "AutoReply, DR, NDR, NRN, OOF, RN"); |
|
|
|
|
|
|
|
|
|
|
|
message.setFrom(from); |
|
|
|
message.setFrom(from); |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(to)) { |
|
|
|
if (ArrayUtil.isNotEmpty(to)) { |
|
|
|
message.setRecipients(Message.RecipientType.TO, to); |
|
|
|
message.setRecipients(Message.RecipientType.TO, to); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(cc)) { |
|
|
|
if (ArrayUtil.isNotEmpty(cc)) { |
|
|
|
message.setRecipients(Message.RecipientType.CC, cc); |
|
|
|
message.setRecipients(Message.RecipientType.CC, cc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(bcc)) { |
|
|
|
if (ArrayUtil.isNotEmpty(bcc)) { |
|
|
|
message.setRecipients(Message.RecipientType.BCC, bcc); |
|
|
|
message.setRecipients(Message.RecipientType.BCC, bcc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
subject = GetterUtil.getString(subject); |
|
|
|
subject = GetterUtil.getString(subject); |
|
|
|
|
|
|
|
|
|
|
|
message.setSubject(subject); |
|
|
|
message.setSubject(subject); |
|
|
|
|
|
|
|
|
|
|
|
if ((fileAttachments != null) && (fileAttachments.size() > 0)) { |
|
|
|
if ((fileAttachments != null) && (fileAttachments.size() > 0)) { |
|
|
|
MimeMultipart rootMultipart = new MimeMultipart(_MULTIPART_TYPE_MIXED); |
|
|
|
MimeMultipart rootMultipart = new MimeMultipart(_MULTIPART_TYPE_MIXED); |
|
|
|
|
|
|
|
|
|
|
|
MimeMultipart messageMultipart = new MimeMultipart(_MULTIPART_TYPE_ALTERNATIVE); |
|
|
|
MimeMultipart messageMultipart = new MimeMultipart(_MULTIPART_TYPE_ALTERNATIVE); |
|
|
|
|
|
|
|
|
|
|
|
MimeBodyPart messageBodyPart = new MimeBodyPart(); |
|
|
|
MimeBodyPart messageBodyPart = new MimeBodyPart(); |
|
|
|
|
|
|
|
|
|
|
|
messageBodyPart.setContent(messageMultipart); |
|
|
|
messageBodyPart.setContent(messageMultipart); |
|
|
|
|
|
|
|
|
|
|
|
rootMultipart.addBodyPart(messageBodyPart); |
|
|
|
rootMultipart.addBodyPart(messageBodyPart); |
|
|
|
|
|
|
|
|
|
|
|
if (htmlFormat) { |
|
|
|
if (htmlFormat) { |
|
|
|
MimeBodyPart bodyPart = new MimeBodyPart(); |
|
|
|
MimeBodyPart bodyPart = new MimeBodyPart(); |
|
|
|
|
|
|
|
|
|
|
|
bodyPart.setContent(body, _TEXT_HTML); |
|
|
|
bodyPart.setContent(body, _TEXT_HTML); |
|
|
|
|
|
|
|
|
|
|
|
messageMultipart.addBodyPart(bodyPart); |
|
|
|
messageMultipart.addBodyPart(bodyPart); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
MimeBodyPart bodyPart = new MimeBodyPart(); |
|
|
|
MimeBodyPart bodyPart = new MimeBodyPart(); |
|
|
|
|
|
|
|
|
|
|
|
bodyPart.setText(body); |
|
|
|
bodyPart.setText(body); |
|
|
|
|
|
|
|
|
|
|
|
messageMultipart.addBodyPart(bodyPart); |
|
|
|
messageMultipart.addBodyPart(bodyPart); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < fileAttachments.size(); i++) { |
|
|
|
for (int i = 0; i < fileAttachments.size(); i++) { |
|
|
|
FileAttachment fileAttachment = fileAttachments.get(i); |
|
|
|
FileAttachment fileAttachment = fileAttachments.get(i); |
|
|
|
|
|
|
|
|
|
|
|
File file = fileAttachment.getFile(); |
|
|
|
File file = fileAttachment.getFile(); |
|
|
|
|
|
|
|
|
|
|
|
if (file == null) { |
|
|
|
if (file == null) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
MimeBodyPart mimeBodyPart = new MimeBodyPart(); |
|
|
|
MimeBodyPart mimeBodyPart = new MimeBodyPart(); |
|
|
|
|
|
|
|
|
|
|
|
DataSource dataSource = new FileDataSource(file); |
|
|
|
DataSource dataSource = new FileDataSource(file); |
|
|
|
|
|
|
|
|
|
|
|
mimeBodyPart.setDataHandler(new DataHandler(dataSource)); |
|
|
|
mimeBodyPart.setDataHandler(new DataHandler(dataSource)); |
|
|
|
mimeBodyPart.setDisposition(Part.ATTACHMENT); |
|
|
|
mimeBodyPart.setDisposition(Part.ATTACHMENT); |
|
|
|
|
|
|
|
|
|
|
|
if (fileAttachment.getFileName() != null) { |
|
|
|
if (fileAttachment.getFileName() != null) { |
|
|
|
mimeBodyPart.setFileName(fileAttachment.getFileName()); |
|
|
|
mimeBodyPart.setFileName(fileAttachment.getFileName()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mimeBodyPart.setFileName(file.getName()); |
|
|
|
mimeBodyPart.setFileName(file.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rootMultipart.addBodyPart(mimeBodyPart); |
|
|
|
rootMultipart.addBodyPart(mimeBodyPart); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
message.setContent(rootMultipart); |
|
|
|
message.setContent(rootMultipart); |
|
|
|
|
|
|
|
|
|
|
|
message.saveChanges(); |
|
|
|
message.saveChanges(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (htmlFormat) { |
|
|
|
if (htmlFormat) { |
|
|
|
message.setContent(body, _TEXT_HTML); |
|
|
|
message.setContent(body, _TEXT_HTML); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
message.setContent(body, _TEXT_PLAIN); |
|
|
|
message.setContent(body, _TEXT_PLAIN); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
message.setSentDate(new Date()); |
|
|
|
message.setSentDate(new Date()); |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(replyTo)) { |
|
|
|
if (ArrayUtil.isNotEmpty(replyTo)) { |
|
|
|
message.setReplyTo(replyTo); |
|
|
|
message.setReplyTo(replyTo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (messageId != null) { |
|
|
|
if (messageId != null) { |
|
|
|
message.setHeader("Message-ID", messageId); |
|
|
|
message.setHeader("Message-ID", messageId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (inReplyTo != null) { |
|
|
|
if (inReplyTo != null) { |
|
|
|
message.setHeader("In-Reply-To", inReplyTo); |
|
|
|
message.setHeader("In-Reply-To", inReplyTo); |
|
|
|
message.setHeader("References", inReplyTo); |
|
|
|
message.setHeader("References", inReplyTo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int batchSize = GetterUtil.getInteger(PropsUtil.get(PropsKeys.MAIL_BATCH_SIZE), _BATCH_SIZE); |
|
|
|
int batchSize = GetterUtil.getInteger(PropsUtil.get(PropsKeys.MAIL_BATCH_SIZE), _BATCH_SIZE); |
|
|
|
|
|
|
|
|
|
|
|
_send(session, message, bulkAddresses, batchSize); |
|
|
|
_send(session, message, bulkAddresses, batchSize); |
|
|
|
} catch (SendFailedException sfe) { |
|
|
|
} catch (SendFailedException sfe) { |
|
|
|
_log.error(sfe); |
|
|
|
_log.error(sfe); |
|
|
|
|
|
|
|
|
|
|
|
// if (_isThrowsExceptionOnFailure()) {
|
|
|
|
// if (_isThrowsExceptionOnFailure()) {
|
|
|
|
// throw new MailEngineException(sfe);
|
|
|
|
// throw new MailEngineException(sfe);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
throw new MailEngineException(sfe); |
|
|
|
throw new MailEngineException(sfe); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
throw new MailEngineException(e); |
|
|
|
throw new MailEngineException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
if (_log.isDebugEnabled()) { |
|
|
|
_log.debug("Sending mail takes " + (System.currentTimeMillis() - startTime) + " ms"); |
|
|
|
_log.debug("Sending mail takes " + (System.currentTimeMillis() - startTime) + " ms"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, |
|
|
|
String subject, String body) throws MailEngineException { |
|
|
|
InternetAddress[] bcc, String subject, String body) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, to, cc, bcc, subject, body, false, null, null, null); |
|
|
|
send(from, to, cc, bcc, subject, body, false, null, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, |
|
|
|
String subject, String body, boolean htmlFormat, InternetAddress[] replyTo, String messageId, String inReplyTo) |
|
|
|
InternetAddress[] bcc, String subject, String body, boolean htmlFormat, |
|
|
|
throws MailEngineException { |
|
|
|
InternetAddress[] replyTo, String messageId, String inReplyTo) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, to, cc, bcc, null, subject, body, htmlFormat, replyTo, messageId, inReplyTo, null); |
|
|
|
send(from, to, cc, bcc, null, subject, body, htmlFormat, replyTo, messageId, inReplyTo, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, String subject, |
|
|
|
send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, String subject, String body) |
|
|
|
String body) throws MailEngineException { |
|
|
|
throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
send(from, to, cc, null, subject, body, false, null, null, null); |
|
|
|
send(from, to, cc, null, subject, body, false, null, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, String subject, |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, String subject, |
|
|
|
String body, boolean htmlFormat) throws MailEngineException { |
|
|
|
String body, boolean htmlFormat) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, to, cc, null, subject, body, htmlFormat, null, null, null); |
|
|
|
send(from, to, cc, null, subject, body, htmlFormat, null, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, String subject, String body) |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, String subject, String body) |
|
|
|
throws MailEngineException { |
|
|
|
throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, to, null, null, subject, body, false, null, null, null); |
|
|
|
send(from, to, null, null, subject, body, false, null, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, String subject, String body, boolean htmlFormat) |
|
|
|
public static void send(InternetAddress from, InternetAddress[] to, String subject, String body, |
|
|
|
throws MailEngineException { |
|
|
|
boolean htmlFormat) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(from, to, null, null, subject, body, htmlFormat, null, null, null); |
|
|
|
send(from, to, null, null, subject, body, htmlFormat, null, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(MailMessage mailMessage) throws MailEngineException { |
|
|
|
public static void send(MailMessage mailMessage) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
send(mailMessage.getFrom(), mailMessage.getTo(), mailMessage.getCC(), mailMessage.getBCC(), mailMessage |
|
|
|
send(mailMessage.getFrom(), mailMessage.getTo(), mailMessage.getCC(), mailMessage.getBCC(), |
|
|
|
.getBulkAddresses(), mailMessage.getSubject(), mailMessage.getBody(), mailMessage.isHTMLFormat(), mailMessage |
|
|
|
mailMessage.getBulkAddresses(), mailMessage.getSubject(), mailMessage.getBody(), |
|
|
|
.getReplyTo(), mailMessage.getMessageId(), mailMessage.getInReplyTo(), mailMessage.getFileAttachments(), |
|
|
|
mailMessage.isHTMLFormat(), mailMessage.getReplyTo(), mailMessage.getMessageId(), |
|
|
|
mailMessage.getSMTPAccount()); |
|
|
|
mailMessage.getInReplyTo(), mailMessage.getFileAttachments(), mailMessage.getSMTPAccount()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(String from, String to, String subject, String body) throws MailEngineException { |
|
|
|
public static void send(String from, String to, String subject, String body) throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
send(new InternetAddress(from), new InternetAddress(to), subject, body); |
|
|
|
send(new InternetAddress(from), new InternetAddress(to), subject, body); |
|
|
|
} catch (AddressException ae) { |
|
|
|
} catch (AddressException ae) { |
|
|
|
throw new MailEngineException(ae); |
|
|
|
throw new MailEngineException(ae); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Address[] _getBatchAddresses(Address[] addresses, int index, int batchSize) { |
|
|
|
private static Address[] _getBatchAddresses(Address[] addresses, int index, int batchSize) { |
|
|
|
|
|
|
|
|
|
|
|
if ((batchSize == _BATCH_SIZE) && (index == 0)) { |
|
|
|
if ((batchSize == _BATCH_SIZE) && (index == 0)) { |
|
|
|
return addresses; |
|
|
|
return addresses; |
|
|
|
} else if (batchSize == _BATCH_SIZE) { |
|
|
|
} else if (batchSize == _BATCH_SIZE) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int start = index * batchSize; |
|
|
|
int start = index * batchSize; |
|
|
|
|
|
|
|
|
|
|
|
if (start > addresses.length) { |
|
|
|
if (start > addresses.length) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int end = ((index + 1) * batchSize); |
|
|
|
int end = ((index + 1) * batchSize); |
|
|
|
|
|
|
|
|
|
|
|
if (end > addresses.length) { |
|
|
|
if (end > addresses.length) { |
|
|
|
end = addresses.length; |
|
|
|
end = addresses.length; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ArrayUtil.subset(addresses, start, end); |
|
|
|
return ArrayUtil.subset(addresses, start, end); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Properties _getProperties(Account account) { |
|
|
|
private static Properties _getProperties(Account account) { |
|
|
|
Properties properties = new Properties(); |
|
|
|
Properties properties = new Properties(); |
|
|
|
|
|
|
|
|
|
|
|
String protocol = account.getProtocol(); |
|
|
|
String protocol = account.getProtocol(); |
|
|
|
|
|
|
|
|
|
|
|
properties.setProperty("mail.transport.protocol", protocol); |
|
|
|
properties.setProperty("mail.transport.protocol", protocol); |
|
|
|
properties.setProperty("mail." + protocol + ".host", account.getHost()); |
|
|
|
properties.setProperty("mail." + protocol + ".host", account.getHost()); |
|
|
|
properties.setProperty("mail." + protocol + ".port", String.valueOf(account.getPort())); |
|
|
|
properties.setProperty("mail." + protocol + ".port", String.valueOf(account.getPort())); |
|
|
|
|
|
|
|
|
|
|
|
if (account.isRequiresAuthentication()) { |
|
|
|
if (account.isRequiresAuthentication()) { |
|
|
|
properties.setProperty("mail." + protocol + ".auth", "true"); |
|
|
|
properties.setProperty("mail." + protocol + ".auth", "true"); |
|
|
|
properties.setProperty("mail." + protocol + ".user", account.getUser()); |
|
|
|
properties.setProperty("mail." + protocol + ".user", account.getUser()); |
|
|
|
properties.setProperty("mail." + protocol + ".password", account.getPassword()); |
|
|
|
properties.setProperty("mail." + protocol + ".password", account.getPassword()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (account.isSecure()) { |
|
|
|
if (account.isSecure()) { |
|
|
|
properties.setProperty("mail." + protocol + ".socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
|
|
|
properties.setProperty("mail." + protocol + ".socketFactory.class", |
|
|
|
properties.setProperty("mail." + protocol + ".socketFactory.fallback", "false"); |
|
|
|
"javax.net.ssl.SSLSocketFactory"); |
|
|
|
properties.setProperty("mail." + protocol + ".socketFactory.port", String.valueOf(account.getPort())); |
|
|
|
properties.setProperty("mail." + protocol + ".socketFactory.fallback", "false"); |
|
|
|
} |
|
|
|
properties.setProperty("mail." + protocol + ".socketFactory.port", |
|
|
|
|
|
|
|
String.valueOf(account.getPort())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return properties; |
|
|
|
return properties; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static String _getSMTPProperty(Session session, String suffix) { |
|
|
|
private static String _getSMTPProperty(Session session, String suffix) { |
|
|
|
String protocol = GetterUtil.getString(session.getProperty("mail.transport.protocol")); |
|
|
|
String protocol = GetterUtil.getString(session.getProperty("mail.transport.protocol")); |
|
|
|
|
|
|
|
|
|
|
|
if (protocol.equals(Account.PROTOCOL_SMTPS)) { |
|
|
|
if (protocol.equals(Account.PROTOCOL_SMTPS)) { |
|
|
|
return session.getProperty("mail.smtps." + suffix); |
|
|
|
return session.getProperty("mail.smtps." + suffix); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return session.getProperty("mail.smtp." + suffix); |
|
|
|
return session.getProperty("mail.smtp." + suffix); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// private static boolean _isThrowsExceptionOnFailure() {
|
|
|
|
// private static boolean _isThrowsExceptionOnFailure() {
|
|
|
|
// return GetterUtil.getBoolean(PropsUtil.get(PropsKeys.MAIL_THROWS_EXCEPTION_ON_FAILURE));
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// GetterUtil.getBoolean(PropsUtil.get(PropsKeys.MAIL_THROWS_EXCEPTION_ON_FAILURE));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
private static void _send(Session session, Message message, InternetAddress[] bulkAddresses, int batchSize) |
|
|
|
private static void _send(Session session, Message message, InternetAddress[] bulkAddresses, int batchSize) |
|
|
|
throws MailEngineException { |
|
|
|
throws MailEngineException { |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
boolean smtpAuth = GetterUtil.getBoolean(_getSMTPProperty(session, "auth"), false); |
|
|
|
boolean smtpAuth = GetterUtil.getBoolean(_getSMTPProperty(session, "auth"), false); |
|
|
|
String smtpHost = _getSMTPProperty(session, "host"); |
|
|
|
String smtpHost = _getSMTPProperty(session, "host"); |
|
|
|
int smtpPort = GetterUtil.getInteger(_getSMTPProperty(session, "port"), Account.PORT_SMTP); |
|
|
|
int smtpPort = GetterUtil.getInteger(_getSMTPProperty(session, "port"), Account.PORT_SMTP); |
|
|
|
String user = _getSMTPProperty(session, "user"); |
|
|
|
String user = _getSMTPProperty(session, "user"); |
|
|
|
String password = _getSMTPProperty(session, "password"); |
|
|
|
String password = _getSMTPProperty(session, "password"); |
|
|
|
|
|
|
|
|
|
|
|
if (smtpAuth && Validator.isNotNull(user) && Validator.isNotNull(password)) { |
|
|
|
if (smtpAuth && Validator.isNotNull(user) && Validator.isNotNull(password)) { |
|
|
|
|
|
|
|
|
|
|
|
String protocol = GetterUtil.getString(session.getProperty("mail.transport.protocol"), Account.PROTOCOL_SMTP); |
|
|
|
String protocol = GetterUtil.getString(session.getProperty("mail.transport.protocol"), |
|
|
|
|
|
|
|
Account.PROTOCOL_SMTP); |
|
|
|
|
|
|
|
|
|
|
|
Transport transport = session.getTransport(protocol); |
|
|
|
Transport transport = session.getTransport(protocol); |
|
|
|
|
|
|
|
|
|
|
|
transport.connect(smtpHost, smtpPort, user, password); |
|
|
|
transport.connect(smtpHost, smtpPort, user, password); |
|
|
|
|
|
|
|
|
|
|
|
Address[] addresses = null; |
|
|
|
Address[] addresses = null; |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isNotEmpty(bulkAddresses)) { |
|
|
|
if (ArrayUtil.isNotEmpty(bulkAddresses)) { |
|
|
|
addresses = bulkAddresses; |
|
|
|
addresses = bulkAddresses; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
addresses = message.getAllRecipients(); |
|
|
|
addresses = message.getAllRecipients(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0;; i++) { |
|
|
|
for (int i = 0;; i++) { |
|
|
|
Address[] batchAddresses = _getBatchAddresses(addresses, i, batchSize); |
|
|
|
Address[] batchAddresses = _getBatchAddresses(addresses, i, batchSize); |
|
|
|
|
|
|
|
|
|
|
|
if (ArrayUtil.isEmpty(batchAddresses)) { |
|
|
|
if (ArrayUtil.isEmpty(batchAddresses)) { |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
transport.sendMessage(message, batchAddresses); |
|
|
|
transport.sendMessage(message, batchAddresses); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
transport.close(); |
|
|
|
transport.close(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (ArrayUtil.isNotEmpty(bulkAddresses)) { |
|
|
|
if (ArrayUtil.isNotEmpty(bulkAddresses)) { |
|
|
|
int curBatch = 0; |
|
|
|
int curBatch = 0; |
|
|
|
|
|
|
|
|
|
|
|
Address[] portion = _getBatchAddresses(bulkAddresses, curBatch, batchSize); |
|
|
|
Address[] portion = _getBatchAddresses(bulkAddresses, curBatch, batchSize); |
|
|
|
|
|
|
|
|
|
|
|
while (ArrayUtil.isNotEmpty(portion)) { |
|
|
|
while (ArrayUtil.isNotEmpty(portion)) { |
|
|
|
Transport.send(message, portion); |
|
|
|
Transport.send(message, portion); |
|
|
|
|
|
|
|
|
|
|
|
curBatch++; |
|
|
|
curBatch++; |
|
|
|
|
|
|
|
|
|
|
|
portion = _getBatchAddresses(bulkAddresses, curBatch, batchSize); |
|
|
|
portion = _getBatchAddresses(bulkAddresses, curBatch, batchSize); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Transport.send(message); |
|
|
|
Transport.send(message); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (MessagingException me) { |
|
|
|
} catch (MessagingException me) { |
|
|
|
if (me.getNextException() instanceof SocketException) { |
|
|
|
if (me.getNextException() instanceof SocketException) { |
|
|
|
if (_log.isWarnEnabled()) { |
|
|
|
if (_log.isWarnEnabled()) { |
|
|
|
_log.warn("Failed to connect to a valid mail server. Please " + "make sure one is properly configured. " |
|
|
|
_log.warn("Failed to connect to a valid mail server. Please " |
|
|
|
+ me.getMessage()); |
|
|
|
+ "make sure one is properly configured. " + me.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
LogUtil.log(_log, me); |
|
|
|
LogUtil.log(_log, me); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// if (_isThrowsExceptionOnFailure()) {
|
|
|
|
// if (_isThrowsExceptionOnFailure()) {
|
|
|
|
// throw new MailEngineException(me);
|
|
|
|
// throw new MailEngineException(me);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
throw new MailEngineException(me); |
|
|
|
throw new MailEngineException(me); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static final int _BATCH_SIZE = 0; |
|
|
|
private static final int _BATCH_SIZE = 0; |
|
|
|
|
|
|
|
|
|
|
|
private static final String _MULTIPART_TYPE_ALTERNATIVE = "alternative"; |
|
|
|
private static final String _MULTIPART_TYPE_ALTERNATIVE = "alternative"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String _MULTIPART_TYPE_MIXED = "mixed"; |
|
|
|
private static final String _MULTIPART_TYPE_MIXED = "mixed"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String _TEXT_HTML = "text/html;charset=\"UTF-8\""; |
|
|
|
private static final String _TEXT_HTML = "text/html;charset=\"UTF-8\""; |
|
|
|
|
|
|
|
|
|
|
|
private static final String _TEXT_PLAIN = "text/plain;charset=\"UTF-8\""; |
|
|
|
private static final String _TEXT_PLAIN = "text/plain;charset=\"UTF-8\""; |
|
|
|
|
|
|
|
|
|
|
|
private static Log _log = LogFactoryUtil.getLog(MailEngine.class); |
|
|
|
private static Log _log = LogFactoryUtil.getLog(MailEngine.class); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|