diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailSender.java b/spring-context-support/src/main/java/org/springframework/mail/MailSender.java index 6fd8265f59..3beee8e785 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailSender.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailSender.java @@ -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. diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java index 61f4ecb01d..e964dac0fc 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java @@ -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 { * 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 { * @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 { * @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 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); + } + } } diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java index eb8a48834f..33b23baf72 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java @@ -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 { // Implementation of MailSender //--------------------------------------------------------------------- - @Override - public void send(SimpleMailMessage simpleMessage) throws MailException { - send(new SimpleMailMessage[] {simpleMessage}); - } - @Override public void send(SimpleMailMessage... simpleMessages) throws MailException { List mimeMessages = new ArrayList<>(simpleMessages.length); @@ -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 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.