ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/jmlwrite.htm

69 lines
3.0 KiB
HTML
Raw 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 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. To use the JavaMail APIs in an enterprise application component, such as a servlet, perform these steps:</p>
<ol>
<li><p>Use the WebSphere Development Studio Client to declare mail resource references in your application. For more information, see the WebSphere Development Studio Client Help.</p></li>
<li><p>Configure each JavaMail session object used by your Web component. This is done during the process of deploying the component.</p></li>
<li><p>Use a JNDI lookup to get a reference to a Session object.</p>
<p>See <a href="jndilkup.htm#JavaMail">Example: Look up a JavaMail session</a> for more information.</p>
</li>
</ol>
<p><strong>Example: JavaMail code</strong></p>
<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>
<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>