Use mail

Using JavaMail API, a code segment can be embedded in any Java 2 Enterprise Edition (J2EE) application component, such as an EJB or a servlet, allowing the application to send a message and save a copy of the mail to the Sent folder.

The following is a code sample that you would embed in a J2EE application

javax.naming.InitialContext ctx = new javax.naming.InitialContext();

   javax.mail.Session mail_session = (javax.mail.Session) ctx.lookup("java:comp/env/mail/MailSession3");
   MimeMessage msg = new MimeMessage(mail_session);

   msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(""));

   msg.setFrom(new InternetAddress(""));

   msg.setSubject("Important message from");



   Store store = mail_session.getStore();


   Folder f = store.getFolder("Sent");

   if (!f.exists()) f.create(Folder.HOLDS_MESSAGES);

   f.appendMessages(new Message[] {msg});


J2EE applications can use JavaMail APIs by looking up references to logically named mail connection factories through the java:comp/env/mail subcontext declared in the application deployment descriptor and mapped to installation specific mail session resources. As in the case of other J2EE resources, this can be done in order to eliminate the need for the application to hard code references to external resources.

  1. Locate a resource through JNDI.The J2EE specification considers a mail session instance as a resource, or a factory from which mail transport and store connections can be obtained. You should never hardcode mail sessions, namely fill up a Properties object, then use it to new up a javax.mai.Session object. Instead, follow the J2EE programming model of configuring resources through the system facilities and then locating them through JNDI lookups.

    In the sample code above, the line javax.mail.Session mail_session = (javax.mail.Session) ctx.lookup("java:comp/env/mail/MailSession3"); is an example of not hard coding a mail session and using a resource name located through JNDI. You can consider the lookup name mail/MailSession3, as a soft link to the real resource.

  2. Define resource references while assembling your application.You must define a resource reference for the mail resource in the deployment descriptor of the component, because a mail session is referenced in the JNDI lookup. Typically, you can use the Assembly Toolkit (ATK) shipped with WAS. To learn more about using the ATK see the article Starting the Assembly Toolkit.

    When you create this reference, be sure that the name of the reference matches the name used in the code. For example, the code above uses java:comp/env/mail/MailSession3 in its lookup, therefore the name of this reference must be mail/Session3 and the type of the resource must be javax.mail.Session. After being defined, the deployment descriptor contains the following entry for the mail resource reference


  3. Configure mail providers and sessions.The sample code references a mail resource, the deployment descriptor declares the reference, but the resource itself does not exist yet. Now you need to configure the mail resource that is referenced by your application component. Notice that the mail session you configure must have both its transport and mail access portions defined; the former required because the code is sending a message, the latter because it also saves a copy to the local mail store. When you configure the mail session, you will be asked to specify a JNDI name. This is an important name which you will require when you install your application and link up the resource references in your application with the real resources that you have configured.

  4. Install your application. You can install your application using either the administrative console or the scripting tool. One important step in the install process is that the system will go through all resource references, among other things, and expect you to supply a JNDI name for each of them. This is not an arbitrary JNDI name but the JNDI name given to a particular, configured resource which is the target of the reference.

  5. Manage existing mail providers and sessions.You can update and remove mail providers and sessions.

    To update mail providers and sessions...

    1. Open the administrative console.

    2. Click Resources > Mail Providers in the console navigation tree. Then, click Mail Provider > mail_provider > Mail Session.

    3. Click the mail_provider or mail_session that you want to modify.To remove a mail provider or mail session, click Remove after making your selection.

    4. Click Apply or OK.

    5. Save the configuration.

  6. Enable debugger for a mail session.

If your application has a client, you can update mail providers and mail sessions using the ACRCT.


See Also

JavaMail API
Mail providers and mail sessions
Developing applications that use JNDI
Assembling applications with the Assembly Toolkit
Install a new application
Monitoring performance
Starting the Assembly Toolkit
Mail migration tip
JavaMail security permissions best practices
Mail: Resources for learning