The following code illustrates how an application component sends a message and saves it to the mail account's Sent folder:
... // 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});
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.
In the sample code above, the line javax.mail.Session mail_session = (javax.mail.Session) ctx.lookup("java:comp/env/mail/MailSession3"); is an example of using a resource name located through JNDI rather than hard-coding a mail session. You can consider the lookup name mail/MailSession3 a soft link to the real resource.
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.
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 java:comp/env/mail/MailSession3 in the lookup; therefore, the name of this reference must be mail/Session3 and the type of the resource must be javax.mail.Session. After being defined, the deployment descriptor contains the following entry for the mail resource reference:
<resource-reference> <description>description</description> <res-ref-name>mail/MailSession3</res-ref-name> <res-type>javax.mail.Session</res-type> <res-auth>Container</res-auth>
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).
Note: 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.