JavaMailSenderImpl considers empty username and password as not set (for use with placeholders)

Also includes varargs declaration for applicable send methods.

Issue: SPR-12294
This commit is contained in:
Juergen Hoeller 2014-10-04 01:01:40 +02:00
parent b6a3808a97
commit cc02269c9f
3 changed files with 26 additions and 29 deletions

View File

@ -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.
@ -34,25 +34,19 @@ public interface MailSender {
/** /**
* Send the given simple mail message. * Send the given simple mail message.
* @param simpleMessage the message to send * @param simpleMessage the message to send
* @throws org.springframework.mail.MailParseException * @throws MailParseException in case of failure when parsing the message
* in case of failure when parsing the message * @throws MailAuthenticationException in case of authentication failure
* @throws org.springframework.mail.MailAuthenticationException * @throws MailSendException in case of failure when sending the message
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending the message
*/ */
void send(SimpleMailMessage simpleMessage) throws MailException; void send(SimpleMailMessage simpleMessage) throws MailException;
/** /**
* Send the given array of simple mail messages in batch. * Send the given array of simple mail messages in batch.
* @param simpleMessages the messages to send * @param simpleMessages the messages to send
* @throws org.springframework.mail.MailParseException * @throws MailParseException in case of failure when parsing a message
* in case of failure when parsing a message * @throws MailAuthenticationException in case of authentication failure
* @throws org.springframework.mail.MailAuthenticationException * @throws MailSendException in case of failure when sending a message
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending a message
*/ */
void send(SimpleMailMessage[] simpleMessages) throws MailException; void send(SimpleMailMessage... simpleMessages) throws MailException;
} }

View File

@ -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.
@ -17,7 +17,6 @@
package org.springframework.mail.javamail; package org.springframework.mail.javamail;
import java.io.InputStream; import java.io.InputStream;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException; import org.springframework.mail.MailException;
@ -104,7 +103,7 @@ public interface JavaMailSender extends MailSender {
* in case of failure when sending a message * in case of failure when sending a message
* @see #createMimeMessage * @see #createMimeMessage
*/ */
void send(MimeMessage[] mimeMessages) throws MailException; void send(MimeMessage... mimeMessages) throws MailException;
/** /**
* Send the JavaMail MIME message prepared by the given MimeMessagePreparator. * Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
@ -138,6 +137,6 @@ public interface JavaMailSender extends MailSender {
* @throws org.springframework.mail.MailSendException * @throws org.springframework.mail.MailSendException
* in case of failure when sending a message * in case of failure when sending a message
*/ */
void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException; void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException;
} }

View File

@ -16,7 +16,6 @@
package org.springframework.mail.javamail; package org.springframework.mail.javamail;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -298,7 +297,7 @@ public class JavaMailSenderImpl implements JavaMailSender {
} }
@Override @Override
public void send(SimpleMailMessage[] simpleMessages) throws MailException { public void send(SimpleMailMessage... simpleMessages) throws MailException {
List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(simpleMessages.length); List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(simpleMessages.length);
for (SimpleMailMessage simpleMessage : simpleMessages) { for (SimpleMailMessage simpleMessage : simpleMessages) {
MimeMailMessage message = new MimeMailMessage(createMimeMessage()); MimeMailMessage message = new MimeMailMessage(createMimeMessage());
@ -342,7 +341,7 @@ public class JavaMailSenderImpl implements JavaMailSender {
} }
@Override @Override
public void send(MimeMessage[] mimeMessages) throws MailException { public void send(MimeMessage... mimeMessages) throws MailException {
doSend(mimeMessages, null); doSend(mimeMessages, null);
} }
@ -352,7 +351,7 @@ public class JavaMailSenderImpl implements JavaMailSender {
} }
@Override @Override
public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException { public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
try { try {
List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(mimeMessagePreparators.length); List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(mimeMessagePreparators.length);
for (MimeMessagePreparator preparator : mimeMessagePreparators) { for (MimeMessagePreparator preparator : mimeMessagePreparators) {
@ -368,9 +367,6 @@ public class JavaMailSenderImpl implements JavaMailSender {
catch (MessagingException ex) { catch (MessagingException ex) {
throw new MailParseException(ex); throw new MailParseException(ex);
} }
catch (IOException ex) {
throw new MailPreparationException(ex);
}
catch (Exception ex) { catch (Exception ex) {
throw new MailPreparationException(ex); throw new MailPreparationException(ex);
} }
@ -389,12 +385,20 @@ 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 {
Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>(); 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;
}
}
Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>();
Transport transport; Transport transport;
try { try {
transport = getTransport(getSession()); transport = getTransport(getSession());
transport.connect(getHost(), getPort(), getUsername(), getPassword()); transport.connect(getHost(), getPort(), username, password);
} }
catch (AuthenticationFailedException ex) { catch (AuthenticationFailedException ex) {
throw new MailAuthenticationException(ex); throw new MailAuthenticationException(ex);