Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default methods to MailSender and JavaMailSender as appropriate #23651

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Pull methods up to default methods
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 less
methods to implement.
runeflobakk committed Sep 17, 2019
commit a820753749c8c4d3ca6519008e53fc938464cd7a
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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 javax.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);
}
}

}
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
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 FileTypeMap getDefaultFileTypeMap() {
// 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 MimeMessage createMimeMessage(InputStream contentStream) throws MailExcep
}
}

@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.