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 SummaryFieldsModifier and TypeFieldDescriptionstatic final intThe default port: -1.static final StringThe default protocol: 'smtp'.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected TransportObtain 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 voiddoSend(MimeMessage[] mimeMessages, Object[] originalMessages) Actually send the given array of MimeMessages via JavaMail.Return the default encoding forMimeMessages, ornullif none.getHost()Return the mail server host.AllowMapaccess 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.intgetPort()Return the mail server port.Return the mail protocol.Return the JavaMailSession, lazily initializing it if it hasn't been specified explicitly.protected TransportgetTransport(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.voidsend(MimeMessage... mimeMessages) Send the given array of JavaMail MIME messages in batch.voidsend(SimpleMailMessage... simpleMessages) Send the given array of simple mail messages in batch.voidsetDefaultEncoding(String defaultEncoding) Set the default encoding to use forMimeMessagescreated by this instance.voidsetDefaultFileTypeMap(FileTypeMap defaultFileTypeMap) Set the default Java ActivationFileTypeMapto use forMimeMessagescreated by this instance.voidSet the mail server host, typically an SMTP host.voidsetJavaMailProperties(Properties javaMailProperties) Set JavaMail properties for theSession.voidsetPassword(String password) Set the password for the account at the mail host, if any.voidsetPort(int port) Set the mail server port.voidsetProtocol(String protocol) Set the mail protocol.voidsetSession(Session session) Set the JavaMailSession, possibly pulled from JNDI.voidsetUsername(String username) Set the username for the account at the mail host, if any.voidValidate that this instance can connect to the server that it is configured for.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.mail.javamail.JavaMailSendersend, send, sendMethods inherited from interface org.springframework.mail.MailSendersend
- 
Field Details- 
DEFAULT_PROTOCOLThe default protocol: 'smtp'.- See Also:
 
- 
DEFAULT_PORTpublic static final int DEFAULT_PORTThe default port: -1.- See Also:
 
 
- 
- 
Constructor Details- 
JavaMailSenderImplpublic JavaMailSenderImpl()Create a new instance of theJavaMailSenderImplclass.Initializes the "defaultFileTypeMap"property with a defaultConfigurableMimeFileTypeMap.
 
- 
- 
Method Details- 
setJavaMailPropertiesSet JavaMail properties for theSession.A new Sessionwill 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. 
- 
getJavaMailPropertiesAllowMapaccess 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].
- 
setSessionSet the JavaMailSession, possibly pulled from JNDI.Default is a new Sessionwithout 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.
- 
getSessionReturn the JavaMailSession, lazily initializing it if it hasn't been specified explicitly.
- 
setProtocolSet the mail protocol. Default is "smtp".
- 
getProtocolReturn the mail protocol.
- 
setHostSet the mail server host, typically an SMTP host.Default is the default host of the underlying JavaMail Session. 
- 
getHostReturn the mail server host.
- 
setPortpublic void setPort(int port) Set the mail server port.Default is DEFAULT_PORT, letting JavaMail use the default SMTP port (25).
- 
getPortpublic int getPort()Return the mail server port.
- 
setUsernameSet the username for the account at the mail host, if any.Note that the underlying JavaMail Sessionhas 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 aSessionto use, simply specify this setting viasetJavaMailProperties(java.util.Properties).
- 
getUsernameReturn the username for the account at the mail host.
- 
setPasswordSet the password for the account at the mail host, if any.Note that the underlying JavaMail Sessionhas 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 aSessionto use, simply specify this setting viasetJavaMailProperties(java.util.Properties).
- 
getPasswordReturn the password for the account at the mail host.
- 
setDefaultEncodingSet the default encoding to use forMimeMessagescreated by this instance.Such an encoding will be auto-detected by MimeMessageHelper.
- 
getDefaultEncodingReturn the default encoding forMimeMessages, ornullif none.
- 
setDefaultFileTypeMapSet the default Java ActivationFileTypeMapto use forMimeMessagescreated by this instance.A FileTypeMapspecified here will be autodetected byMimeMessageHelper, avoiding the need to specify theFileTypeMapfor eachMimeMessageHelperinstance.For example, you can specify a custom instance of Spring's ConfigurableMimeFileTypeMaphere. If not explicitly specified, a defaultConfigurableMimeFileTypeMapwill be used, containing an extended set of MIME type mappings (as defined by themime.typesfile contained in the Spring jar).
- 
getDefaultFileTypeMap
- 
sendDescription copied from interface:MailSenderSend the given array of simple mail messages in batch.- Specified by:
- sendin interface- MailSender
- Parameters:
- simpleMessages- the messages to send
- Throws:
- MailParseException- in case of failure when parsing a message
- MailAuthenticationException- in case of authentication failure
- MailSendException- in case of failure when sending a message
- MailException
 
- 
createMimeMessageThis 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:
- createMimeMessagein interface- JavaMailSender
- Returns:
- the new MimeMessage instance
- See Also:
 
- 
createMimeMessageDescription copied from interface:JavaMailSenderCreate a new JavaMail MimeMessage for the underlying JavaMail Session of this sender, using the given input stream as the message source.- Specified by:
- createMimeMessagein interface- JavaMailSender
- Parameters:
- contentStream- the raw MIME input stream for the message
- Returns:
- the new MimeMessage instance
- Throws:
- MailParseException- in case of message creation failure
- MailException
 
- 
sendDescription copied from interface:JavaMailSenderSend the given array of JavaMail MIME messages in batch. The messages need to have been created withJavaMailSender.createMimeMessage().- Specified by:
- sendin interface- JavaMailSender
- Parameters:
- mimeMessages- messages to send
- Throws:
- MailAuthenticationException- in case of authentication failure
- MailSendException- in case of failure when sending a message
- MailException
- See Also:
 
- 
testConnectionValidate that this instance can connect to the server that it is configured for. Throws aMessagingExceptionif the connection attempt failed.- Throws:
- MessagingException
 
- 
doSendprotected void doSend(MimeMessage[] mimeMessages, @Nullable Object[] originalMessages) throws MailException Actually send the given array of MimeMessages via JavaMail.- Parameters:
- mimeMessages- the MimeMessage objects to send
- originalMessages- 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 failure
- MailSendException- in case of failure when sending a message
- MailException
 
- 
connectTransportObtain 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:
 
- 
getTransportObtain 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:
 
 
-