ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/jmlwrite.htm

100 lines
7.0 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Write JavaMail applications</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h4><a name="jmlwrite"></a>Write JavaMail applications</h4>
<p>After you have configured e-mail services on your iSeries server and created a JavaMail Session object with the WebSphere Application Server administrative console, you can write applications that interface with these services through the Session object. According to the J2EE specification, each instance of the javax.mail.Session class is treated as a JNDI resource factory.</p>
<p>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.</p>
<p>To use the JavaMail APIs in an enterprise application component, such as a servlet, enterprise bean, or application client, perform the following steps:</p>
<p>
<ol>
<li><p><strong>Locate a resource through Java Naming and Directory Interface (JNDI).</strong>
<br>The J2EE specification considers a mail session instance as a resource (a factory where mail transport and store connections can be obtained). You should never hard-code mail sessions. Instead, you must follow the J2EE programming model of configuring resources through the system facilities and then locating them through JNDI lookups.</p>
<p>In <a href="jmlexcode.htm">Example: JavaMail code</a>, the line <tt>javax.mail.Session mail_session = (javax.mail.Session) ctx.lookup(&quot;java:comp/env/mail/MailSession3&quot;);</tt> is an example of using a resource name located through JNDI rather than hard-coding a mail session. You can consider the lookup name <tt>mail/MailSession3</tt> a soft link to the real resource.</p>
<p>See <a href="jndi.htm">Java Naming and Directory Interface (JNDI)</a> for more information.</p>
<li><p><strong>Use the Assembly Toolkit to declare mail resource references in your application.</strong></p>
<p>
<ol type="a">
<li>Start the Assembly Toolkit.</li>
<li>In the J2EE perspective, expand <strong>Web Modules</strong>, and right click the Web module you want to modify.</li>
<li>Select <strong>Open With --> Deployment Descriptor Editor</strong>.</li>
<li>In the <strong>Web Deployment Descriptor</strong>, click the <strong>References</strong> tab.</li>
<li>On the <strong>References</strong> page, click the <strong>Resource</strong> tab at the top of the page.</li>
<li>Under the <strong>Resource references</strong> heading, click <strong>Add</strong> to declare mail resource references. Fill in the following information:
<ul>
<li><strong>Name</strong><br>
This is a required field. The <strong>Name</strong> field specifies the JNDI name of the resource relative to the java:comp/env context. Make sure that the name of the reference matches the name used in the code. For example, in <a href="jmlexcode.htm">Example: JavaMail code</a>, it uses <tt>java:comp/env/mail/MailSession3</tt> in the lookup; therefore, the name of this reference must be <tt>mail/Session3</tt>.</li>
<li><strong>Type</strong><br>
This is a required field. The <strong>Type</strong> field specifies the type of the resource. Enter <tt>javax.mail.Session</tt> in the field.</li>
<li><strong>Description</strong><br>
This is an optional field. The <strong>Description</strong> field specifies a description for the resource.</li>
<li><strong>Authentication</strong><br>
The type of authentication to use for the resource. Select <strong>Container</strong> from the drop-down list.</li>
</ul>
</li>
<li>Click File --> Save.
</ol>
<p><strong>Note: </strong>Use the steps above to edit additional enterprise application components as needed.</p>
<p>The tool generates a deployment descriptor that has XML tags similar to those in the following example:</p>
<pre>
&lt;resource-ref&gt;
&lt;description&gt;description&lt;/description&gt;
&lt;res-ref-name&gt;mail/MailSession&lt;/res-ref-name&gt;
&lt;res-type&gt;javax.mail.Session&lt;/res-type&gt;
&lt;res-auth&gt;Container&lt;/res-auth&gt;
&lt;/resource-ref&gt;
</pre>
</li>
<li><p><strong><a href="../admin/appsvrmail.htm">Administer mail resources</a></strong>
<br>You must configure the mail resource that is referenced by your application component. The mail session you configure must have both the transport and mail access portions defined. Transport settings are required because the code is sending a message; mail access settings are required because they also save a copy to the local mail store. When you configure the mail session, you are asked to specify a JNDI name. This name is important and is required when you install your application (to link up the resource references in your application with the real resources that you have configured).</p>
<p>See <a href="jndilkup.htm#JavaMail">Example: Look up a JavaMail session</a> for more information.</p>
</li>
<li><p><strong><a href="../admin/acappinst.htm">Install your application</a></strong> in the <em>Administration</em> topic.
<br>You can install your application using either the administrative console or the scripting tool. An important step in the install process is that the system goes through all resource references and expects you to supply a JNDI name for each of them. This is not an arbitrary JNDI name; it is the JNDI name given to a particular, configured resource that is the target of the reference.</p></li>
<li><p>(Optional) <strong>Manage existing mail providers and sessions</strong>.
<p>
<ol type="a">
<li><a href="../admin/acstart.htm">Start the administrative console</a> in the <em>Administration</em> topic.</li>
<li>In the topology tree, expand <strong>Resources</strong> and click <strong>Mail Providers --> Mail session</strong>.</li>
<li>Click the mail provider or mail session that you want to modify.</li>
<li>To remove a mail provider or mail session, click <strong>Remove</strong>.</li>
<li>Click <strong>Apply</strong> or <strong>OK</strong>.</li>
<li>Save the configuration.</li>
</ol>
</p>
</li>
<li><p>(Optional) <strong>Enable debugger for a mail session</strong>.
<p>
<ol type="a">
<li><a href="../admin/acstart.htm">Start the administrative console</a> in the <em>Administration</em> topic.</li>
<li>In the topology tree, expand <strong>Resources</strong> and click <strong>Mail Providers --> Mail session</strong>.</li>
<li>Click <strong>Debug</strong>. Debug is enabled for the session.</li>
<li>Click <strong>Apply</strong> or <strong>OK</strong>.</li>
</ol>
</li>
</ol>
</p>
</body>
</html>