Skip to content

Commit cc02269

Browse files
committed
JavaMailSenderImpl considers empty username and password as not set (for use with placeholders)
Also includes varargs declaration for applicable send methods. Issue: SPR-12294
1 parent b6a3808 commit cc02269

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

spring-context-support/src/main/java/org/springframework/mail/MailSender.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,25 +34,19 @@ public interface MailSender {
3434
/**
3535
* Send the given simple mail message.
3636
* @param simpleMessage the message to send
37-
* @throws org.springframework.mail.MailParseException
38-
* in case of failure when parsing the message
39-
* @throws org.springframework.mail.MailAuthenticationException
40-
* in case of authentication failure
41-
* @throws org.springframework.mail.MailSendException
42-
* in case of failure when sending the message
37+
* @throws MailParseException in case of failure when parsing the message
38+
* @throws MailAuthenticationException in case of authentication failure
39+
* @throws MailSendException in case of failure when sending the message
4340
*/
4441
void send(SimpleMailMessage simpleMessage) throws MailException;
4542

4643
/**
4744
* Send the given array of simple mail messages in batch.
4845
* @param simpleMessages the messages to send
49-
* @throws org.springframework.mail.MailParseException
50-
* in case of failure when parsing a message
51-
* @throws org.springframework.mail.MailAuthenticationException
52-
* in case of authentication failure
53-
* @throws org.springframework.mail.MailSendException
54-
* in case of failure when sending a message
46+
* @throws MailParseException in case of failure when parsing a message
47+
* @throws MailAuthenticationException in case of authentication failure
48+
* @throws MailSendException in case of failure when sending a message
5549
*/
56-
void send(SimpleMailMessage[] simpleMessages) throws MailException;
50+
void send(SimpleMailMessage... simpleMessages) throws MailException;
5751

5852
}

spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.mail.javamail;
1818

1919
import java.io.InputStream;
20-
2120
import javax.mail.internet.MimeMessage;
2221

2322
import org.springframework.mail.MailException;
@@ -104,7 +103,7 @@ public interface JavaMailSender extends MailSender {
104103
* in case of failure when sending a message
105104
* @see #createMimeMessage
106105
*/
107-
void send(MimeMessage[] mimeMessages) throws MailException;
106+
void send(MimeMessage... mimeMessages) throws MailException;
108107

109108
/**
110109
* Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
@@ -138,6 +137,6 @@ public interface JavaMailSender extends MailSender {
138137
* @throws org.springframework.mail.MailSendException
139138
* in case of failure when sending a message
140139
*/
141-
void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException;
140+
void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException;
142141

143142
}

spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.mail.javamail;
1818

19-
import java.io.IOException;
2019
import java.io.InputStream;
2120
import java.util.ArrayList;
2221
import java.util.Date;
@@ -294,11 +293,11 @@ public FileTypeMap getDefaultFileTypeMap() {
294293

295294
@Override
296295
public void send(SimpleMailMessage simpleMessage) throws MailException {
297-
send(new SimpleMailMessage[] { simpleMessage });
296+
send(new SimpleMailMessage[] {simpleMessage});
298297
}
299298

300299
@Override
301-
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
300+
public void send(SimpleMailMessage... simpleMessages) throws MailException {
302301
List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(simpleMessages.length);
303302
for (SimpleMailMessage simpleMessage : simpleMessages) {
304303
MimeMailMessage message = new MimeMailMessage(createMimeMessage());
@@ -342,17 +341,17 @@ public void send(MimeMessage mimeMessage) throws MailException {
342341
}
343342

344343
@Override
345-
public void send(MimeMessage[] mimeMessages) throws MailException {
344+
public void send(MimeMessage... mimeMessages) throws MailException {
346345
doSend(mimeMessages, null);
347346
}
348347

349348
@Override
350349
public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException {
351-
send(new MimeMessagePreparator[] { mimeMessagePreparator });
350+
send(new MimeMessagePreparator[] {mimeMessagePreparator});
352351
}
353352

354353
@Override
355-
public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException {
354+
public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
356355
try {
357356
List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(mimeMessagePreparators.length);
358357
for (MimeMessagePreparator preparator : mimeMessagePreparators) {
@@ -368,9 +367,6 @@ public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailExce
368367
catch (MessagingException ex) {
369368
throw new MailParseException(ex);
370369
}
371-
catch (IOException ex) {
372-
throw new MailPreparationException(ex);
373-
}
374370
catch (Exception ex) {
375371
throw new MailPreparationException(ex);
376372
}
@@ -389,12 +385,20 @@ public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailExce
389385
* in case of failure when sending a message
390386
*/
391387
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
392-
Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>();
388+
String username = getUsername();
389+
String password = getPassword();
390+
if ("".equals(username)) { // probably from a placeholder
391+
username = null;
392+
if ("".equals(password)) { // in conjunction with "" username, this means no password to use
393+
password = null;
394+
}
395+
}
393396

397+
Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>();
394398
Transport transport;
395399
try {
396400
transport = getTransport(getSession());
397-
transport.connect(getHost(), getPort(), getUsername(), getPassword());
401+
transport.connect(getHost(), getPort(), username, password);
398402
}
399403
catch (AuthenticationFailedException ex) {
400404
throw new MailAuthenticationException(ex);

0 commit comments

Comments
 (0)