spring-framework / org.springframework.mail.javamail / JavaMailSenderImpl

JavaMailSenderImpl

open class JavaMailSenderImpl : JavaMailSender

Production implementation of the JavaMailSender interface, supporting both JavaMail MimeMessage and Spring SimpleMailMessage. 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 javax.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.

Author
Dmitriy Kopylenko

Author
Juergen Hoeller

Since
10.09.2003

See Also
javax.mail.internet.MimeMessagejavax.mail.Session#setSession#setJavaMailProperties#setHost#setPort#setUsername#setPassword

Constructors

<init>

JavaMailSenderImpl()

Create a new instance of the JavaMailSenderImpl class.

Initializes the "defaultFileTypeMap" property with a default ConfigurableMimeFileTypeMap.

Properties

DEFAULT_PORT

static val DEFAULT_PORT: Int

The default port: -1

DEFAULT_PROTOCOL

static val DEFAULT_PROTOCOL: String

The default protocol: 'smtp'

Functions

createMimeMessage

open fun createMimeMessage(): MimeMessage

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.

open fun createMimeMessage(contentStream: InputStream): MimeMessage

getDefaultEncoding

open fun getDefaultEncoding(): String

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

getDefaultFileTypeMap

open fun getDefaultFileTypeMap(): FileTypeMap

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

getHost

open fun getHost(): String

Return the mail server host.

getJavaMailProperties

open fun getJavaMailProperties(): Properties

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]".

getPassword

open fun getPassword(): String

Return the password for the account at the mail host.

getPort

open fun getPort(): Int

Return the mail server port.

getProtocol

open fun getProtocol(): String

Return the mail protocol.

getSession

open fun getSession(): Session

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

getUsername

open fun getUsername(): String

Return the username for the account at the mail host.

send

open fun send(simpleMessage: SimpleMailMessage): Unit
open fun send(vararg simpleMessages: SimpleMailMessage): Unit
open fun send(mimeMessage: MimeMessage): Unit
open fun send(vararg mimeMessages: MimeMessage): Unit
open fun send(mimeMessagePreparator: MimeMessagePreparator): Unit
open fun send(vararg mimeMessagePreparators: MimeMessagePreparator): Unit

setDefaultEncoding

open fun setDefaultEncoding(defaultEncoding: String): Unit

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

Such an encoding will be auto-detected by MimeMessageHelper.

setDefaultFileTypeMap

open fun setDefaultFileTypeMap(defaultFileTypeMap: FileTypeMap): Unit

Set the default Java Activation FileTypeMap to use for MimeMessage 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).

setHost

open fun setHost(host: String): Unit

Set the mail server host, typically an SMTP host.

Default is the default host of the underlying JavaMail Session.

setJavaMailProperties

open fun setJavaMailProperties(javaMailProperties: Properties): Unit

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.

setPassword

open fun setPassword(password: String): Unit

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.

setPort

open fun setPort(port: Int): Unit

Set the mail server port.

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

setProtocol

open fun setProtocol(protocol: String): Unit

Set the mail protocol. Default is "smtp".

setSession

open fun setSession(session: Session): Unit

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.

setUsername

open fun setUsername(username: String): Unit

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.

testConnection

open fun testConnection(): Unit

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