Package javax.mail
javax.mail
package defines classes that are common to
all mail systems.
The javax.mail.internet
package defines classes that are specific
to mail systems based on internet standards such as MIME, SMTP, POP3, and IMAP.
The JavaMail API includes the javax.mail
package and subpackages.
For an overview of the JavaMail API, read the JavaMail specification.
The code to send a plain text message can be as simple as the following:
Properties props = new Properties(); props.put("mail.smtp.host", "my-mail-server"); Session session = Session.getInstance(props, null); try { MimeMessage msg = new MimeMessage(session); msg.setFrom("[email protected]"); msg.setRecipients(Message.RecipientType.TO, "[email protected]"); msg.setSubject("JavaMail hello world example"); msg.setSentDate(new Date()); msg.setText("Hello, world!\n"); Transport.send(msg, "[email protected]", "my-password"); } catch (MessagingException mex) { System.out.println("send failed, exception: " + mex); }The JavaMail download bundle contains many more complete examples in the "demo" directory.
Don't forget to see the JavaMail API FAQ for answers to the most common questions. The JavaMail web site contains many additional resources.
Properties
The JavaMail API supports the following standard properties,
which may be set in the Session
object, or in the
Properties
object used to create the Session
object.
The properties are always set as strings; the Type column describes
how the string is interpreted. For example, use
props.put("mail.debug", "true");to set the
mail.debug
property, which is of type boolean.
The following properties are supported by the reference implementation (RI) of JavaMail, but are not currently a required part of the specification. The names, types, defaults, and semantics of these properties may change in future releases.
Name | Type | Description |
---|---|---|
mail.debug.auth | boolean | Include protocol authentication commands (including usernames and passwords) in the debug output. Default is false. |
mail.debug.auth.username | boolean | Include the user name in non-protocol debug output. Default is true. |
mail.debug.auth.password | boolean | Include the password in non-protocol debug output. Default is false. |
mail.transport.protocol.address-type | String |
Specifies the default message transport protocol for the specified address type.
The Session method getTransport(Address) returns a
Transport object that implements this protocol when the address is of the
specified type (e.g., "rfc822" for standard internet addresses).
By default the first Transport configured for that address type is used.
This property can be used to override the behavior of the
send method of the
Transport class so that (for example) the "smtps"
protocol is used instead of the "smtp" protocol by setting the property
mail.transport.protocol.rfc822 to "smtps" .
|
mail.event.scope | String | Controls the scope of events. (See the javax.mail.event package.) By default, a separate event queue and thread is used for events for each Store, Transport, or Folder. If this property is set to "session", all such events are put in a single event queue processed by a single thread for the current session. If this property is set to "application", all such events are put in a single event queue processed by a single thread for the current application. (Applications are distinguished by their context class loader.) |
mail.event.executor | java.util.concurrent.Executor | By default, a new Thread is created for each event queue. This thread is used to call the listeners for these events. If this property is set to an instance of an Executor, the Executor.execute method is used to run the event dispatcher for an event queue. The event dispatcher runs until the event queue is no longer in use. |
The JavaMail API also supports several System properties;
see the javax.mail.internet
package documentation
for details.
The JavaMail reference
implementation includes protocol providers in subpackages of
com.sun.mail
. Note that the APIs to these protocol
providers are not part of the standard JavaMail API. Portable
programs will not use these APIs.
Nonportable programs may use the APIs of the protocol providers
by (for example) casting a returned Folder
object to a
com.sun.mail.imap.IMAPFolder
object. Similarly for
Store
and Message
objects returned from the
standard JavaMail APIs.
The protocol providers also support properties that are specific to
those providers. The package documentation for the
IMAP
, POP3
,
and SMTP
packages provide details.
In addition to printing debugging output as controlled by the
Session
configuration, the current
implementation of classes in this package log the same information using
Logger
as described in the following table:
Logger Name | Logging Level | Purpose |
---|---|---|
javax.mail | CONFIG | Configuration of the Session |
javax.mail | FINE | General debugging output |
-
ClassDescriptionThis abstract class models the addresses in a message.This exception is thrown when the connect method on a Store or Transport object fails due to an authentication failure (e.g., bad user name or password).The class Authenticator represents an object that knows how to obtain authentication for a network connection.This class models a Part that is contained within a Multipart.A
DataSource
that also implementsEncodingAware
may specify the Content-Transfer-Encoding to use for its data.Clients use a FetchProfile to list the Message attributes that it wishes to prefetch from the server for a range of messages.This inner class is the base class of all items that can be requested in a FetchProfile.The Flags class represents the set of flags on a Message.This inner class represents an individual system flag.Folder is an abstract class that represents a folder for mail messages.This exception is thrown when a method is invoked on a Messaging object and the Folder that owns that object has died due to some reason.This exception is thrown by Folder methods, when those methods are invoked on a non existent folder.The Header class stores a name/value pair to represent headers.The exception thrown when a write is attempted on a read-only attribute of any Messaging object.Annotation used by Java EE applications to define aMailSession
to be registered with JNDI.Declares one or moreMailSessionDefinition
annotations.This class models an email message.This inner class defines the types of recipients allowed by the Message class.An interface optionally implemented byDataSources
to supply information to aDataContentHandler
about the message context in which the data content object is operating.The context in which a piece of Message content is contained.The exception thrown when an invalid method is invoked on an expunged Message.The base class for all exceptions thrown by the Messaging classesThe exception thrown when a method is not supported by the implementationMultipart is a container that holds multiple body parts.MultipartDataSource is aDataSource
that contains body parts.This exception is thrown when Session attempts to instantiate a Provider that doesn't exist.ThePart
interface is the common base interface for Messages and BodyParts.The class PasswordAuthentication is a data holder that is used by Authenticator.The Provider is a class that describes a protocol implementation.This inner class defines the Provider type.This class represents a set of quotas for a given quota root.An individual resource in a quota root.An interface implemented by Stores that support quotas.This exception is thrown when an attempt is made to open a folder read-write access when the folder is marked read-only.This exception is thrown when the message cannot be sent.An abstract class that contains the functionality common to messaging services, such as stores and transports.The Session class represents a mail session and is not subclassed.An abstract class that models a message store and its access protocol, for storing and retrieving messages.This exception is thrown when a method is invoked on a Messaging object and the Store that owns that object has died due to some reason.An abstract class that models a message transport.TheUIDFolder
interface is implemented by Folders that can support the "disconnected" mode of operation, by providing unique-ids for messages in the folder.A fetch profile item for fetching UIDs.The name of a URL.