java.lang.ObjectThe 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.javax.mail.Session
The Session class provides access to the protocol providers that
implement the Store, Transport, and related
classes. The protocol providers are configured using the following files:
javamail.providers and
javamail.default.providers javamail.address.map and
javamail.default.address.map
Each javamail.X resource file is searched for using
three methods in the following order:
java.home/lib/javamail.X META-INF/javamail.X META-INF/javamail.default.X
The first method allows the user to include their own version of the
resource file by placing it in the lib directory where the
java.home property points. The second method allows an
application that uses the JavaMail APIs to include their own resource
files in their application's or jar file's META-INF
directory. The javamail.default.X default files
are part of the JavaMail mail.jar file.
File location depends upon how the ClassLoader method
getResource is implemented. Usually, the
getResource method searches through CLASSPATH until it
finds the requested file and then stops. JDK 1.1 has a limitation that
the number of files of each name that will be found in the CLASSPATH is
limited to one. However, this only affects method two, above; method
one is loaded from a specific location (if allowed by the
SecurityManager) and method three uses a different name to ensure that
the default resource file is always loaded successfully. J2SE 1.2 and
later are not limited to one file of a given name.
The ordering of entries in the resource files matters. If multiple entries exist, the first entries take precedence over the later entries. For example, the first IMAP provider found will be set as the default IMAP implementation until explicitly changed by the application. The user- or system-supplied resource files augment, they do not override, the default files included with the JavaMail APIs. This means that all entries in all files loaded will be available.
javamail.providers and
javamail.default.providers
These resource files specify the stores and transports that are available on the system, allowing an application to "discover" what store and transport implementations are available. The protocol implementations are listed one per line. The file format defines four attributes that describe a protocol implementation. Each attribute is an "="-separated name-value pair with the name in lowercase. Each name-value pair is semi-colon (";") separated. The following names are defined.
| Name | Description |
|---|---|
| protocol | Name assigned to protocol.
For example, smtp for Transport. |
| type | Valid entries are store and transport. |
| class | Class name that implements this protocol. |
| vendor | Optional string identifying the vendor. |
| version | Optional string identifying the version. |
Here's an example of META-INF/javamail.default.providers
file contents:
protocol=imap; type=store; class=com.sun.mail.imap.IMAPStore; vendor=Sun Microsystems, Inc.; protocol=smtp; type=transport; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc.;
javamail.address.map and
javamail.default.address.map
These resource files map transport address types to the transport
protocol. The getType method of
javax.mail.Address returns the address type. The
javamail.address.map file maps the transport type to the
protocol. The file format is a series of name-value pairs. Each key
name should correspond to an address type that is currently installed
on the system; there should also be an entry for each
javax.mail.Address implementation that is present if it is
to be used. For example, the
javax.mail.internet.InternetAddress method
getType returns "rfc822". Each referenced protocol should
be installed on the system. For the case of news, below,
the client should install a Transport provider supporting the nntp
protocol.
Here are the typical contents of a javamail.address.map file:
rfc822=smtp news=nntp
1.76 - , 07/05/04John - ManiBill - ShannonMax - Spivak| Method from javax.mail.Session Summary: |
|---|
| addProvider, getDebug, getDebugOut, getDefaultInstance, getDefaultInstance, getFolder, getInstance, getInstance, getPasswordAuthentication, getProperties, getProperty, getProvider, getProviders, getStore, getStore, getStore, getStore, getTransport, getTransport, getTransport, getTransport, getTransport, requestPasswordAuthentication, setDebug, setDebugOut, setPasswordAuthentication, setProtocolForAddress, setProvider |
| Methods from java.lang.Object: |
|---|
| equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from javax.mail.Session Detail: |
|---|
|
|
System.out is returned. |
Note that a default session created with no Authenticator is 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. |
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.
Note also that the Properties object is used only the first time
this method is called, when a new Session object is created.
Subsequent calls return the Session object that was created by the
first call, and ignore the passed Properties object. Use the
In JDK 1.2, additional security Permission objects may be used to control access to the default session. |
The "scheme" part of the URL string (Refer RFC 1738) is used to locate the Store protocol. The rest of the URL string (that is, 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.). Providers of IP-based mail Stores should implement that syntax for referring to Folders. |
|
|
|
|
|
|
|
mail.store.protocol property specifies the
desired protocol. If an appropriate Store object is not obtained,
NoSuchProviderException is thrown |
|
|
|
mail.transport.protocol property
specifies the desired protocol. If an appropriate Transport
object cannot be obtained, MessagingException is thrown. |
|
|
|
|
Connecting to <protocol> mail service on host <addr>, port <port>. <prompt> User Name: <defaultUserName> Password: |
Since the debug setting can be turned on only after the Session
has been created, to turn on debugging in the Session
constructor, set the property |
out is null, System.out will be used.
Note that debugging output that occurs before any session is created,
as a result of setting the mail.debug system property,
will always be sent to System.out. |
This is normally used only by the store or transport implementations to allow authentication information to be shared among multiple uses of a session. |
javamail.default.address.map or
javamail.address.map files or resources. |
|