use of cz.metacentrum.perun.notif.mail.EmailPreparationException in project perun by CESNET.
the class PerunNotifEmailManagerImpl method sendEmailsInBatch.
private void sendEmailsInBatch(List<PerunNotifPlainMessage> messageList) {
if (!sendMessages) {
return;
}
List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>();
// for logging purposes
List<String> messagesContents = new ArrayList<String>();
for (PerunNotifPlainMessage emailMessage : messageList) {
logger.debug("SENDING PLAIN MESSAGE ; to: " + emailMessage.getTo() + " ; cc: " + emailMessage.getCc() + " ; bcc: " + emailMessage.getBcc());
MimeMessage mimeMessage = createMimeMessage();
try {
emailMessage.prepare(mimeMessage);
mimeMessages.add(mimeMessage);
messagesContents.add(emailMessage.getContent());
} catch (Exception ex) {
failedEmailLogger.error(emailMessage.toString());
logger.error("Preparing message to send failed.", ex);
}
}
try {
doSend(mimeMessages.toArray(new MimeMessage[mimeMessages.size()]), messagesContents);
} catch (EmailSendException ex) {
Map<Object, Exception> failedMessages = ex.getFailedMessages();
if (failedMessages != null && !failedMessages.isEmpty()) {
for (Object key : failedMessages.keySet()) {
try {
MimeMessage message = (MimeMessage) key;
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
byte[] charData = out.toByteArray();
String str = new String(charData, Charset.forName("UTF-8"));
failedEmailLogger.error(str);
} catch (Exception e) {
logger.error("Failed to write log about unsended email.", ex);
}
}
}
logger.error("Sending of the email failed.", ex);
} catch (Exception ex) {
throw new EmailPreparationException(ex);
}
}
Aggregations