Class JavaMailSenderImpl
- All Implemented Interfaces:
JavaMailSender
,MailSender
JavaMailSender
interface,
supporting both JavaMail MimeMessages
and Spring
SimpleMailMessages
. Can also be used as a
plain MailSender
implementation.
Allows for defining all settings locally as bean properties.
Alternatively, a pre-configured JavaMail Session
can be
specified, possibly pulled from an application server's JNDI environment.
Non-default properties in this object will always override the settings
in the JavaMail Session
. Note that if overriding all values locally,
there is no added value in setting a pre-configured Session
.
- Since:
- 10.09.2003
- Author:
- Dmitriy Kopylenko, Juergen Hoeller
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default port: -1.static final String
The default protocol: 'smtp'. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected Transport
Obtain and connect a Transport from the underlying JavaMail Session, passing in the specified host, port, username, and password.This implementation creates a SmartMimeMessage, holding the specified default encoding and default FileTypeMap.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.protected void
doSend
(MimeMessage[] mimeMessages, Object[] originalMessages) Actually send the given array of MimeMessages via JavaMail.Return the default encoding forMimeMessages
, ornull
if none.getHost()
Return the mail server host.AllowMap
access to the JavaMail properties of this sender, with the option to add or override specific entries.Return the password for the account at the mail host.int
getPort()
Return the mail server port.Return the mail protocol.Return the JavaMailSession
, lazily initializing it if it hasn't been specified explicitly.protected Transport
getTransport
(Session session) Obtain a Transport object from the given JavaMail Session, using the configured protocol.Return the username for the account at the mail host.void
send
(MimeMessage mimeMessage) Send the given JavaMail MIME message.void
send
(MimeMessage... mimeMessages) Send the given array of JavaMail MIME messages in batch.void
send
(MimeMessagePreparator mimeMessagePreparator) Send the JavaMail MIME message prepared by the given MimeMessagePreparator.void
send
(MimeMessagePreparator... mimeMessagePreparators) Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.void
send
(SimpleMailMessage simpleMessage) Send the given simple mail message.void
send
(SimpleMailMessage... simpleMessages) Send the given array of simple mail messages in batch.void
setDefaultEncoding
(String defaultEncoding) Set the default encoding to use forMimeMessages
created by this instance.void
setDefaultFileTypeMap
(FileTypeMap defaultFileTypeMap) Set the default Java ActivationFileTypeMap
to use forMimeMessages
created by this instance.void
Set the mail server host, typically an SMTP host.void
setJavaMailProperties
(Properties javaMailProperties) Set JavaMail properties for theSession
.void
setPassword
(String password) Set the password for the account at the mail host, if any.void
setPort
(int port) Set the mail server port.void
setProtocol
(String protocol) Set the mail protocol.void
setSession
(Session session) Set the JavaMailSession
, possibly pulled from JNDI.void
setUsername
(String username) Set the username for the account at the mail host, if any.void
Validate that this instance can connect to the server that it is configured for.
-
Field Details
-
DEFAULT_PROTOCOL
The default protocol: 'smtp'.- See Also:
-
DEFAULT_PORT
public static final int DEFAULT_PORTThe default port: -1.- See Also:
-
-
Constructor Details
-
JavaMailSenderImpl
public JavaMailSenderImpl()Create a new instance of theJavaMailSenderImpl
class.Initializes the
"defaultFileTypeMap"
property with a defaultConfigurableMimeFileTypeMap
.
-
-
Method Details
-
setJavaMailProperties
Set JavaMail properties for theSession
.A new
Session
will be created with those properties. Use either this method orsetSession(jakarta.mail.Session)
, but not both.Non-default properties in this instance will override given JavaMail properties.
-
getJavaMailProperties
AllowMap
access to the JavaMail properties of this sender, with the option to add or override specific entries.Useful for specifying entries directly, for example via
javaMailProperties[mail.smtp.auth]
. -
setSession
Set the JavaMailSession
, possibly pulled from JNDI.Default is a new
Session
without defaults, that is completely configured via this instance's properties.If using a pre-configured
Session
, non-default properties in this instance will override the settings in theSession
. -
getSession
Return the JavaMailSession
, lazily initializing it if it hasn't been specified explicitly. -
setProtocol
Set the mail protocol. Default is "smtp". -
getProtocol
Return the mail protocol. -
setHost
Set the mail server host, typically an SMTP host.Default is the default host of the underlying JavaMail Session.
-
getHost
Return the mail server host. -
setPort
public void setPort(int port) Set the mail server port.Default is
DEFAULT_PORT
, letting JavaMail use the default SMTP port (25). -
getPort
public int getPort()Return the mail server port. -
setUsername
Set the username for the account at the mail host, if any.Note that the underlying JavaMail
Session
has to be configured with the property"mail.smtp.auth"
set totrue
, else the specified username will not be sent to the mail server by the JavaMail runtime. If you are not explicitly passing in aSession
to use, simply specify this setting viasetJavaMailProperties(java.util.Properties)
. -
getUsername
Return the username for the account at the mail host. -
setPassword
Set the password for the account at the mail host, if any.Note that the underlying JavaMail
Session
has to be configured with the property"mail.smtp.auth"
set totrue
, else the specified password will not be sent to the mail server by the JavaMail runtime. If you are not explicitly passing in aSession
to use, simply specify this setting viasetJavaMailProperties(java.util.Properties)
. -
getPassword
Return the password for the account at the mail host. -
setDefaultEncoding
Set the default encoding to use forMimeMessages
created by this instance.Such an encoding will be auto-detected by
MimeMessageHelper
. -
getDefaultEncoding
Return the default encoding forMimeMessages
, ornull
if none. -
setDefaultFileTypeMap
Set the default Java ActivationFileTypeMap
to use forMimeMessages
created by this instance.A
FileTypeMap
specified here will be autodetected byMimeMessageHelper
, avoiding the need to specify theFileTypeMap
for eachMimeMessageHelper
instance.For example, you can specify a custom instance of Spring's
ConfigurableMimeFileTypeMap
here. If not explicitly specified, a defaultConfigurableMimeFileTypeMap
will be used, containing an extended set of MIME type mappings (as defined by themime.types
file contained in the Spring jar). -
getDefaultFileTypeMap
-
send
Description copied from interface:MailSender
Send the given simple mail message.- Specified by:
send
in interfaceMailSender
- Parameters:
simpleMessage
- the message to send- Throws:
MailParseException
- in case of failure when parsing the messageMailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending the messageMailException
-
send
Description copied from interface:MailSender
Send the given array of simple mail messages in batch.- Specified by:
send
in interfaceMailSender
- Parameters:
simpleMessages
- the messages to send- Throws:
MailParseException
- in case of failure when parsing a messageMailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending a messageMailException
-
createMimeMessage
This implementation creates a SmartMimeMessage, holding the specified default encoding and default FileTypeMap. This special defaults-carrying message will be autodetected byMimeMessageHelper
, which will use the carried encoding and FileTypeMap unless explicitly overridden.- Specified by:
createMimeMessage
in interfaceJavaMailSender
- Returns:
- the new MimeMessage instance
- See Also:
-
createMimeMessage
Description copied from interface:JavaMailSender
Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender, using the given input stream as the message source.- Specified by:
createMimeMessage
in interfaceJavaMailSender
- Parameters:
contentStream
- the raw MIME input stream for the message- Returns:
- the new MimeMessage instance
- Throws:
MailParseException
- in case of message creation failureMailException
-
send
Description copied from interface:JavaMailSender
Send the given JavaMail MIME message. The message needs to have been created withJavaMailSender.createMimeMessage()
.- Specified by:
send
in interfaceJavaMailSender
- Parameters:
mimeMessage
- message to send- Throws:
MailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending the messageMailException
- See Also:
-
send
Description copied from interface:JavaMailSender
Send the given array of JavaMail MIME messages in batch. The messages need to have been created withJavaMailSender.createMimeMessage()
.- Specified by:
send
in interfaceJavaMailSender
- Parameters:
mimeMessages
- messages to send- Throws:
MailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending a messageMailException
- See Also:
-
send
Description copied from interface:JavaMailSender
Send the JavaMail MIME message prepared by the given MimeMessagePreparator.Alternative way to prepare MimeMessage instances, instead of
JavaMailSender.createMimeMessage()
andJavaMailSender.send(MimeMessage)
calls. Takes care of proper exception conversion.- Specified by:
send
in interfaceJavaMailSender
- Parameters:
mimeMessagePreparator
- the preparator to use- Throws:
MailPreparationException
- 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
-
send
Description copied from interface:JavaMailSender
Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.Alternative way to prepare MimeMessage instances, instead of
JavaMailSender.createMimeMessage()
andJavaMailSender.send(MimeMessage[])
calls. Takes care of proper exception conversion.- Specified by:
send
in interfaceJavaMailSender
- Parameters:
mimeMessagePreparators
- the preparator to use- Throws:
MailPreparationException
- 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
-
testConnection
Validate that this instance can connect to the server that it is configured for. Throws aMessagingException
if the connection attempt failed.- Throws:
MessagingException
-
doSend
protected void doSend(MimeMessage[] mimeMessages, @Nullable Object[] originalMessages) throws MailException Actually send the given array of MimeMessages via JavaMail.- Parameters:
mimeMessages
- the MimeMessage objects to sendoriginalMessages
- corresponding original message objects that the MimeMessages have been created from (with same array length and indices as the "mimeMessages" array), if any- Throws:
MailAuthenticationException
- in case of authentication failureMailSendException
- in case of failure when sending a messageMailException
-
connectTransport
Obtain and connect a Transport from the underlying JavaMail Session, passing in the specified host, port, username, and password.- Returns:
- the connected Transport object
- Throws:
MessagingException
- if the connect attempt failed- Since:
- 4.1.2
- See Also:
-
getTransport
Obtain a Transport object from the given JavaMail Session, using the configured protocol.Can be overridden in subclasses, e.g. to return a mock Transport object.
- Throws:
NoSuchProviderException
- See Also:
-