JavaMail and WebLogic Server
The second half of this chapter focuses specifically with using JavaMail with WebLogic Server. We look first at configuring your WebLogic Server to use JavaMail, and then look at the details of building, sending, and receiving messages, and developing your own JavaMail applications.
Configuring WebLogic Server with a Mail Session
When writing email applications or components, you have a choice as to how you are going to define your Session object:
You can create a new Session object and set all the session properties into the body of the code or in initialization parameters.
You can create and define a Mail Session with the Administration Console. This is a universal javax.mail.Session object and is made available via a JNDI lookup to an email component that needs it.
Creating and using a WebLogic Server Mail Session greatly simplifies the construction and maintenance of mail applications and is the method we'll use in our examples. By configuring the Mail Session in WebLogic, you don't have to code the session parameters in the body of the code (or in servlet or EJB initialization parameters). You can make changes to the Mail Session's parameters on the fly within the Administration Console. Using a single Mail Session object also lowers overhead by removing the need to create a new Mail Session object for each client.
The following steps walk you through creating and configuring a Mail Session in the Administration Console:
Start WebLogic Server for the domain in which you're developing a JavaMail application.
Start the Administration Console.
In the Administration Console, click the Mail node in the left panel. The Mail Sessions page is displayed in the right panel, as shown in Figure 13.1.
Figure 13.1. The Mail Session introduction page.
Click the link to Configure a New Mail Session. The Create a new MailSession page is displayed, as shown in Figure 13.2.
Figure 13.2. Enter the properties for your new Mail Session in the Create a New MailSession page.
Complete the form in the right panel, as shown in Figure 13.3.
In the Name field, enter a name for the new session. For illustrative purposes, we retain the default name and call this MyMail Session.
In the JNDIName field, enter a name that will be used as a JNDI lookup name. We call this MyMailSession (no spaces).
In the Properties field, you may enter any properties to override the default settings of the JavaMail API. In the example, I've overridden the default store protocol IMAP to POP, with the statement mail.transport.protocol=pop. I'll also add the statement mail.debug=true, which will override the false setting and show JavaMail events in the WebLogic Server command window display.
Figure 13.3. The completed Mail Session parameters page, which sets the store protocol as POP.
A complete list of the Mail Session property fields, their descriptions, key examples, and default values are listed in the following section.
To use the POP protocol, you must have installed the POP3 extension or upgraded your JavaMail API to version 1.3, as previously explained in this chapter. Otherwise, leave the section blank to use the default IMAP protocol that comes with the weblogic.jar installation package. Click the Create button.
Finally, you must apply the Mail Session to a target server. Select the server myserver and click the Apply button as shown in Figure 13.4.
Figure 13.4. Assign the Mail Session to a server.
The new Mail Session, MyMail Session, is displayed under the Mail icon in the left panel. Clicking on the Mail Session icon in the left panel displays the configuration information in the right panel, as shown in Figure 13.5.
Figure 13.5. The MyMail Session page confirms all the configuration parameters for the MyMail Session Mail Session.
Mail Session Properties
The Mail Session object has default settings for all of its properties. This section outlines the more commonly used Mail Session properties that you can override when defining your own Mail Session object. An example will be provided, along with the default setting:
Name the mail server host machine.
Name of the local machine, such as localhost
Specify the mail store (retrieval) protocol.
Specify the mail transport (send) protocol.
Name the default user for retrieving email.
The value of the user.name Java system property
Specify the mail host for a specific protocol. For instance, you can set different mail services for your SMTP host and your IMAP host.
mail.smtp.host=smtp.mail.yahoo.com and mail.imap.host=localhost
The value of the mail.host property, such as localhost
Specify the username associated with a specific protocol for logging into a mailer server.
mail.smtp.user=myyahoousername and mail.imap.user=weblogic
The default value of the mail.user property
There are other Mail Session properties as well. Please refer to the following links for an exhaustive list: