|
Home TOC Index |
|
Search
Feedback |
Mail Session Connections
If you've ever ordered a product from a Web site, you've probably received an e-mail confirming your order. The
ConfirmerBeanclass demonstrates how to send e-mail from an enterprise bean.The source code for this example is in the
j2eetutorial/examples/src/ejb/confirmerdirectory. To compile the code, go to thej2eetutorial/examplesdirectory and typeantconfirmer. A sampleConfirmerApp.earfile is in the j2eetutorial/examples/earsdirectory.In the
sendNoticemethod of theConfirmerBeanclass, thelookupmethod returns aSessionobject, which represents a mail session. Like a database connection, a mail session is a resource. As with any resource, you must link the coded name (TheMailSession) with a JNDI name. Using theSessionobject as an argument, thesendNoticemethod creates an emptyMessageobject. After calling severalsetmethods on theMessageobject,sendNoticeinvokes thesendmethod of theTransportclass to send the message on its way. The source code for thesendNoticemethod follows.public void sendNotice(String recipient) { try { Context initial = new InitialContext(); Session session = (Session) initial.lookup( "java:comp/env/TheMailSession"); Message msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false)); msg.setSubject("Test Message from ConfirmerBean"); DateFormat dateFormatter = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.SHORT); Date timeStamp = new Date(); String messageText = "Thank you for your order." + '\n' + "We received your order on " + dateFormatter.format(timeStamp) + "."; msg.setText(messageText); msg.setHeader("X-Mailer", mailer); msg.setSentDate(timeStamp); Transport.send(msg); } catch(Exception e) { throw new EJBException(e.getMessage()); } }Running the ConfirmerEJB Example
Deploying the Application
- In
deploytool, open thej2eetutorial/examples/ears/ConfirmerApp.earfile (FileOpen).
- In the Resource Refs tab of the bean, specify the resource reference for the mail session with the values shown in Table 16-1.
- Deploy the
SavingsAccountAppapplication (ToolsDeploy). In the Introduction dialog box, make sure that you select the Return Client JAR checkbox.
Running the Client
- In a terminal window, go to the
j2eetutorial/examples/earsdirectory.- Set the
APPCPATHenvironment variable toConfirmerAppClient.jar.- Type the following command on a single line, replacing
<recipient>with the e-mail address of the person who will receive the message.runclient -client ConfirmerApp.ear -name ConfirmerClient -textauth <recipient>- At the login prompts, enter
guestfor the user name andguest123for the password.Troubleshooting
If the application cannot connect to the mail server it will generate this exception:
javax.mail.MessagingException: Could not connect to SMTP hostTo fix this problem, make sure that the mail server is running and that you've entered the correct name for the mail server host in the Resource Refs tab of the
deploytool.
|
Home TOC Index |
|
Search
Feedback |