Top Description Methods
org.springframework.mail.javamail

public Interface JavaMailSender

extends MailSender
Known Direct Implementers
org.springframework.mail.javamail.JavaMailSenderImpl
Imports
java.io.InputStream, java.util.ArrayList, .List, jakarta.mail.MessagingException, jakarta.mail.internet.MimeMessage, org.springframework.mail.MailException, .MailParseException, .MailPreparationException, .MailSender

Extended org.springframework.mail.MailSender interface for JavaMail, supporting MIME messages both as direct arguments and through preparation callbacks. Typically used in conjunction with the MimeMessageHelper class for convenient creation of JavaMail MimeMessages, including attachments etc.

Clients should talk to the mail sender through this interface if they need mail functionality beyond org.springframework.mail.SimpleMailMessage. The production implementation is JavaMailSenderImpl; for testing, mocks can be created based on this interface. Clients will typically receive the JavaMailSender reference through dependency injection.

The recommended way of using this interface is the MimeMessagePreparator mechanism, possibly using a MimeMessageHelper for populating the message. See MimeMessageHelper's javadoc for an example.

The entire JavaMail jakarta.mail.Session management is abstracted by the JavaMailSender. Client code should not deal with a Session in any way, rather leave the entire JavaMail configuration and resource handling to the JavaMailSender implementation. This also increases testability.

A JavaMailSender client is not as easy to test as a plain org.springframework.mail.MailSender client, but still straightforward compared to traditional JavaMail code: Just let createMimeMessage() return a plain MimeMessage created with a Session.getInstance(new Properties()) call, and check the passed-in messages in your mock implementations of the various send methods.

Author
Juergen Hoeller
Since
07.10.2003
See Also
jakarta.mail.internet.MimeMessage, jakarta.mail.Session, JavaMailSenderImpl, MimeMessagePreparator, MimeMessageHelper

Method Summary

Modifier and TypeMethod and Description
public MimeMessage

Returns:

the new MimeMessage instance
createMimeMessage
()

Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender.

public MimeMessage

Returns:

the new MimeMessage instance
createMimeMessage
(InputStream
the raw MIME input stream for the message
contentStream
)

Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender, using the given input stream as the message source.

public default void
send(MimeMessage
message to send
mimeMessage
)

Send the given JavaMail MIME message.

public void
send(MimeMessage...
messages to send
mimeMessages
)

Send the given array of JavaMail MIME messages in batch.

public default void
send(MimeMessagePreparator
the preparator to use
mimeMessagePreparator
)

Send the JavaMail MIME message prepared by the given MimeMessagePreparator.

public default void
send(MimeMessagePreparator...
the preparator to use
mimeMessagePreparators
)

Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.

Inherited from org.springframework.mail.MailSender:
sendsend

Method Detail

createMimeMessageback to summary
public MimeMessage createMimeMessage()

Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender. Needs to be called to create MimeMessage instances that can be prepared by the client and passed to send(MimeMessage).

Returns:MimeMessage

the new MimeMessage instance

See Also
send(MimeMessage), send(MimeMessage[])
createMimeMessageback to summary
public MimeMessage createMimeMessage(InputStream contentStream) throws MailException

Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender, using the given input stream as the message source.

Parameters
contentStream:InputStream

the raw MIME input stream for the message

Returns:MimeMessage

the new MimeMessage instance

Exceptions
MailParseException:
in case of message creation failure
sendback to summary
public default void send(MimeMessage mimeMessage) throws MailException

Send the given JavaMail MIME message. The message needs to have been created with createMimeMessage().

Parameters
mimeMessage:MimeMessage

message to send

Exceptions
MailAuthenticationException:
in case of authentication failure
MailSendException:
in case of failure when sending the message
See Also
createMimeMessage
sendback to summary
public void send(MimeMessage... mimeMessages) throws MailException

Send the given array of JavaMail MIME messages in batch. The messages need to have been created with createMimeMessage().

Parameters
mimeMessages:MimeMessage[]

messages to send

Exceptions
MailAuthenticationException:
in case of authentication failure
MailSendException:
in case of failure when sending a message
See Also
createMimeMessage
sendback to summary
public default void send(MimeMessagePreparator mimeMessagePreparator) throws MailException

Send the JavaMail MIME message prepared by the given MimeMessagePreparator.

Alternative way to prepare MimeMessage instances, instead of createMimeMessage() and send(MimeMessage) calls. Takes care of proper exception conversion.

Parameters
mimeMessagePreparator:MimeMessagePreparator

the preparator to use

Exceptions
MailPreparationException:
in case of failure when preparing the message
MailParseException:
in case of failure when parsing the message
MailAuthenticationException:
in case of authentication failure
MailSendException:
in case of failure when sending the message
sendback to summary
public default void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException

Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.

Alternative way to prepare MimeMessage instances, instead of createMimeMessage() and send(MimeMessage[]) calls. Takes care of proper exception conversion.

Parameters
mimeMessagePreparators:MimeMessagePreparator[]

the preparator to use

Exceptions
MailPreparationException:
in case of failure when preparing a message
MailParseException:
in case of failure when parsing a message
MailAuthenticationException:
in case of authentication failure
MailSendException:
in case of failure when sending a message