org.codemonkey.simplejavamail
Class MailSession

java.lang.Object
  extended by org.codemonkey.simplejavamail.MailSession

 class MailSession
extends Object

Creates and initializes a Session instance based on provided host and credentials as well as a TransportStrategy instance to apply SMTP, SMTPS or SMTP + TLS protocol semantics.

The Session needs to be created before assembling the concerning e-mail message in the Mailer as the Sun JavaMail API directly wires the e-mail message into the Session. The session again is needed when actually sending the e-mail or when toString() is called.

Author:
Benny Bottema

Field Summary
private  Session session
          Used to actually send the email.
private  TransportStrategy transportStrategy
          The transport protocol strategy enum that actually handles the session configuration.
 
Constructor Summary
MailSession(Session session)
          Default constructor, stores the given mail session for later use.
MailSession(String host, int port, String username, String password, TransportStrategy transportStrategy)
          Overloaded constructor which produces a new Session on the fly.
 
Method Summary
 Session createMailSession(String host, int port, String username, String password)
          Actually instantiates and configures the Session instance.
 Session getSession()
          Bean getter for session.
 void sendMessage(Message message)
          Sends a Sun JavaMail Message object using the Session#getTransport().
 String toString()
          Returns host details, credentials used and whether authentication will take place and finally the transport protocol used.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

session

private final Session session
Used to actually send the email. This session can come from being passed in the default constructor, or made by this MailSession directly, when no Session instance was provided.

See Also:
MailSession(Session), MailSession(String, int, String, String, TransportStrategy)

transportStrategy

private TransportStrategy transportStrategy
The transport protocol strategy enum that actually handles the session configuration. Session configuration meaning setting the right properties for the appropriate transport type (ie. "mail.smtp.host" for SMTP, "mail.smtps.host" for SMTPS).

Constructor Detail

MailSession

public MailSession(Session session)
Default constructor, stores the given mail session for later use. It is assumed all properties are set, such as host, credentials and transport properties.

Parameters:
session - A preconfigured mail Session object with which a Message can be produced.

MailSession

public MailSession(String host,
                   int port,
                   String username,
                   String password,
                   TransportStrategy transportStrategy)
Overloaded constructor which produces a new Session on the fly. Use this if you don't have a mail session configured in your web container, or Spring context etc.

Parameters:
host - The address URL of the SMTP server to be used.
port - The port of the SMTP server.
username - An optional username, may be null.
password - An optional password, may be null, but only if username is null as well.
Method Detail

createMailSession

public Session createMailSession(String host,
                                 int port,
                                 String username,
                                 String password)
Actually instantiates and configures the Session instance. Delegates resolving transport protocol specific properties to the transportStrategy in two ways:
  1. request an initial property list which the strategy may pre-populate
  2. by requesting the property names according to the respective transport protocol it handles (for the host property name it would be "mail.smtp.host" for SMTP and "mail.smtps.host" for SMTPS)

Parameters:
host - The address URL of the SMTP server to be used.
port - The port of the SMTP server.
username - An optional username, may be null.
password - An optional password, may be null.
Returns:
A fully configured Session instance complete with transport protocol settings.
See Also:
TransportStrategy.generateProperties(), TransportStrategy.propertyNameHost(), TransportStrategy.propertyNamePort(), TransportStrategy.propertyNameUsername(), TransportStrategy.propertyNameAuthenticate()

sendMessage

public void sendMessage(Message message)
                 throws MessagingException
Sends a Sun JavaMail Message object using the Session#getTransport(). It will call Transport#connect() assuming all connection details have been configured in the provided Session instance.

Performs a call to Message#saveChanges() as the Sun JavaMail API indicates it is needed to configure the message headers and providing a message id.

Parameters:
message - The message to send. Generate by the Mailer.
Throws:
MessagingException - Thrown by Message#saveChanges(), Transport#connect(), Transport#sendMessage(Message, javax.mail.Address[]), Transport#close()

toString

public String toString()
Returns host details, credentials used and whether authentication will take place and finally the transport protocol used.

Overrides:
toString in class Object

getSession

public Session getSession()
Bean getter for session.



Copyright © 2009 under Apache v2 license.