public interface JavaMailSender extends MailSender
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 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 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
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.
MimeMessage
,
Session
,
JavaMailSenderImpl
,
MimeMessagePreparator
,
MimeMessageHelper
Modifier and Type | Method and Description |
---|---|
MimeMessage |
createMimeMessage()
Create a new JavaMail MimeMessage for the underlying JavaMail Session
of this sender.
|
MimeMessage |
createMimeMessage(InputStream contentStream)
Create a new JavaMail MimeMessage for the underlying JavaMail Session
of this sender, using the given input stream as the message source.
|
void |
send(MimeMessage... mimeMessages)
Send the given array of JavaMail MIME messages in batch.
|
void |
send(MimeMessage mimeMessage)
Send the given JavaMail MIME message.
|
void |
send(MimeMessagePreparator... mimeMessagePreparators)
Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.
|
void |
send(MimeMessagePreparator mimeMessagePreparator)
Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
|
send, send
MimeMessage createMimeMessage()
send(MimeMessage)
,
send(MimeMessage[])
MimeMessage createMimeMessage(InputStream contentStream) throws MailException
contentStream
- the raw MIME input stream for the messageMailParseException
- in case of message creation failureMailException
void send(MimeMessage mimeMessage) throws MailException
createMimeMessage()
.mimeMessage
- message to sendMailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending the messageMailException
createMimeMessage()
void send(MimeMessage... mimeMessages) throws MailException
createMimeMessage()
.mimeMessages
- messages to sendMailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending a messageMailException
createMimeMessage()
void send(MimeMessagePreparator mimeMessagePreparator) throws MailException
Alternative way to prepare MimeMessage instances, instead of
createMimeMessage()
and send(MimeMessage)
calls.
Takes care of proper exception conversion.
mimeMessagePreparator
- the preparator to useMailPreparationException
- in case of failure when preparing the messageMailParseException
- in case of failure when parsing the messageMailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending the messageMailException
void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException
Alternative way to prepare MimeMessage instances, instead of
createMimeMessage()
and send(MimeMessage[])
calls.
Takes care of proper exception conversion.
mimeMessagePreparators
- the preparator to useMailPreparationException
- in case of failure when preparing a messageMailParseException
- in case of failure when parsing a messageMailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending a messageMailException