ending out email in a Web application is a common business need. In the Java realm, sending out e-mail is facilitated via the JavaMail API, which is a required part of the enterprise platform (Java EE) but can also be used with Java SE. JavaMail is a set of Java APIs that allow for the reading and sending of e-mail from within a Java application.
In this article, I'll show you how to create a Java servlet housed in an enterprise application that sends out e-mail via the JavaMail API. My solution will utilize IBM WebSphere Application Server Version 6 and, for development, IBM Rational Application Developer Version 6 (hereafter called Application Developer). Application Developer is an Eclipse-based integrated development environment (IDE) that is ideally suited for use with WebSphere Application Server Version 6. The IDE has a built-in, single server test environment for WebSphere Application Server Version 6 (hereafter called WAS 6) that one can use to unit test their enterprise applications. In this article you'll also learn some of the tooling in Application Developer that can help speed your WAS 6 development endeavors.
Finding Out Your SMTP Server Info
Before getting started, you need to find out what your organization's SMTP server name and server port are. This can usually be found out by knocking on your system administrator's door. Your SMTP server is usually something like SMTP.YOURCOMPANY.COM. The default SMTP port is 25. If you don't have access to an SMTP server or if your system administrator doesn't like you, then you can always run your own SMTP server. A popular, free program for running your own SMTP server is PostCast Server.
Configuring a Mail Provider
In order to send out an e-mail in a Java servlet, one could possibly create a mail session via a Properties object. However, on a J2EE server such as WAS 6, the recommended approach is to look up a Mail Session via the Java Naming and Directory Interface (JNDI). Accordingly, I'll show you how to setup a Java Mail Session using the WAS 6 Administrative Console.
If you are using the built in WebSphere Application Server Test Environment, then you can fire up the Administrative Console from the Servers view by right-clicking to reveal the context menu and choosing "Run administrative console." Otherwise, if you are using an external WebSphere Application Server environment, then just go to the administrative console using your Web browser. From the left-hand panel, choose Resources>Mail Providers (see Figure 1).
From the Mail Providers screen, click the Built-in Mail Provider. The Built-in Mail Provider of WAS 6 includes three protocol providers: Simple Mail Transfer Protocol (SMTP), Internet Message Access Protocol (IMAP) and Post Office Protocol (POP3). For sending out mail, I'll be using the SMTP protocol provider of the built-in mail provider (see Figure 2).
Figure 1. Choose the Resources> Mail Providers option to begin creating a Mail Provider.
Figure 2. Select the Built-in Mail Provider as the mail provider.
In the subsequent Built-in Mail Provider configuration screen, click the "Mail sessions" link under the 'Additional Properties' area. Next, you need to create a Mail Session object. To begin creating the Mail Session object, click the New button (see Figure 3
In the next screen, you need to define configuration information for your mail session. Specify a name of mymailsession. For a JNDI name, specify mail/mymailsession (see Figure 4).
Enter your SMTP server name for the "Mail transport host." Make sure the Mail transport protocol is "smtp." In some cases, an SMTP server restricts outgoing mail to authenticated users only. If this is the case with your server, additionally specify the authorized user ID and password recognized by your SMTP server on this dialog.
Figure 3. Click New to start creating a Mail Session.
Figure 4. Carefully configure your mail session on this dialog.
After you have specified your appropriate mail session configuration information, click the "Apply" button at the bottom of the configuration screen. You will be asked to "Click Save to apply changes to the master configuration." Go ahead and do so and click Save again in the following screen to persist your administrative changes.