javax.mail
Class Session

java.lang.Object
  |
  +--javax.mail.Session

public final class Session
extends java.lang.Object

The Session class represents a mail session and is not subclassed. It collects together properties and defaults used by the mail API's. A single default session can be shared by multiple applications on the desktop. Unshared sessions can also be created.


Method Summary
 boolean getDebug()
          Get the debug setting for this Session.
static Session getDefaultInstance(java.util.Properties props, Authenticator authenticator)
          Get the default Session object.
 Folder getFolder(URLName url)
          Get a closed Folder object for the given URLName.
static Session getInstance(java.util.Properties props, Authenticator authenticator)
          Get a new Session object.
 PasswordAuthentication getPasswordAuthentication(URLName url)
          Return any saved PasswordAuthentication for this (store or transport) URLName.
 java.util.Properties getProperties()
          Returns the Properties object associated with this Session
 java.lang.String getProperty(java.lang.String name)
          Returns the value of the specified property.
 Provider getProvider(java.lang.String protocol)
          Returns the default Provider for the protocol specified.
 Provider[] getProviders()
          This method returns an array of all the implementations installed via the javamail.[default.]providers files that can be loaded using the ClassLoader available to this application.
 Store getStore()
          Get a Store object that implements this user's desired Store protcol.
 Store getStore(Provider provider)
          Get an instance of the store specified by Provider.
 Store getStore(java.lang.String protocol)
          Get a Store object that implements the specified protocol.
 Store getStore(URLName url)
          Get a Store object for the given URLName.
 Transport getTransport()
          Get a Transport object that implements this user's desired Transport protcol.
 Transport getTransport(Address address)
          Get a Transport object that can transport a Message to the specified address type.
 Transport getTransport(Provider provider)
          Get an instance of the transport specified in the Provider.
 Transport getTransport(java.lang.String protocol)
          Get a Transport object that implements the specified protocol.
 Transport getTransport(URLName url)
          Get a Transport object for the given URLName.
 PasswordAuthentication requestPasswordAuthentication(java.net.InetAddress addr, int port, java.lang.String protocol, java.lang.String prompt, java.lang.String defaultUserName)
          Call back to the application to get the needed user name and password.
 void setDebug(boolean debug)
          Set the debug setting for this Session.
 void setPasswordAuthentication(URLName url, PasswordAuthentication pw)
          Save a PasswordAuthentication for this (store or transport) URLName.
 void setProvider(Provider provider)
          Set the passed Provider to be the default implementation for the protocol in Provider.protocol overriding any previous values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static Session getInstance(java.util.Properties props,
                                  Authenticator authenticator)
Get a new Session object.
Parameters:
props - Properties object that hold relevant properties.
It is expected that the client supplies values for the properties listed in Appendix A of the JavaMail spec (particularly mail.store.protocol, mail.transport.protocol, mail.host, mail.user, and mail.from) as the defaults are unlikely to work in all cases.
authenticator - Authenticator object used to call back to the application when a user name and password is needed.
Returns:
a new Session object
See Also:
Authenticator

getDefaultInstance

public static Session getDefaultInstance(java.util.Properties props,
                                         Authenticator authenticator)
Get the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default.

Since the default session is potentially available to all code executing in the same Java virtual machine, and the session can contain security sensitive information such as user names and passwords, access to the default session is restricted. The Authenticator object, which must be created by the caller, is used indirectly to check access permission. The Authenticator object passed in when the session is created is compared with the Authenticator object passed in to subsequent requests to get the default session. If both objects are the same, or are from the same ClassLoader, the request is allowed. Otherwise, it is denied.

Note that if the Authenticator object used to create the session is null, anyone can get the default session by passing in null.

In JDK 1.2, additional security Permission objects may be used to control access to the default session.

Parameters:
props - Properties object. Used only if a new Session object is created.
It is expected that the client supplies values for the properties listed in Appendix A of the JavaMail spec (particularly mail.store.protocol, mail.transport.protocol, mail.host, mail.user, and mail.from) as the defaults are unlikely to work in all cases.
authenticator - Authenticator object. Used only if a new Session object is created. Otherwise, it must match the Authenticator used to create the Session.
Returns:
the default Session object

setDebug

public void setDebug(boolean debug)
Set the debug setting for this Session.

Since debug can be turned on only after the Session has been created, to turn on debugging in the Session constructor, set the property mail.debug to true. Subsequent calls to get/setDebug() have no affect on and override the mail.debug property.

Parameters:
debug - Debug setting

getDebug

public boolean getDebug()
Get the debug setting for this Session.

Since debug can be turned on only after the Session has been created, to turn on debugging in the Session constructor, set the property mail.debug to true. Subsequent calls to get/setDebug() have no affect on and override the mail.debug property.

Returns:
current debug setting

getProviders

public Provider[] getProviders()
This method returns an array of all the implementations installed via the javamail.[default.]providers files that can be loaded using the ClassLoader available to this application.
Returns:
Array of configured providers

getProvider

public Provider getProvider(java.lang.String protocol)
                     throws NoSuchProviderException
Returns the default Provider for the protocol specified. Checks mail.<protocol>.class property first and if it exists, returns the Provider associated with this implementation. If it doesn't exist, returns the Provider that appeared first in the configuration files. If an implementation for the protocol isn't found, throws NoSuchProviderException
Parameters:
protocol - Configured protocol (i.e. smtp, imap, etc)
Returns:
Currently configured Provider for the specified protocol
Throws:
NoSuchProviderException - If a provider for the given protocol is not found.

setProvider

public void setProvider(Provider provider)
                 throws NoSuchProviderException
Set the passed Provider to be the default implementation for the protocol in Provider.protocol overriding any previous values.
Parameters:
provider - Currently configured Provider which will be set as the default for the protocol
Throws:
NoSuchProviderException - If the provider passed in is invalid.

getStore

public Store getStore()
               throws NoSuchProviderException
Get a Store object that implements this user's desired Store protcol. The mail.store.protocol property specifies the desired protocol. If an appropriate Store object is not obtained, NoSuchProviderException is thrown
Returns:
a Store object
Throws:
NoSuchProviderException - If a provider for the given protocol is not found.

getStore

public Store getStore(java.lang.String protocol)
               throws NoSuchProviderException
Get a Store object that implements the specified protocol. If an appropriate Store object cannot be obtained, NoSuchProviderException is thrown.
Parameters:
protocol -  
Returns:
a Store object
Throws:
NoSuchProviderException - If a provider for the given protocol is not found.

getStore

public Store getStore(URLName url)
               throws NoSuchProviderException
Get a Store object for the given URLName. If the requested Store object cannot be obtained, NoSuchProviderException is thrown. The "scheme" part of the URL string (Refer RFC 1738) is used to locate the Store protocol.

Parameters:
url - URLName that represents the desired Store
Returns:
a closed Store object
Throws:
NoSuchProviderException - If a provider for the given URLName is not found.
See Also:
getFolder(URLName), URLName

getStore

public Store getStore(Provider provider)
               throws NoSuchProviderException
Get an instance of the store specified by Provider. Instantiates the store and returns it.
Parameters:
provider - Store Provider that will be instantiated
Returns:
Instantiated Store
Throws:
NoSuchProviderException - If a provider for the given Provider is not found.

getFolder

public Folder getFolder(URLName url)
                 throws MessagingException
Get a closed Folder object for the given URLName. If the requested Folder object cannot be obtained, null is returned.

The "scheme" part of the URL string (Refer RFC 1738) is used to locate the Store protocol. The rest of the URL string (ie the "schemepart", as per RFC 1738) is used by that Store in a protocol dependent manner to locate and instantiate the appropriate Folder object.

Note that RFC 1738 also specifies the syntax for the "schemepart" for IP-based protocols (IMAP4, POP3 etc). So providers of IP-based mail Stores should implement that syntax for referring to Folders.

Parameters:
url - URLName that represents the desired folder
Returns:
Folder
Throws:
NoSuchProviderException - If a provider for the given URLName is not found.
MessagingException - if the Folder could not be located or created.
See Also:
getStore(URLName), URLName

getTransport

public Transport getTransport()
                       throws NoSuchProviderException
Get a Transport object that implements this user's desired Transport protcol. The mail.transport.protocol property specifies the desired protocol. If an appropriate Transport object cannot be obtained, MessagingException is thrown.
Returns:
a Transport object
Throws:
NoSuchProviderException - If the provider is not found.

getTransport

public Transport getTransport(java.lang.String protocol)
                       throws NoSuchProviderException
Get a Transport object that implements the specified protocol. If an appropriate Transport object cannot be obtained, null is returned.
Returns:
a Transport object
Throws:
NoSuchProviderException - If provider for the given protocol is not found.

getTransport

public Transport getTransport(URLName url)
                       throws NoSuchProviderException
Get a Transport object for the given URLName. If the requested Transport object cannot be obtained, NoSuchProviderException is thrown. The "scheme" part of the URL string (Refer RFC 1738) is used to locate the Transport protocol.

Parameters:
url - URLName that represents the desired Transport
Returns:
a closed Transport object
Throws:
NoSuchProviderException - If a provider for the given URLName is not found.
See Also:
URLName

getTransport

public Transport getTransport(Provider provider)
                       throws NoSuchProviderException
Get an instance of the transport specified in the Provider. Instantiates the transport and returns it.
Parameters:
provider - Transport Provider that will be instantiated
Returns:
Instantiated Transport
Throws:
NoSuchProviderException - If provider for the given provider is not found.

getTransport

public Transport getTransport(Address address)
                       throws NoSuchProviderException
Get a Transport object that can transport a Message to the specified address type.
Parameters:
address -  
Returns:
A Transport object
Throws:
NoSuchProviderException - If provider for the Address type is not found
See Also:
Address

setPasswordAuthentication

public void setPasswordAuthentication(URLName url,
                                      PasswordAuthentication pw)
Save a PasswordAuthentication for this (store or transport) URLName. If pw is null the entry corresponding to the URLName is removed.

This is normally used only by the store or transport implementations to allow authentication information to be shared among multiple uses of a session.


getPasswordAuthentication

public PasswordAuthentication getPasswordAuthentication(URLName url)
Return any saved PasswordAuthentication for this (store or transport) URLName. Normally used only by store or transport implementations.
Returns:
the PasswordAuthentication corresponding to the URLName

requestPasswordAuthentication

public PasswordAuthentication requestPasswordAuthentication(java.net.InetAddress addr,
                                                            int port,
                                                            java.lang.String protocol,
                                                            java.lang.String prompt,
                                                            java.lang.String defaultUserName)
Call back to the application to get the needed user name and password. The application should put up a dialog something like:

 Connecting to <protocol> mail service on host <addr>, port <port>.
 <prompt>

 User Name: <defaultUserName>
 Password:
 
Parameters:
addr - InetAddress of the host. may be null.
protocol - protocol scheme (e.g. imap, pop3, etc.)
prompt - any additional String to show as part of the prompt; may be null.
defaultUserName - the default username. may be null.
Returns:
the authentication which was collected by the authenticator; may be null.

getProperties

public java.util.Properties getProperties()
Returns the Properties object associated with this Session
Returns:
Properties object

getProperty

public java.lang.String getProperty(java.lang.String name)
Returns the value of the specified property. Returns null if this property does not exist.
Returns:
String that is the property value