Browse Source

Add default methods to MailSender and JavaMailSender when appropriate

send(..) -methods of JavaMailSenderImpl which is only delegating to
other methods are pulled up as default methods in the interfaces
JavaMailSender and MailSender, to make these interfaces require fewer
methods to implement.

See gh-23651
pull/31244/head
Rune Flobakk 5 years ago committed by Stéphane Nicoll
parent
commit
f67fad4c14
  1. 4
      spring-context-support/src/main/java/org/springframework/mail/MailSender.java
  2. 34
      spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java
  3. 38
      spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java

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

@ -38,7 +38,9 @@ public interface MailSender { @@ -38,7 +38,9 @@ public interface MailSender {
* @throws MailAuthenticationException in case of authentication failure
* @throws MailSendException in case of failure when sending the message
*/
void send(SimpleMailMessage simpleMessage) throws MailException;
default void send(SimpleMailMessage simpleMessage) throws MailException {
send(new SimpleMailMessage[] {simpleMessage});
}
/**
* Send the given array of simple mail messages in batch.

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

@ -17,10 +17,15 @@ @@ -17,10 +17,15 @@
package org.springframework.mail.javamail;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.MailParseException;
import org.springframework.mail.MailPreparationException;
import org.springframework.mail.MailSender;
/**
@ -92,7 +97,9 @@ public interface JavaMailSender extends MailSender { @@ -92,7 +97,9 @@ public interface JavaMailSender extends MailSender {
* in case of failure when sending the message
* @see #createMimeMessage
*/
void send(MimeMessage mimeMessage) throws MailException;
default void send(MimeMessage mimeMessage) throws MailException {
send(new MimeMessage[] {mimeMessage});
}
/**
* Send the given array of JavaMail MIME messages in batch.
@ -121,7 +128,9 @@ public interface JavaMailSender extends MailSender { @@ -121,7 +128,9 @@ public interface JavaMailSender extends MailSender {
* @throws org.springframework.mail.MailSendException
* in case of failure when sending the message
*/
void send(MimeMessagePreparator mimeMessagePreparator) throws MailException;
default void send(MimeMessagePreparator mimeMessagePreparator) throws MailException {
send(new MimeMessagePreparator[] {mimeMessagePreparator});
}
/**
* Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.
@ -138,6 +147,25 @@ public interface JavaMailSender extends MailSender { @@ -138,6 +147,25 @@ public interface JavaMailSender extends MailSender {
* @throws org.springframework.mail.MailSendException
* in case of failure when sending a message
*/
void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException;
default void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
try {
List<MimeMessage> mimeMessages = new ArrayList<>(mimeMessagePreparators.length);
for (MimeMessagePreparator preparator : mimeMessagePreparators) {
MimeMessage mimeMessage = createMimeMessage();
preparator.prepare(mimeMessage);
mimeMessages.add(mimeMessage);
}
send(mimeMessages.toArray(new MimeMessage[0]));
}
catch (MailException ex) {
throw ex;
}
catch (MessagingException ex) {
throw new MailParseException(ex);
}
catch (Exception ex) {
throw new MailPreparationException(ex);
}
}
}

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

@ -37,7 +37,6 @@ import org.springframework.lang.Nullable; @@ -37,7 +37,6 @@ import org.springframework.lang.Nullable;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.MailException;
import org.springframework.mail.MailParseException;
import org.springframework.mail.MailPreparationException;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.util.Assert;
@ -307,11 +306,6 @@ public class JavaMailSenderImpl implements JavaMailSender { @@ -307,11 +306,6 @@ public class JavaMailSenderImpl implements JavaMailSender {
// Implementation of MailSender
//---------------------------------------------------------------------
@Override
public void send(SimpleMailMessage simpleMessage) throws MailException {
send(new SimpleMailMessage[] {simpleMessage});
}
@Override
public void send(SimpleMailMessage... simpleMessages) throws MailException {
List<MimeMessage> mimeMessages = new ArrayList<>(simpleMessages.length);
@ -351,43 +345,11 @@ public class JavaMailSenderImpl implements JavaMailSender { @@ -351,43 +345,11 @@ public class JavaMailSenderImpl implements JavaMailSender {
}
}
@Override
public void send(MimeMessage mimeMessage) throws MailException {
send(new MimeMessage[] {mimeMessage});
}
@Override
public void send(MimeMessage... mimeMessages) throws MailException {
doSend(mimeMessages, null);
}
@Override
public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException {
send(new MimeMessagePreparator[] {mimeMessagePreparator});
}
@Override
public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
try {
List<MimeMessage> mimeMessages = new ArrayList<>(mimeMessagePreparators.length);
for (MimeMessagePreparator preparator : mimeMessagePreparators) {
MimeMessage mimeMessage = createMimeMessage();
preparator.prepare(mimeMessage);
mimeMessages.add(mimeMessage);
}
send(mimeMessages.toArray(new MimeMessage[0]));
}
catch (MailException ex) {
throw ex;
}
catch (MessagingException ex) {
throw new MailParseException(ex);
}
catch (Exception ex) {
throw new MailPreparationException(ex);
}
}
/**
* Validate that this instance can connect to the server that it is configured
* for. Throws a {@link MessagingException} if the connection attempt failed.

Loading…
Cancel
Save