Merge pull request #23651 from runeflobakk
* pr/23651: Polish contribution Add default methods to MailSender and JavaMailSender when appropriate Closes gh-23651
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -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.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,10 +17,15 @@
|
||||
package org.springframework.mail.javamail;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.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<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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<MimeMessage> 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<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.
|
||||
|
||||
Reference in New Issue
Block a user