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
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
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;
import org.springframework.mail.SimpleMailMessage;

/**
* Extended {@link org.springframework.mail.MailSender} interface for JavaMail,
Expand Down Expand Up @@ -82,6 +88,26 @@ public interface JavaMailSender extends MailSender {
*/
MimeMessage createMimeMessage(InputStream contentStream) throws MailException;


//---------------------------------------------------------------------
// Implementation of MailSender
//---------------------------------------------------------------------

@Override
default void send(SimpleMailMessage... simpleMessages) throws MailException {
List<MimeMessage> mimeMessages = new ArrayList<>(simpleMessages.length);
for (SimpleMailMessage simpleMessage : simpleMessages) {
MimeMailMessage message = new MimeMailMessage(createMimeMessage());
simpleMessage.copyTo(message);
MimeMessage mimeMessage = message.getMimeMessage();
if (mimeMessage instanceof SmartMimeMessage) {
((SmartMimeMessage) mimeMessage).setOriginalSimpleMailMessage(simpleMessage);
}
mimeMessages.add(mimeMessage);
}
send(mimeMessages.toArray(new MimeMessage[0]));
}

/**
* Send the given JavaMail MIME message.
* The message needs to have been created with {@link #createMimeMessage()}.
Expand All @@ -92,7 +118,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.
Expand Down Expand Up @@ -121,7 +149,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.
Expand All @@ -138,6 +168,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
@@ -0,0 +1,88 @@
/*
* Copyright 2002-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.mail.javamail;

import java.io.InputStream;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;


/**
* A decorator pattern facility for a {@link JavaMailSender}, where the sending
* operation can be customized (e.g. implement a whitelist, feature-toggle, etc)
* by overriding {@link JavaMailSenderDecorator#send(MimeMessage...)}.
* Any deriving class <em>may</em> also override the methods to
* {@link #createMimeMessage() create messages}, if applicable, but this is not
* a requirement.
*
* <h2>Implementation note</h2>
* When overriding {@link #send(MimeMessage...)}, the decorated
* {@code JavaMailSender} can be accessed as {@code super.}{@link #decoratedMailSender}.
*
* @author Rune Flobakk
*/
public abstract class JavaMailSenderDecorator implements JavaMailSender {

/**
* The decorated {@link JavaMailSender} which may (or not) be delegated to
* by {@link #send(MimeMessage...)}.
*/
protected final JavaMailSender decoratedMailSender;

public JavaMailSenderDecorator(JavaMailSender decoratedMailSender) {
this.decoratedMailSender = decoratedMailSender;
}

@Override
public MimeMessage createMimeMessage() {
return this.decoratedMailSender.createMimeMessage();
}

@Override
public MimeMessage createMimeMessage(InputStream contentStream) throws MailException {
return this.decoratedMailSender.createMimeMessage(contentStream);
}

@Override
public final void send(SimpleMailMessage simpleMessage) throws MailException {
JavaMailSender.super.send(simpleMessage);
}

@Override
public final void send(SimpleMailMessage... simpleMessages) throws MailException {
JavaMailSender.super.send(simpleMessages);
}

@Override
public final void send(MimeMessage mimeMessage) throws MailException {
JavaMailSender.super.send(mimeMessage);
}

@Override
public final void send(MimeMessagePreparator mimeMessagePreparator) throws MailException {
JavaMailSender.super.send(mimeMessagePreparator);
}

@Override
public final void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
JavaMailSender.super.send(mimeMessagePreparators);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package org.springframework.mail.javamail;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

Expand All @@ -37,7 +35,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;
Expand Down Expand Up @@ -303,27 +300,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);
for (SimpleMailMessage simpleMessage : simpleMessages) {
MimeMailMessage message = new MimeMailMessage(createMimeMessage());
simpleMessage.copyTo(message);
mimeMessages.add(message.getMimeMessage());
}
doSend(mimeMessages.toArray(new MimeMessage[0]), simpleMessages);
}


//---------------------------------------------------------------------
// Implementation of JavaMailSender
//---------------------------------------------------------------------
Expand Down Expand Up @@ -351,43 +327,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.
Expand Down Expand Up @@ -442,7 +386,7 @@ protected void doSend(MimeMessage[] mimeMessages, @Nullable Object[] originalMes
catch (Exception ex) {
// Effectively, all remaining messages failed...
for (int j = i; j < mimeMessages.length; j++) {
Object original = (originalMessages != null ? originalMessages[j] : mimeMessages[j]);
Object original = (originalMessages != null ? originalMessages[j] : resolveOriginal(mimeMessages[j]));
failedMessages.put(original, ex);
}
throw new MailSendException("Mail server connection failed", ex, failedMessages);
Expand All @@ -465,7 +409,7 @@ protected void doSend(MimeMessage[] mimeMessages, @Nullable Object[] originalMes
transport.sendMessage(mimeMessage, (addresses != null ? addresses : new Address[0]));
}
catch (Exception ex) {
Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
Object original = (originalMessages != null ? originalMessages[i] : resolveOriginal(mimeMessage));
failedMessages.put(original, ex);
}
}
Expand All @@ -492,6 +436,10 @@ protected void doSend(MimeMessage[] mimeMessages, @Nullable Object[] originalMes
}
}

private static Object resolveOriginal(MimeMessage mimeMessage) {
return mimeMessage instanceof SmartMimeMessage ? ((SmartMimeMessage) mimeMessage).getOriginalOrElseThis() : mimeMessage;
}

/**
* Obtain and connect a Transport from the underlying JavaMail Session,
* passing in the specified host, port, username, and password.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.mail.internet.MimeMessage;

import org.springframework.lang.Nullable;
import org.springframework.mail.SimpleMailMessage;

/**
* Special subclass of the standard JavaMail {@link MimeMessage}, carrying a
Expand All @@ -45,6 +46,9 @@ class SmartMimeMessage extends MimeMessage {
@Nullable
private final FileTypeMap defaultFileTypeMap;

@Nullable
private SimpleMailMessage originalMessage;


/**
* Create a new SmartMimeMessage.
Expand All @@ -60,6 +64,13 @@ public SmartMimeMessage(
this.defaultFileTypeMap = defaultFileTypeMap;
}

void setOriginalSimpleMailMessage(@Nullable SimpleMailMessage originalMessage) {
this.originalMessage = originalMessage;
}

Object getOriginalOrElseThis() {
return this.originalMessage != null ? this.originalMessage : this;
}

/**
* Return the default encoding of this message, or {@code null} if none.
Expand Down
Loading