org.springframework.mail.javamail
Class JavaMailSenderImpl

java.lang.Object
  extended by org.springframework.mail.javamail.JavaMailSenderImpl
All Implemented Interfaces:
JavaMailSender, MailSender

public class JavaMailSenderImpl
extends java.lang.Object
implements JavaMailSender

Production implementation of the 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 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.

Since:
10.09.2003
Author:
Dmitriy Kopylenko, Juergen Hoeller
See Also:
javax.mail.internet.MimeMessage, javax.mail.Session, setSession(Session), setJavaMailProperties(java.util.Properties), setHost(java.lang.String), setPort(int), setUsername(java.lang.String), setPassword(java.lang.String)

Field Summary
static int DEFAULT_PORT
          The default port: -1
static java.lang.String DEFAULT_PROTOCOL
          The default protocol: 'smtp'
private  java.lang.String defaultEncoding
           
private  javax.activation.FileTypeMap defaultFileTypeMap
           
private static java.lang.String HEADER_MESSAGE_ID
           
private  java.lang.String host
           
private  java.util.Properties javaMailProperties
           
private  java.lang.String password
           
private  int port
           
private  java.lang.String protocol
           
private  Session session
           
private  java.lang.String username
           
 
Constructor Summary
JavaMailSenderImpl()
          Create a new instance of the JavaMailSenderImpl class.
 
Method Summary
 MimeMessage createMimeMessage()
          This implementation creates a SmartMimeMessage, holding the specified default encoding and default FileTypeMap.
 MimeMessage createMimeMessage(java.io.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, java.lang.Object[] originalMessages)
          Actually send the given array of MimeMessages via JavaMail.
 java.lang.String getDefaultEncoding()
          Return the default encoding for MimeMessages, or null if none.
 javax.activation.FileTypeMap getDefaultFileTypeMap()
          Return the default Java Activation FileTypeMap for MimeMessages, or null if none.
 java.lang.String getHost()
          Return the mail server host.
 java.util.Properties getJavaMailProperties()
          Allow Map access to the JavaMail properties of this sender, with the option to add or override specific entries.
 java.lang.String getPassword()
          Return the password for the account at the mail host.
 int getPort()
          Return the mail server port.
 java.lang.String getProtocol()
          Return the mail protocol.
 Session getSession()
          Return the JavaMail Session, lazily initializing it if hasn't been specified explicitly.
protected  Transport getTransport(Session session)
          Obtain a Transport object from the given JavaMail Session, using the configured protocol.
 java.lang.String getUsername()
          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 JavaMail MIME message.
 void send(SimpleMailMessage simpleMessage)
          Send the given JavaMail MIME message.
 void send(SimpleMailMessage[] simpleMessages)
          Send the given JavaMail MIME message.
 void setDefaultEncoding(java.lang.String defaultEncoding)
          Set the default encoding to use for MimeMessages created by this instance.
 void setDefaultFileTypeMap(javax.activation.FileTypeMap defaultFileTypeMap)
          Set the default Java Activation FileTypeMap to use for MimeMessages created by this instance.
 void setHost(java.lang.String host)
          Set the mail server host, typically an SMTP host.
 void setJavaMailProperties(java.util.Properties javaMailProperties)
          Set JavaMail properties for the Session.
 void setPassword(java.lang.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(java.lang.String protocol)
          Set the mail protocol.
 void setSession(Session session)
          Set the JavaMail Session, possibly pulled from JNDI.
 void setUsername(java.lang.String username)
          Set the username for the account at the mail host, if any.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PROTOCOL

public static final java.lang.String DEFAULT_PROTOCOL
The default protocol: 'smtp'

See Also:
Constant Field Values

DEFAULT_PORT

public static final int DEFAULT_PORT
The default port: -1

See Also:
Constant Field Values

HEADER_MESSAGE_ID

private static final java.lang.String HEADER_MESSAGE_ID
See Also:
Constant Field Values

javaMailProperties

private java.util.Properties javaMailProperties

session

private Session session

protocol

private java.lang.String protocol

host

private java.lang.String host

port

private int port

username

private java.lang.String username

password

private java.lang.String password

defaultEncoding

private java.lang.String defaultEncoding

defaultFileTypeMap

private javax.activation.FileTypeMap defaultFileTypeMap
Constructor Detail

JavaMailSenderImpl

public JavaMailSenderImpl()
Create a new instance of the JavaMailSenderImpl class.

Initializes the "defaultFileTypeMap" property with a default ConfigurableMimeFileTypeMap.

Method Detail

setJavaMailProperties

public void setJavaMailProperties(java.util.Properties javaMailProperties)
Set JavaMail properties for the Session.

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

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


getJavaMailProperties

public java.util.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]".


setSession

public 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(java.util.Properties)

getSession

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


setProtocol

public void setProtocol(java.lang.String protocol)
Set the mail protocol. Default is "smtp".


getProtocol

public java.lang.String getProtocol()
Return the mail protocol.


setHost

public void setHost(java.lang.String host)
Set the mail server host, typically an SMTP host.

Default is the default host of the underlying JavaMail Session.


getHost

public java.lang.String 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

public void setUsername(java.lang.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(java.util.Properties).

See Also:
setSession(Session), setPassword(java.lang.String)

getUsername

public java.lang.String getUsername()
Return the username for the account at the mail host.


setPassword

public void setPassword(java.lang.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(java.util.Properties).

See Also:
setSession(Session), setUsername(java.lang.String)

getPassword

public java.lang.String getPassword()
Return the password for the account at the mail host.


setDefaultEncoding

public void setDefaultEncoding(java.lang.String defaultEncoding)
Set the default encoding to use for MimeMessages created by this instance.

Such an encoding will be auto-detected by MimeMessageHelper.


getDefaultEncoding

public java.lang.String getDefaultEncoding()
Return the default encoding for MimeMessages, or null if none.


setDefaultFileTypeMap

public void setDefaultFileTypeMap(javax.activation.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(javax.activation.FileTypeMap)

getDefaultFileTypeMap

public javax.activation.FileTypeMap getDefaultFileTypeMap()
Return the default Java Activation FileTypeMap for MimeMessages, or null if none.


send

public void send(SimpleMailMessage simpleMessage)
          throws MailException
Description copied from interface: JavaMailSender
Send the given JavaMail MIME message. The message needs to have been created with JavaMailSender.createMimeMessage().

Specified by:
send in interface JavaMailSender
Specified by:
send in interface MailSender
Parameters:
simpleMessage - message to send
Throws:
MailParseException - in case of failure when parsing the message
MailAuthenticationException - in case of authentication failure
MailSendException - in case of failure when sending the message
MailException
See Also:
JavaMailSender.createMimeMessage()

send

public void send(SimpleMailMessage[] simpleMessages)
          throws MailException
Description copied from interface: JavaMailSender
Send the given JavaMail MIME message. The message needs to have been created with JavaMailSender.createMimeMessage().

Specified by:
send in interface JavaMailSender
Specified by:
send in interface MailSender
Parameters:
simpleMessages - message 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
See Also:
JavaMailSender.createMimeMessage()

createMimeMessage

public MimeMessage 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.

Specified by:
createMimeMessage in interface JavaMailSender
Returns:
the new MimeMessage instance
See Also:
setDefaultEncoding(java.lang.String), setDefaultFileTypeMap(javax.activation.FileTypeMap)

createMimeMessage

public MimeMessage createMimeMessage(java.io.InputStream contentStream)
                              throws MailException
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 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

send

public void send(MimeMessage mimeMessage)
          throws MailException
Description copied from interface: JavaMailSender
Send the given JavaMail MIME message. The message needs to have been created with JavaMailSender.createMimeMessage().

Specified by:
send in interface JavaMailSender
Specified by:
send in interface MailSender
Parameters:
mimeMessage - message to send
Throws:
MailParseException - in case of failure when parsing the message
MailAuthenticationException - in case of authentication failure
MailSendException - in case of failure when sending the message
MailException
See Also:
JavaMailSender.createMimeMessage()

send

public void send(MimeMessage[] mimeMessages)
          throws MailException
Description copied from interface: JavaMailSender
Send the given JavaMail MIME message. The message needs to have been created with JavaMailSender.createMimeMessage().

Specified by:
send in interface JavaMailSender
Specified by:
send in interface MailSender
Parameters:
mimeMessages - message 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
See Also:
JavaMailSender.createMimeMessage()

doSend

protected void doSend(MimeMessage[] mimeMessages,
                      java.lang.Object[] originalMessages)
               throws MailException
Actually send the given array of MimeMessages via JavaMail.

Parameters:
mimeMessages - 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

getTransport

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.

Throws:
NoSuchProviderException
See Also:
javax.mail.Session#getTransport(String), getProtocol()