📄 resources5.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Mail Session Connections</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="Resources4.html" /> <link rel="Next" href="Resources6.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="Resources4.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Resources6.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp79827"> </a><h2 class="pHeading1">Mail Session Connections</h2><a name="wp80750"> </a><p class="pBody">If you've ever ordered a product from a Web site, you've probably received an e-mail confirming your order. The <code class="cCode">ConfirmerBean</code> class demonstrates how to send e-mail from an enterprise bean. </p><a name="wp79830"> </a><p class="pBody">The source code for this example is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><<code class="cVariable">INSTALL</code>>/j2eetutorial14/ejb/confirmer/src/<a name="wp80952"> </a></pre></div><a name="wp79832"> </a><p class="pBody">In the <code class="cCode">sendNotice</code> method of the <code class="cCode">ConfirmerBean</code> class, the <code class="cCode">lookup</code> method returns a <code class="cCode">Session</code> object, 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 (<code class="cCode">mail/TheMailSession</code>) with a JNDI name. Using the <code class="cCode">Session</code> object as an argument, the <code class="cCode">sendNotice</code> method creates an empty <code class="cCode">Message</code> object. After calling several <code class="cCode">set</code> methods on the <code class="cCode">Message</code> object, <code class="cCode">sendNotice</code> invokes the <code class="cCode">send</code> method of the <code class="cCode">Transport</code> class to send the message on its way. The source code for the <code class="cCode">sendNotice</code> method follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void sendNotice(String recipient) { try { Context initial = new InitialContext(); Session session = (Session) initial.lookup( "java:comp/env/mail/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()); }}<a name="wp79834"> </a></pre></div><a name="wp79835"> </a><h3 class="pHeading2">Running the ConfirmerEJB Example</h3><a name="wp80910"> </a><h4 class="pHeading3">Setting Up the JavaMail Resource</h4><a name="wp80911"> </a><p class="pBody">To create a JavaMail resource in the J2EE Application Server with the Admin Console, follow these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81802"> </a><div class="pSmartList1"><li>Open the URL <code class="cCode">http://localhost:4848/asadmin</code> in a browser.</li></div><a name="wp81803"> </a><div class="pSmartList1"><li>Select the Java Mail Sessions node.</li></div><a name="wp81805"> </a><div class="pSmartList1"><li>Click New.</li></div><a name="wp81806"> </a><div class="pSmartList1"><li>Type <code class="cCode">mail/MySession</code> for the JNDI Name.</li></div><a name="wp81842"> </a><div class="pSmartList1"><li>Type the name of the host running your mail server for the Mail Host.</li></div><a name="wp81849"> </a><div class="pSmartList1"><li>Type the destination email address for Default User.</li></div><a name="wp81850"> </a><div class="pSmartList1"><li>Type your email address for the Default Return Address.</li></div><a name="wp81857"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp81858"> </a><div class="pSmartList1"><li>Note that <code class="cCode">mail/MySession</code> is listed under the Java Mail Sessions node.</li></div></ol></div><a name="wp79836"> </a><h4 class="pHeading3">Deploying the Application</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80884"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, open the <code class="cCode">ConfirmerApp.ear</code> file, which resides in this directory:</li></div><a name="wp81109"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/provided-ears/</code></p><a name="wp81114"> </a><div class="pSmartList1"><li>Verify the resource reference.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81120"> </a><div class="pSmartList2"><li>In the tree, expand the <code class="cCode">ConfirmerApp</code> node.</li></div><a name="wp81121"> </a><div class="pSmartList2"><li>Select the <code class="cCode">ConfirmerEJB</code> node.</li></div><a name="wp81122"> </a><div class="pSmartList2"><li>Select the Resource Refs tab.</li></div><a name="wp81123"> </a><div class="pSmartList2"><li>Note the JavaMail resource reference for<code class="cCode"> mail/TheMailSession</code>.</li></div></ol></div><a name="wp81124"> </a><div class="pSmartList1"><li>Verify the mapping of the reference to the JNDI name.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81132"> </a><div class="pSmartList2"><li>In the tree, select the ConfirmerApp node.</li></div><a name="wp81133"> </a><div class="pSmartList2"><li>Select the JNDI Names tab.</li></div><a name="wp81134"> </a><div class="pSmartList2"><li>Note the mapping of <code class="cCode">mail/TheMailSession</code> (coded in <code class="cCode">ConfirmerBean.java</code>) to <code class="cCode">mail/MySession</code> (specified in <code class="cCode">build.properties</code>).</li></div></ol></div><a name="wp81117"> </a><div class="pSmartList1"><li>Deploy the <code class="cCode">ConfirmerApp</code> Application.</li></div><a name="wp81110"> </a><div class="pSmartList1"><li>In the Deploy Module dialog box, do the following: </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81111"> </a><div class="pSmartList2"><li>Select the Return Client JAR checkbox. </li></div><a name="wp80902"> </a><div class="pSmartList2"><li>In the field below the check box, enter the following:</li></div><a name="wp80903"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/confirmer</code></p></ol></div></ol></div><a name="wp79878"> </a><h4 class="pHeading3">Running the Client</h4><a name="wp80799"> </a><p class="pBody">To run the <code class="cCode">SavingsAccountClient</code> program, do the following:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80800"> </a><div class="pSmartList1"><li>In a terminal window, go to this directory: </li></div><a name="wp80801"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/confirmer/</code></p><a name="wp80802"> </a><div class="pSmartList1"><li>Type the following command on a single line:</li></div><a name="wp80803"> </a><p class="pBodyRelative"><code class="cCode">appclient -client ConfirmerAppClient.jar </code><code class="cVariable">your_email_address</code></p><a name="wp80804"> </a><div class="pSmartList1"><li>The client should display the following lines:</li></div><a name="wp80805"> </a><p class="pBodyRelative"><code class="cCode">...<br />Sending email to . . .<br />...</code></p></ol></div><a name="wp80794"> </a><p class="pBody">To modify this example, see the instructions in <a href="EJB11.html#wp81442">Modifying the J2EE Application</a></p> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="Resources4.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Resources6.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -