Sun Java logo     Previous      Contents      Index      Next     

Sun logo
Sun Java System Application Server Platform Edition 8 Developer's Guide 

Chapter 15  
Using the JavaMail API

This chapter describes how to use the JavaMail™ API, which provides a set of abstract classes defining objects that comprise a mail system.

This chapter contains the following sections:


Introducing JavaMail

The JavaMail API defines classes such as Message, Store, and Transport. The API can be extended and can be subclassed to provide new protocols and to add functionality when necessary. In addition, the API provides concrete subclasses of the abstract classes. These subclasses, including MimeMessage and MimeBodyPart, implement widely used Internet mail protocols and conform to the RFC822 and RFC2045 specifications. The JavaMail API includes support for the IMAP4, POP3, and SMTP protocols.

The JavaMail architectural components are as follows:

For more information, see the Sun Java System Application Server Administration Guide and the JavaMail specification at:

http://java.sun.com/products/javamail/


Creating a JavaMail Session

You can create a JavaMail session in the following ways:

The “Using The Administration Console” section describes each JavaMail session setting. The “Using The Command Line Interface” section merely lists syntax and default values.

Using the Administration Console

To create a JavaMail session using the Administration Console, perform these steps:

  1. Login to the Administration Console by going to the following URL in your web browser:
  2. http://host:port/asadmin

    For example:

    http://localhost:4848/asadmin

  3. Click on the Java Mail Sessions component.
  4. Click the New button.
  5. Enter the following information:
    • JNDI Name (required) - Enter the JNDI name that application components must use to access the JavaMail session. For more information, see Looking Up a JavaMail Session.
    • Mail Host (required) - The mail server host name.
    • Default User (required) - The mail server user name.
    • Default Return Address (required) - The e-mail address the mail server uses to indicate the message sender.
    • Description (optional) - You can enter a text description of the JavaMail session.
  6. Check the Session Enabled box to enable the JavaMail session. If a JavaMail session is disabled, it is unavailable for lookups, but its configuration remains in the domain.
  7. You can also edit the following Advanced settings:
    • Store Protocol - Specifies the storage protocol service, which connects to a mail server, retrieves messages, and saves messages in folder(s). Example values are imap (the default) and pop3.
    • Store Protocol Class - Specifies the service provider implementation class for storage. The default is com.sun.mail.imap.IMAPStore.
    • Transport Protocol - Specifies the transport protocol service, which sends messages. The default is smtp.
    • Transport Protocol Class - Specifies the service provider implementation class for transport. The default is com.sun.mail.smtp.SMTPTransport.
    • Debug Enabled - Enables debugging for this JavaMail session.
  8. To add a property, click the Add Property button and enter the property name and value. To delete properties, check the properties you want to delete, then click the Delete Properties button. For details about JavaMail session properties, see JavaMail Session Properties.
  9. Click the OK button.

Using the Command Line Interface

To create a JavaMail session using the command line, use the asadmin create-javamail-resource command. The syntax is as follows, with defaults shown for optional parameters that have them:

asadmin create-javamail-resource --user user --mailhost mail_host --mailuser mail_user --fromaddress address [--storeprotocol=imap] [--storeprotocolclass=com.sun.mail.imap.IMAPStore] [--transprotocol=smtp] [--transprotocolclass=com.sun.mail.smtp.SMTPTransport] [--debug=false] [--enabled=true] [--description text] [--property (name=value)[:name=value]*] jndi_name

For more information about the parameters specific to asadmin create-javamail-resource, see Using the Administration Console.

For more information about the optional general asadmin parameters (--password, --passwordfile, --host, --port, --secure, --terse, --echo, and --interactive), see the Sun Java System Application Server Administration Guide.

For example:

asadmin create-javamail-resource --user joeuser --mailhost MailServer --mailuser MailUser --fromaddress user@mailserver.com MailSession

To delete a JavaMail session, use the following command:

asadmin delete-javamail-resource --user user jndi_name

For example:

asadmin delete-javamail-resource --user joeuser MailSession

To list JavaMail sessions, use the following command:

asadmin list-javamail-resources --user user

For example:

asadmin list-javamail-resources --user joeuser


JavaMail Session Properties

You can set properties for a JavaMail Session object. Every property name must start with a mail- prefix. Sun Java System Application Server changes the dash (-) character to a period (.) in the name of the property and saves the property to the MailConfiguration and JavaMail Session objects. If the name of the property doesn’t start with mail-, the property is ignored.

For example, if you want to define the property mail.from in a JavaMail Session object, first define the property as follows:

After you get the JavaMail Session object, you can get the mail.from property to retrieve the value as follows:

String password = session.getProperty("mail.from");


Looking Up a JavaMail Session

The standard Java Naming and Directory Interface™ (JNDI) subcontext for JavaMail sessions is java:comp/env/mail.

Registering JavaMail sessions in the mail naming subcontext of a JNDI namespace, or in one of its child subcontexts, is standard. The JNDI namespace is hierarchical, like a file system’s directory structure, so it is easy to find and nest references. A JavaMail session is bound to a logical JNDI name. The name identifies a subcontext, mail, of the root context, and a logical name. To change the JavaMail session, you can change its entry in the JNDI namespace without having to modify the application.

The resource lookup in the application code looks like this:

InitialContext ic = new InitialContext();
String snName = "java:comp/env/mail/MyMailSession";
Session session = (Session)ic.lookup(snName);

For more information about the JNDI API, see Chapter 13, “Using the Java Naming and Directory Interface.”


Sending Messages Using JavaMail

To send a message using JavaMail, perform the following tasks:

  1. Import the packages that you need:
  2. import java.util.*;
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.naming.*;

  3. Look up the JavaMail session, as described in Looking Up a JavaMail Session:
  4. InitialContext ic = new InitialContext();
    String snName = "java:comp/env/mail/MyMailSession";
    Session session = (Session)ic.lookup(snName);

  5. Override the JavaMail session properties if necessary. For example:
  6. Properties props = session.getProperties();
    props.put("mail.from", "user2@mailserver.com");

  7. Create a MimeMessage. The msgRecipient, msgSubject, and msgTxt variables in the following example contain input from the user:
  8. Message msg = new MimeMessage(session);
    msg.setSubject(msgSubject);
    msg.setSentDate(new Date());
    msg.setFrom();
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(msgRecipient, false));
    msg.setText(msgTxt);

  9. Send the message:
  10. Transport.send(msg);


Reading Messages Using JavaMail

To read a message using JavaMail, perform the following tasks:

  1. Import the packages that you need:
  2. import java.util.*;
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.naming.*;

  3. Look up the JavaMail session, as described in Looking Up a JavaMail Session:
  4. InitialContext ic = new InitialContext();
    String snName = "java:comp/env/mail/MyMailSession";
    Session session = (javax.mail.Session)ic.lookup(snName);

  5. Override the JavaMail session properties if necessary. For example:
  6. Properties props = session.getProperties();
    props.put("mail.from", "user2@mailserver.com");

  7. Get a Store object from the Session, then connect to the mail server using the Store object’s connect() method. You must supply a mail server name, a mail user name, and a password.
  8. Store store = session.getStore();
    store.connect("MailServer", "MailUser", "secret");

  9. Get the default folder, then get the INBOX folder:
  10. Folder folder = store.getFolder("INBOX");

  11. It is efficient to read the Message objects (which represent messages on the server) into an array:
  12. Message[] messages = folder.getMessages();



Previous      Contents      Index      Next     


Copyright 2004 Sun Microsystems, Inc. All rights reserved.