InfoCenter Home >
4: Developing applications >
4.6: Java Technologies >
4.6.1: Using JavaMail >
4.6.1.4: Running the JavaMail sample

4.6.1.4: Running the JavaMail sample

The JavaMail sample is packaged in an ear file called jmsample.ear that is located in directory:

 product_installation_root/installableApps
The JavaMail sample contains three application components:
  1. one stateful session bean
  2. one servlet
  3. one JSP file

Each component, when invoked, gathers data to compose a mail message, and then sends the message. Optionally, the sent message can also be saved in an IMAP account, in a folder named Sent. See article Writing JavaMail applications for the coding example to send a message and create the Sent folder.

Complete the following tasks to use jmsample.ear:

  1. Set up mail servers
  2. Configure a MailSession resource
  3. Install the jmsample.ear components

  1. Set up mail servers

    To send out email messages, you need a mail transport server. Since SMTP is the most widely used transport protocol, such a mail server is also known as a SMTP server. An alternative to installing and configuring your own SMTP server, is to use an existing implementation. For example, if your Internet mail address is john_smith@mycompany.com, then mycompany.com could serve as your SMTP server. Ask your company's email administrator for more information.

    The components in the jmsample.ear file can optionally save a copy of the sent message into an email account. If you plan to try this capability, you will also need to set up an IMAP mail account.

  2. Configure a MailSession resource

    See article Properties related to JavaMail support for information on MailSession resource properties.

    One property that requires your attention when you configure the MailSession resource for jmsample.ear is JNDI Name. You can explicitly define this property or allow System Management to define it for you.

    Since for all components in this sample the MailSession resource references have been pre-bound to the JNDI path mail/DefaultMailSession, enter this path in the JNDI Name property.

    If, at this time, you do not define the JNDI Name as shown above, you will have to bind the application's mail resource references to this mail session when you install jmsample.ear.

    Review the tutorial, Create a JavaMail session, for detailed information on configuring the mail session resource.

  3. Install the jmsample.ear components

    Install the jmsample.ear file as an enterprise application. Use the SEAppInstall command.

    Perform the following steps to install the jmsample.ear using the SEAppInstall command:

      1. Go to a command prompt.
      2. Go to the product_installation_root\installableApps directory, and locate the jmsample.ear file.
      3. Enter the following command to install the sample:
        • For Windows, enter:
          SEAppInstall -install jmsample.ear
        • For UNIX platforms, enter:
          product_installation_root/bin/SEAppInstall.sh -install jmsample.ear

        The SEAppInstall command will display several messages indicating its progress. The command will also prompt you for input. In this example of the dialog, the questions asked by the command are in bold, and your responses are in italics.

          Do you wish to deploy all of the EJBs in this application ([Y]es/[n]o)?
          Yes
          (A message should display indicating the EJBs were deployed successfully.)
          Which type of database are you using (optional)?
          0
          What DB Schema name do you want to use for this application (optional)?
          Press Enter
          Do you wish to precompile all JSPs in this application ([Y]es/[n]o)?
          Yes
          (At the "Default Datasource JNDI Name (optional) []" prompt, press Enter to accept the default.)
          (At the "JNDI Name [ejb/JMSamEJB]:" prompt, press Enter to accept the default.)
          (At the "JNDI Name [mail/DefaultMailSession]:" prompt, press Enter to accept the default.)
          (At the "JNDI Name [mail/DefaultMailSession]:" prompt, press Enter to accept the default.)
          (At the "JavaMailSample WebApp (WebApp_1) []:" prompt, enter default_host to specify the virtual host.)
          (A message should display stating the application installed successfully.)
      4. Stop and restart WebSphere Application Server with the following commands:
        • Go to a command prompt
        • On Windows, enter stopServer. On UNIX platforms, enter product_installation_root/bin/stopServer.sh
        • On Windows, enter startServer. On UNIX platforms, enter product_installation_root/bin/startServer.sh
        • (Check the product_installation_root/logs/default_server_stdout.log file and make sure the "Server Default Server open for e-business" message is displayed.)

    After the install, you can invoke the servlet or JSP by using one of the following URLs:

    http://localhost:9080/jmsample/servlet
    http://localhost:9080/jmsample/Email.jsp

    To test the JavaMail servlet, do the following:

    1. Open a browser window
    2. Enter http://localhost:9080/jmsample/servlet
      (The servlet GUI should display.)
    3. Enter the following information, replacing variable input as appropriate:
      • To: Your e-mail address as for example, anybody@mycompany.com
      • Cc: Optionally enter another e-mail address here.
      • From: somebody@mycompany.com
      • Subject: JavaMail Servlet Test
      • Message to send: Any text as for example, This is a test message that is being sent from the JavaMail Mail Servlet.
      • Check the Save the sent message into mail store box.
      • Send Option: As is
      • Click the Send button.
        (If the test number at the top of the page was incremented from 1 to 2, the message was sent successfully.)
    4. Log on to your e-mail account and verify that you received the mail

    To test the JavaMail Java Server Page, do the following:

    1. Open a browser window
    2. Enter http://localhost:9080/jmsample/Email.jsp
      (The Java Server Page GUI should display.)
    3. Enter the following information, replacing variable input as appropriate:
      • To: Your e-mail address as for example, anybody@mycompany.com
      • Cc: Optionally enter another e-mail address here.
      • From: somebody@mycompany.com
      • Subject: JavaMail JSP Test
      • Message to send: Any text as for example, This is a test message that is being sent from the JavaMail Mail JSP.
      • Check the Save the sent message into mail store box.
      • Send Option: As is
      • Click the Send button.
        (If the test number at the top of the page was incremented from 1 to 2, the message was sent successfully.)
    4. Log on to your e-mail account and verify that you received the mail

    Use these instructions to invoke the EJB:

    1. Locate file deplmtest.jar in the product_installation_root/InstalledApps/jmsample.ear directory.
    2. Copy the deplmtest.jar file to the product_installation_root/classes directory.

         The deplmtest.jar file contains all the artifacts for the EJB, including the proxies and a simple client.
    3. The client uses the system properties as data input for the message creation. The following example demonstrates how Java system properties are gathered for the EJB client on Windows NT:
        The line breaks in this example were added to make the information more legible. This information really exists as one line of input.

      Change the property values in this example to the ones you defined for your test.

      For the Windows platform, specify the following:

      product_installation_root\java\bin\java -Djava.ext.dirs=product_installation_root\java\jre\lib\ext;product_installation_root\classes;product_installation_root\lib
      -Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
      -Dmailtest.to=bob@mycompany.com
      -Dmailtest.cc=alice@mycompany.com
      -Dmailtest.from=john@mycompany.com
      -Dmailtest.subj="Important message sent from an EJB"
      -Dmailtest.message="As the subject line says, this is a very important message."
      -Dmailtest.save_msg=off
      -Dmailtest.ejbhome=ejb/JMSampEJB  mailtest.MailClient
      

      For UNIX platforms, specify the following:

        Entering the following command as one, continuous line of input at a command prompt might not work on some UNIX platforms. Use the back slash to indicate the command continues on the next line, or invoke the command from a shell script.
      product_installation_root/java/bin/java -Djava.ext.dirs=product_installation_root/java/jre/lib/ext:product_installation_root/classes:product_installation_root/lib
      -Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
      -Dmailtest.to=bob@mycompany.com
      -Dmailtest.cc=alice@mycompany.com
      -Dmailtest.from=john@mycompany.com
      -Dmailtest.subj="Important message sent from an EJB"
      -Dmailtest.message="As the subject line says, this is a very important message."
      -Dmailtest.save_msg=off
      -Dmailtest.ejbhome=ejb/JMSampEJB  mailtest.MailClient
      

See the related topics for links to Javadoc and the Create a JavaMail session tutorial.

Go to previous article: Debugging JavaMail Go to next article: JNDI (Java Naming and Directory Interface) overview

 

 
Go to previous article: Debugging JavaMail Go to next article: JNDI (Java Naming and Directory Interface) overview