Search in sources :

Example 1 with EmailPreparationException

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);
    }
}
Also used : PerunNotifPlainMessage(cz.metacentrum.perun.notif.mail.PerunNotifPlainMessage) EmailPreparationException(cz.metacentrum.perun.notif.mail.EmailPreparationException) MimeMessage(javax.mail.internet.MimeMessage) EmailSendException(cz.metacentrum.perun.notif.mail.exception.EmailSendException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EmailPreparationException(cz.metacentrum.perun.notif.mail.EmailPreparationException) EmailAuthenticationException(cz.metacentrum.perun.notif.mail.exception.EmailAuthenticationException) IOException(java.io.IOException) EmailSendException(cz.metacentrum.perun.notif.mail.exception.EmailSendException) EmailException(cz.metacentrum.perun.notif.mail.exception.EmailException)

Aggregations

EmailPreparationException (cz.metacentrum.perun.notif.mail.EmailPreparationException)1 PerunNotifPlainMessage (cz.metacentrum.perun.notif.mail.PerunNotifPlainMessage)1 EmailAuthenticationException (cz.metacentrum.perun.notif.mail.exception.EmailAuthenticationException)1 EmailException (cz.metacentrum.perun.notif.mail.exception.EmailException)1 EmailSendException (cz.metacentrum.perun.notif.mail.exception.EmailSendException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 MimeMessage (javax.mail.internet.MimeMessage)1