Example: JavaMail code

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});

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.