Top Description Fields Constructors Methods
org.springframework.mail.javamail

public Class JavaMailSenderImpl

extends Object
implements JavaMailSender
Class Inheritance
All Implemented Interfaces
org.springframework.mail.javamail.JavaMailSender, org.springframework.mail.MailSender
Imports
java.io.InputStream, java.util.ArrayList, .Date, .LinkedHashMap, .List, .Map, .Properties, jakarta.activation.FileTypeMap, jakarta.mail.Address, .AuthenticationFailedException, .MessagingException, .NoSuchProviderException, .Session, .Transport, jakarta.mail.internet.MimeMessage, org.springframework.lang.Nullable, org.springframework.mail.MailAuthenticationException, .MailException, .MailParseException, .MailSendException, .SimpleMailMessage, org.springframework.util.Assert

Production implementation of the JavaMailSender interface, supporting both JavaMail MimeMessages and Spring SimpleMailMessages. Can also be used as a plain org.springframework.mail.MailSender implementation.

Allows for defining all settings locally as bean properties. Alternatively, a pre-configured JavaMail jakarta.mail.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.

Authors
Dmitriy Kopylenko, Juergen Hoeller
Since
10.09.2003
See Also
jakarta.mail.internet.MimeMessage, jakarta.mail.Session, setSession, setJavaMailProperties, setHost, setPort, setUsername, setPassword

Field Summary

Modifier and TypeField and Description
public static final int
DEFAULT_PORT

The default port: -1.

public static final String
DEFAULT_PROTOCOL

The default protocol: 'smtp'.

private String
private FileTypeMap
private static final String
private String
private Properties
private String
private int
private String
private Session
private String

Constructor Summary

AccessConstructor and Description
public
JavaMailSenderImpl()

Create a new instance of the JavaMailSenderImpl class.

Method Summary

Modifier and TypeMethod and Description
protected Transport

Returns:

the connected Transport object
connectTransport
()

Obtain and connect a Transport from the underlying JavaMail Session, passing in the specified host, port, username, and password.

public MimeMessage
createMimeMessage()

Implements org.springframework.mail.javamail.JavaMailSender.createMimeMessage.

This implementation creates a SmartMimeMessage, holding the specified default encoding and default FileTypeMap.

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

Implements org.springframework.mail.javamail.JavaMailSender.createMimeMessage.

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[]
the MimeMessage objects to send
mimeMessages
,
Object[]
corresponding original message objects that the MimeMessages have been created from (with same array length and indices as the "mimeMessages" array), if any
originalMessages
)

Actually send the given array of MimeMessages via JavaMail.

public String
getDefaultEncoding()

Return the default encoding for MimeMessages, or null if none.

public FileTypeMap
getDefaultFileTypeMap()

Return the default Java Activation FileTypeMap for MimeMessages, or null if none.

public String
getHost()

Return the mail server host.

public Properties
getJavaMailProperties()

Allow Map access to the JavaMail properties of this sender, with the option to add or override specific entries.

public String
getPassword()

Return the password for the account at the mail host.

public int
getPort()

Return the mail server port.

public String
getProtocol()

Return the mail protocol.

public synchronized Session
getSession()

Return the JavaMail Session, 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.

public String
getUsername()

Return the username for the account at the mail host.

public void
send(SimpleMailMessage...
the messages to send
simpleMessages
)

Implements org.springframework.mail.MailSender.send.

Send the given array of simple mail messages in batch.

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

Implements org.springframework.mail.javamail.JavaMailSender.send.

Send the given array of JavaMail MIME messages in batch.

public void
setDefaultEncoding(String defaultEncoding)

Set the default encoding to use for MimeMessages created by this instance.

public void
setDefaultFileTypeMap(FileTypeMap defaultFileTypeMap)

Set the default Java Activation FileTypeMap to use for MimeMessages created by this instance.

public void
setHost(String host)

Set the mail server host, typically an SMTP host.

public void
setJavaMailProperties(Properties javaMailProperties)

Set JavaMail properties for the Session.

public void
setPassword(String password)

Set the password for the account at the mail host, if any.

public void
setPort(int port)

Set the mail server port.

public void
setProtocol(String protocol)

Set the mail protocol.

public synchronized void
setSession(Session session)

Set the JavaMail Session, possibly pulled from JNDI.

public void
setUsername(String username)

Set the username for the account at the mail host, if any.

public void
testConnection()

Validate that this instance can connect to the server that it is configured for.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_PORTback to summary
public static final int DEFAULT_PORT

The default port: -1.

DEFAULT_PROTOCOLback to summary
public static final String DEFAULT_PROTOCOL

The default protocol: 'smtp'.

defaultEncodingback to summary
private String defaultEncoding
Annotations
@Nullable
defaultFileTypeMapback to summary
private FileTypeMap defaultFileTypeMap
Annotations
@Nullable
HEADER_MESSAGE_IDback to summary
private static final String HEADER_MESSAGE_ID
hostback to summary
private String host
Annotations
@Nullable
javaMailPropertiesback to summary
private Properties javaMailProperties
passwordback to summary
private String password
Annotations
@Nullable
portback to summary
private int port
protocolback to summary
private String protocol
Annotations
@Nullable
sessionback to summary
private Session session
Annotations
@Nullable
usernameback to summary
private String username
Annotations
@Nullable

Constructor Detail

JavaMailSenderImplback to summary
public JavaMailSenderImpl()

Create a new instance of the JavaMailSenderImpl class.

Initializes the "defaultFileTypeMap" property with a default ConfigurableMimeFileTypeMap.

Method Detail

connectTransportback to summary
protected Transport connectTransport() throws MessagingException

Obtain and connect a Transport from the underlying JavaMail Session, passing in the specified host, port, username, and password.

Returns:Transport

the connected Transport object

Exceptions
MessagingException:
if the connect attempt failed
Since
4.1.2
See Also
getTransport, getHost(), getPort(), getUsername(), getPassword()
createMimeMessageback to summary
public MimeMessage createMimeMessage()

Implements org.springframework.mail.javamail.JavaMailSender.createMimeMessage.

This implementation creates a SmartMimeMessage, holding the specified default encoding and default FileTypeMap. This special defaults-carrying message will be autodetected by MimeMessageHelper, which will use the carried encoding and FileTypeMap unless explicitly overridden.

Returns:MimeMessage

Doc from org.springframework.mail.javamail.JavaMailSender.createMimeMessage.

the new MimeMessage instance

Annotations
@Override
See Also
setDefaultEncoding, setDefaultFileTypeMap
createMimeMessageback to summary
public MimeMessage createMimeMessage(InputStream contentStream) throws MailException

Implements org.springframework.mail.javamail.JavaMailSender.createMimeMessage.

Doc from org.springframework.mail.javamail.JavaMailSender.createMimeMessage.

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

Annotations
@Override
doSendback to summary
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException

Actually send the given array of MimeMessages via JavaMail.

Parameters
mimeMessages:MimeMessage[]

the MimeMessage objects to send

originalMessages:Object[]

corresponding original message objects that the MimeMessages have been created from (with same array length and indices as the "mimeMessages" array), if any

Exceptions
MailAuthenticationException:
in case of authentication failure
MailSendException:
in case of failure when sending a message
getDefaultEncodingback to summary
public String getDefaultEncoding()

Return the default encoding for MimeMessages, or null if none.

Annotations
@Nullable
getDefaultFileTypeMapback to summary
public FileTypeMap getDefaultFileTypeMap()

Return the default Java Activation FileTypeMap for MimeMessages, or null if none.

Annotations
@Nullable
getHostback to summary
public String getHost()

Return the mail server host.

Annotations
@Nullable
getJavaMailPropertiesback to summary
public Properties getJavaMailProperties()

Allow Map 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].

getPasswordback to summary
public String getPassword()

Return the password for the account at the mail host.

Annotations
@Nullable
getPortback to summary
public int getPort()

Return the mail server port.

getProtocolback to summary
public String getProtocol()

Return the mail protocol.

Annotations
@Nullable
getSessionback to summary
public synchronized Session getSession()

Return the JavaMail Session, lazily initializing it if it hasn't been specified explicitly.

getTransportback to summary
protected Transport getTransport(Session session) throws NoSuchProviderException

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.

See Also
jakarta.mail.Session#getTransport(String), getSession(), getProtocol()
getUsernameback to summary
public String getUsername()

Return the username for the account at the mail host.

Annotations
@Nullable
sendback to summary
public void send(SimpleMailMessage... simpleMessages) throws MailException

Implements org.springframework.mail.MailSender.send.

Doc from org.springframework.mail.MailSender.send.

Send the given array of simple mail messages in batch.

Parameters
simpleMessages:SimpleMailMessage[]

the messages to send

Annotations
@Override
sendback to summary
public void send(MimeMessage... mimeMessages) throws MailException

Implements org.springframework.mail.javamail.JavaMailSender.send.

Doc from org.springframework.mail.javamail.JavaMailSender.send.

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

Annotations
@Override
setDefaultEncodingback to summary
public void setDefaultEncoding(String defaultEncoding)

Set the default encoding to use for MimeMessages created by this instance.

Such an encoding will be auto-detected by MimeMessageHelper.

setDefaultFileTypeMapback to summary
public void setDefaultFileTypeMap(FileTypeMap defaultFileTypeMap)

Set the default Java Activation FileTypeMap to use for MimeMessages created by this instance.

A FileTypeMap specified here will be autodetected by MimeMessageHelper, avoiding the need to specify the FileTypeMap for each MimeMessageHelper instance.

For example, you can specify a custom instance of Spring's ConfigurableMimeFileTypeMap here. If not explicitly specified, a default ConfigurableMimeFileTypeMap will be used, containing an extended set of MIME type mappings (as defined by the mime.types file contained in the Spring jar).

See Also
MimeMessageHelper#setFileTypeMap
setHostback to summary
public void setHost(String host)

Set the mail server host, typically an SMTP host.

Default is the default host of the underlying JavaMail Session.

setJavaMailPropertiesback to summary
public void setJavaMailProperties(Properties javaMailProperties)

Set JavaMail properties for the Session.

A new Session will be created with those properties. Use either this method or setSession, but not both.

Non-default properties in this instance will override given JavaMail properties.

setPasswordback to summary
public void setPassword(String password)

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 to true, else the specified password will not be sent to the mail server by the JavaMail runtime. If you are not explicitly passing in a Session to use, simply specify this setting via setJavaMailProperties.

See Also
setSession, setUsername
setPortback to summary
public void setPort(int port)

Set the mail server port.

Default is DEFAULT_PORT, letting JavaMail use the default SMTP port (25).

setProtocolback to summary
public void setProtocol(String protocol)

Set the mail protocol. Default is "smtp".

setSessionback to summary
public synchronized void setSession(Session session)

Set the JavaMail Session, 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 the Session.

See Also
setJavaMailProperties
setUsernameback to summary
public void setUsername(String username)

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 to true, else the specified username will not be sent to the mail server by the JavaMail runtime. If you are not explicitly passing in a Session to use, simply specify this setting via setJavaMailProperties.

See Also
setSession, setPassword
testConnectionback to summary
public void testConnection() throws MessagingException

Validate that this instance can connect to the server that it is configured for. Throws a MessagingException if the connection attempt failed.