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

73 lines
4.1 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>Example: JavaMail code</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="jmlexcode"></a>Example: JavaMail code</h5>
<p>The following code illustrates how an application component sends a message and saves it to the mail account's Sent folder:</p>
<pre>
...
// obtain JNDI initial context
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
// use JNDI lookup to obtain Session
mail_session = (javax.mail.Session) ctx.lookup
(&quot;java:comp/env/mail/MailSession&quot;);
// create new mail message object using Session
MimeMessage msg = new MimeMessage(mail_session);
// set message properties
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse
(&quot;somebody@some_domain.net&quot;));
msg.setFrom(new InternetAddress(&quot;me@my_company.com&quot;));
msg.setSubject(&quot;Important message from me&quot;);
String msg_text = new String(&quot;Hello!&quot;);
msg.setText(msg_text);
// get Session object's store
Store store = mail_session.getStore();
// connect to store
store.connect();
// obtain reference to &quot;Sent&quot; folder
Folder f = store.getFolder(&quot;Sent&quot;);
// create &quot;Sent&quot; folder if it does not exist
if (!f.exists()) f.create(Folder.HOLDS_MESSAGES);
// add message to &quot;Sent&quot; folder
f.appendMessages(new Message[] {msg});
</pre>
<ul>
<li><p>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 the sample code above, 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></li>
<li><p>You must define a resource reference for the mail resource in the deployment descriptor of the component, because a mail session is referenced in the JNDI lookup.</p>
<p>When you create this reference, Make sure that the name of the reference matches the name used in the code. For example, the code above uses <tt>java:comp/env/mail/MailSession3</tt> in the lookup; therefore, the name of this reference must be <tt>mail/Session3</tt> and the type of the resource must be <tt>javax.mail.Session</tt>. After being defined, the deployment descriptor contains the following entry for the mail resource reference:</p>
<pre>&lt;resource-reference&gt;
&lt;description&gt;description&lt;/description&gt;
&lt;res-ref-name&gt;mail/MailSession3&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;
</pre>
</li>
<li><p>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></li>
</ul>
<p><strong>Note:</strong> The preceding example uses the IMAP protocol for receiving e-mail messages. On the iSeries platform, it is supported by the Domino e-mail server, but not the TCP/IP Connectivity Utilities e-mail services, which only provides access to received messages through the POP3 protocol.</p>
</body>
</html>