Java Reference
In-Depth Information
The best way to handle e-mail is to use the JavaMail API. The JavaMail API makes handling e-mail very
straightforward, as it is designed to provide a protocol-independent means of sending and receiving
messages. You can download the JavaMail API from Sun. In addition, you need to download the Java
Activation Framework (JAF), which provides the basic MIME-type support used in most e-mail
applications.
The first step in sending an e-mail using JavaMail is to get a JavaMail Session . Within the context of
the Session , you create a new Message object, set its properties, and send it. These are the core
JavaMail API classes needed to do perform these tasks:
 
Session — defines a basic mail session
 
Message — in most cases you will use javax.mail.internet.MimeMessage .
 
Address — normally, you use the javax.mail.internet.InternetAddress .
 
Transport — performs the protocol-specific tasks involved in sending the message
 
Store — to receive e-mail messages, you first connect to a Mail Store.
 
Folder — a Mail Store contains folders of messages that can be downloaded and read.
The session, message, address and transport classes of the core JavaMail API are
explained below, and are illustrated in the first example, which shows you how to send an e-mail
message. The remaining classes are used when receiving e-mails messages, and are explained and
illustrated in the second example which illustrates how to receive e-mail messages.
The Session object defines a basic mail session. It uses a java.util.Properties object to hold
application-level information such as the mail server, username, and password. In most cases, you can
just use the shared session, even if you are working with multiple-user mailboxes.
The Message object represents the e-mail message. Properties of the Message object include the
subject, the content, and the addresses of the sender and the recipient. A Mime Message is an e-mail
message that understands different MIME types and headers.
E-mail addresses are implemented using the Address object. Normally, you use the
javax.mail.internet.InternetAddress class. The Address object has constructors that let
you set just an e-mail address or set an e-mail address and the name of the sender or recipient.
Note
The JavaMail API does not check the contents of an Address object, so unless your
mail server prevents you, there is nothing stopping you from sending a message that
appears to be from anyone.
The Transport object handles the protocol-specific language for sending the message (usually
SMTP). You can use the default version of the class by calling the static send() method, or you can
get a specific instance from the session. Here's an example:
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Note
The basic send() mechanism makes a separate connection to the server for each
method call. When you need to send multiple messages, it is better to get a specific
instance of Transport; this keeps the connection with the mail server active between
messages.
The first example explains how to use these classes to send an e-mail message. Receiving e-mail
messages is covered in the second example.
Using JavaMail with JDBC to Send an E-mail Message
Search WWH ::




Custom Search