|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -330,7 +330,7 @@ public class JavaMailSenderImpl implements JavaMailSender { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return new MimeMessage(getSession(), contentStream); |
|
|
|
return new MimeMessage(getSession(), contentStream); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (MessagingException ex) { |
|
|
|
catch (Exception ex) { |
|
|
|
throw new MailParseException("Could not parse raw MIME content", ex); |
|
|
|
throw new MailParseException("Could not parse raw MIME content", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -385,35 +385,40 @@ public class JavaMailSenderImpl implements JavaMailSender { |
|
|
|
* in case of failure when sending a message |
|
|
|
* in case of failure when sending a message |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException { |
|
|
|
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException { |
|
|
|
String username = getUsername(); |
|
|
|
Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>(); |
|
|
|
String password = getPassword(); |
|
|
|
Transport transport = null; |
|
|
|
if ("".equals(username)) { // probably from a placeholder
|
|
|
|
|
|
|
|
username = null; |
|
|
|
try { |
|
|
|
if ("".equals(password)) { // in conjunction with "" username, this means no password to use
|
|
|
|
for (int i = 0; i < mimeMessages.length; i++) { |
|
|
|
password = null; |
|
|
|
|
|
|
|
|
|
|
|
// 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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>(); |
|
|
|
|
|
|
|
Transport transport; |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
transport = getTransport(getSession()); |
|
|
|
transport = connectTransport(); |
|
|
|
transport.connect(getHost(), getPort(), username, password); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
catch (AuthenticationFailedException ex) { |
|
|
|
catch (AuthenticationFailedException ex) { |
|
|
|
throw new MailAuthenticationException(ex); |
|
|
|
throw new MailAuthenticationException(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (MessagingException ex) { |
|
|
|
catch (Exception ex) { |
|
|
|
// Effectively, all messages failed...
|
|
|
|
// Effectively, all remaining messages failed...
|
|
|
|
for (int i = 0; i < mimeMessages.length; i++) { |
|
|
|
for (int j = i; j < mimeMessages.length; j++) { |
|
|
|
Object original = (originalMessages != null ? originalMessages[i] : mimeMessages[i]); |
|
|
|
Object original = (originalMessages != null ? originalMessages[j] : mimeMessages[j]); |
|
|
|
failedMessages.put(original, ex); |
|
|
|
failedMessages.put(original, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
throw new MailSendException("Mail server connection failed", ex, failedMessages); |
|
|
|
throw new MailSendException("Mail server connection failed", ex, failedMessages); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
// Send message via current transport...
|
|
|
|
for (int i = 0; i < mimeMessages.length; i++) { |
|
|
|
|
|
|
|
MimeMessage mimeMessage = mimeMessages[i]; |
|
|
|
MimeMessage mimeMessage = mimeMessages[i]; |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (mimeMessage.getSentDate() == null) { |
|
|
|
if (mimeMessage.getSentDate() == null) { |
|
|
|
@ -427,7 +432,7 @@ public class JavaMailSenderImpl implements JavaMailSender { |
|
|
|
} |
|
|
|
} |
|
|
|
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); |
|
|
|
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (MessagingException ex) { |
|
|
|
catch (Exception ex) { |
|
|
|
Object original = (originalMessages != null ? originalMessages[i] : mimeMessage); |
|
|
|
Object original = (originalMessages != null ? originalMessages[i] : mimeMessage); |
|
|
|
failedMessages.put(original, ex); |
|
|
|
failedMessages.put(original, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -435,9 +440,11 @@ public class JavaMailSenderImpl implements JavaMailSender { |
|
|
|
} |
|
|
|
} |
|
|
|
finally { |
|
|
|
finally { |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
if (transport != null) { |
|
|
|
transport.close(); |
|
|
|
transport.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (MessagingException ex) { |
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) { |
|
|
|
if (!failedMessages.isEmpty()) { |
|
|
|
if (!failedMessages.isEmpty()) { |
|
|
|
throw new MailSendException("Failed to close server connection after message failures", ex, |
|
|
|
throw new MailSendException("Failed to close server connection after message failures", ex, |
|
|
|
failedMessages); |
|
|
|
failedMessages); |
|
|
|
@ -453,11 +460,39 @@ public class JavaMailSenderImpl implements JavaMailSender { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Obtain and connect a Transport from the underlying JavaMail Session, |
|
|
|
|
|
|
|
* passing in the specified host, port, username, and password. |
|
|
|
|
|
|
|
* @return the connected Transport object |
|
|
|
|
|
|
|
* @throws MessagingException if the connect attempt failed |
|
|
|
|
|
|
|
* @since 4.1.2 |
|
|
|
|
|
|
|
* @see #getTransport |
|
|
|
|
|
|
|
* @see #getHost() |
|
|
|
|
|
|
|
* @see #getPort() |
|
|
|
|
|
|
|
* @see #getUsername() |
|
|
|
|
|
|
|
* @see #getPassword() |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected Transport connectTransport() throws MessagingException { |
|
|
|
|
|
|
|
String username = getUsername(); |
|
|
|
|
|
|
|
String password = getPassword(); |
|
|
|
|
|
|
|
if ("".equals(username)) { // probably from a placeholder
|
|
|
|
|
|
|
|
username = null; |
|
|
|
|
|
|
|
if ("".equals(password)) { // in conjunction with "" username, this means no password to use
|
|
|
|
|
|
|
|
password = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Transport transport = getTransport(getSession()); |
|
|
|
|
|
|
|
transport.connect(getHost(), getPort(), username, password); |
|
|
|
|
|
|
|
return transport; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a Transport object from the given JavaMail Session, |
|
|
|
* Obtain a Transport object from the given JavaMail Session, |
|
|
|
* using the configured protocol. |
|
|
|
* using the configured protocol. |
|
|
|
* <p>Can be overridden in subclasses, e.g. to return a mock Transport object. |
|
|
|
* <p>Can be overridden in subclasses, e.g. to return a mock Transport object. |
|
|
|
* @see javax.mail.Session#getTransport(String) |
|
|
|
* @see javax.mail.Session#getTransport(String) |
|
|
|
|
|
|
|
* @see #getSession() |
|
|
|
* @see #getProtocol() |
|
|
|
* @see #getProtocol() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected Transport getTransport(Session session) throws NoSuchProviderException { |
|
|
|
protected Transport getTransport(Session session) throws NoSuchProviderException { |
|
|
|
|