Flylib.com

Books Software

 
 
 

Section 8.7. Deploying JavaMail on JBoss


8.7. Deploying JavaMail on JBoss

We now need to configure JBoss so we can use it as a JavaMail provider. JBoss manages a JavaMail Session as a JMX MBean that creates the Session and registers its JNDI name . The JBoss JavaMail XML descriptor, $JBOSS_HOME/server/jbossatwork/deploy/mail-service.xml , sets up a JMX MBean that configures a JNDI-based JavaMail Session in Example 8-8.

Example 8-8. mail-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>

<server>

  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="User">yourUserId</attribute>
    <attribute name="Password">yourPassword</attribute>
    <attribute name="Configuration">
       <configuration>
          <!-- Set the protocol for your mail server -->
          <property name="mail.store.protocol" value="pop3"/>
          <property name="mail.transport.protocol" value="smtp"/>

          <!-- Configure the POP3 Server  -->
          <property name="mail.pop3.host" value="yourIsp.pop3.host"/>

          <!-- Configure the SMTP gateway server -->
          <property name="mail.smtp.host" value="yourIsp.smtp.host "/>
          <property name="mail.smtp.port" value="25"/>

          <property name="mail.debug" value="true"/>
       </configuration>
    </attribute>
  </mbean>

</server>

The mail-service.xml file configures a JavaMail Session with the email account properties that you use to connect with your email service provider:

  • Mail Store Protocol

  • Mail Transport Protocol

  • SMTP server name

  • POP server name

  • An email account user ID

  • An email account password

These are standard JavaMail properties, and you can find a complete listing of them in Appendix A of the JavaMail Design Specification at: http://java.sun.com/products/javamail/JavaMail-1.2.pdf.

The example above uses bogus values for its ISP settings, so you'll need to edit the $JBOSS_HOME/server/jbossatwork/deploy/mail-service.xml file and fill in the protocol and account settings you use to access your ISP's mail server. You could change the JNDI name to something other than java:/Mail , but every JBoss installation we've seen uses this value to deploy its JavaMail Session . Therefore we recommend sticking with the default value to be consistent with the way most people use JBoss.



8.8. JavaMail Checklist

Before we move on to test the JAW Motors application's new email functionality, let's recap what we've done to implement JavaMail:

  • Added Java Code:

    • Called the TextEmail JavaMail utility class from the CreditCheckProcessor MDB

    • Wrote a TextEmail utility class to encapsulate sending an email message with JavaMail

    • Factored out JNDI calls into the ServiceLocator

  • Upgraded deployment:

    • Added JavaMail-based JNDI reference settings to the EJB-based deployment descriptors ( ejb-jar.xml and jboss.xml )

    • Automated JavaMail-based JNDI reference settings in the CreditCheckProcessor MDB with XDoclet

    • Configured JavaMail on JBoss with an MBean



8.9. Testing the Credit Check Notification Email

Now that we've developed and deployed JavaMail code to send an email notification message to the user , let's test our application to ensure that everything still works properly. Here are the steps to build and deploy the application:

  • Type ant in the root directory of ch08 to build the project.

  • Shut down JBoss so the Ant script can clean up the JBoss deployment area.

  • Type ant colddeploy to deploy the EAR file ( jaw.ear ) to the $JBOSS_HOME/server/default/deploy directory.

  • Start JBoss back up.

  • Visit http://localhost:8080/jaw in a web browser.

Click on the "Run Credit Check" link on the JAW Motors home page, enter data on the "Run Credit Check" form, and press the "Submit Credit Info" button. You should be routed back to the main page, and in a minute or two you should see a message that looks like this in your email client's Inbox:

From:    credit.check@jbossatwork.com
To:      yourName@host.domain
Subject: Credit Check Result

Pass Credit Check

The credit check randomly passes or fails, so you could also see " Fail Credit Check " in the email message body.