Search in sources :

Example 1 with MailAuthenticationException

use of org.springframework.mail.MailAuthenticationException in project spring-framework by spring-projects.

the class JavaMailSenderImpl method doSend.

/**
	 * Actually send the given array of MimeMessages via JavaMail.
	 * @param mimeMessages MimeMessage objects to send
	 * @param originalMessages corresponding original message objects
	 * that the MimeMessages have been created from (with same array
	 * length and indices as the "mimeMessages" array), if any
	 * @throws org.springframework.mail.MailAuthenticationException
	 * in case of authentication failure
	 * @throws org.springframework.mail.MailSendException
	 * in case of failure when sending a message
	 */
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
    Map<Object, Exception> failedMessages = new LinkedHashMap<>();
    Transport transport = null;
    try {
        for (int i = 0; i < mimeMessages.length; i++) {
            // Check transport connection first...
            if (transport == null || !transport.isConnected()) {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                    // Ignore - we're reconnecting anyway
                    }
                    transport = null;
                }
                try {
                    transport = connectTransport();
                } catch (AuthenticationFailedException ex) {
                    throw new MailAuthenticationException(ex);
                } catch (Exception ex) {
                    // Effectively, all remaining messages failed...
                    for (int j = i; j < mimeMessages.length; j++) {
                        Object original = (originalMessages != null ? originalMessages[j] : mimeMessages[j]);
                        failedMessages.put(original, ex);
                    }
                    throw new MailSendException("Mail server connection failed", ex, failedMessages);
                }
            }
            // Send message via current transport...
            MimeMessage mimeMessage = mimeMessages[i];
            try {
                if (mimeMessage.getSentDate() == null) {
                    mimeMessage.setSentDate(new Date());
                }
                String messageId = mimeMessage.getMessageID();
                mimeMessage.saveChanges();
                if (messageId != null) {
                    // Preserve explicitly specified message id...
                    mimeMessage.setHeader(HEADER_MESSAGE_ID, messageId);
                }
                transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            } catch (Exception ex) {
                Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
                failedMessages.put(original, ex);
            }
        }
    } finally {
        try {
            if (transport != null) {
                transport.close();
            }
        } catch (Exception ex) {
            if (!failedMessages.isEmpty()) {
                throw new MailSendException("Failed to close server connection after message failures", ex, failedMessages);
            } else {
                throw new MailSendException("Failed to close server connection after message sending", ex);
            }
        }
    }
    if (!failedMessages.isEmpty()) {
        throw new MailSendException(failedMessages);
    }
}
Also used : MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MailSendException(org.springframework.mail.MailSendException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MimeMessage(javax.mail.internet.MimeMessage) Transport(javax.mail.Transport) MailParseException(org.springframework.mail.MailParseException) MessagingException(javax.mail.MessagingException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MailSendException(org.springframework.mail.MailSendException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) NoSuchProviderException(javax.mail.NoSuchProviderException) MailPreparationException(org.springframework.mail.MailPreparationException) MailException(org.springframework.mail.MailException) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 AuthenticationFailedException (javax.mail.AuthenticationFailedException)1 MessagingException (javax.mail.MessagingException)1 NoSuchProviderException (javax.mail.NoSuchProviderException)1 Transport (javax.mail.Transport)1 MimeMessage (javax.mail.internet.MimeMessage)1 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)1 MailException (org.springframework.mail.MailException)1 MailParseException (org.springframework.mail.MailParseException)1 MailPreparationException (org.springframework.mail.MailPreparationException)1 MailSendException (org.springframework.mail.MailSendException)1