69 lines
3.0 KiB
HTML
69 lines
3.0 KiB
HTML
<!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
|
|
("java:comp/env/mail/MailSession");
|
|
|
|
// create new mail message object using Session
|
|
MimeMessage msg = new MimeMessage(mail_session);
|
|
|
|
// set message properties
|
|
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse
|
|
("somebody@some_domain.net"));
|
|
msg.setFrom(new InternetAddress("me@my_company.com"));
|
|
msg.setSubject("Important message from me");
|
|
String msg_text = new String("Hello!");
|
|
msg.setText(msg_text);
|
|
|
|
// get Session object's store
|
|
Store store = mail_session.getStore();
|
|
// connect to store
|
|
store.connect();
|
|
// obtain reference to "Sent" folder
|
|
Folder f = store.getFolder("Sent");
|
|
// create "Sent" folder if it does not exist
|
|
if (!f.exists()) f.create(Folder.HOLDS_MESSAGES);
|
|
// add message to "Sent" 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>
|