Chapter 30 Creating JavaMail


Writing JavaMail for EAServer

You can implement JavaMail for EAServer as you would for any other server that follows the JavaMail specification. JavaMail for EAServer can be coded to the standard JavaMail API and uses classes in the javax.mail and javax.mail.internet packages. It also uses the J2EE Java Naming and Directory Interface (JNDI) to resolve the JavaMail session used in the operating environment in which the JavaMail program is deployed.

Creating a JavaMail session

The javax.mail.Session object is responsible for managing a user's mail configuration settings and handling authentication for the individual transports used during the session.

To create platform-independent applications, a JavaMail program can use a resource factory reference to obtain a JavaMail session. A resource factory is an object that provides access to specific resources within a program's deployed environment using the specific naming conventions defined by JNDI. All resource factory references are organized by resource type in the application's component environment. For example, JavaMail resource factory references are found in java:comp/env/mail. For more information on using resource factory references, see:

To obtain an initial JNDI naming context for your JavaMail session, create an instance of the javax.naming.InitialContext object. Then call the lookup method to invoke the javax.mail.Session factory reference to obtain a JavaMail session. This session will map to the local mail server as defined for the environment in which your JavaMail program is deployed. See "Deploying a JavaMail-enabled Web application" for information on specifying your local resources.

Constructing a message

Message is an abstract class in the JavaMail API. Subclasses of Message implement the concrete functionality needed for specific messaging systems. The JavaMail reference implementation includes a MimeMessage class that implements the standard for basic Internet messages and the Multipurpose Internet Mail Extensions.

To construct a message, instantiate a MimeMessage object, set the required attributes (headers), and provide the appropriate header values and body content. At a minimum, you should specify the From, To, and Date headers.

Use the setFrom method to set the From header field using the value of InternetAddress. Use the setRecipients method to set the specified recipient type to a given address. Use the setSentDate method to set the date.

Sending a message

You use the Transport class to send a message. If you create a JavaMail session that uses the SMTP provider included with EAServer, you can simply use the Transport.send method to send your completed message to all the recipient addresses specified.

Sample EAServer JavaMail program

In this example, an e-mail message is sent to the user of a Web-based travel reservation system confirming the user's reservation.

public String mailIt
(java.lang.String from,
java.lang.String to,
java.lang.String subject,
java.lang.String textmessage)
{
String status = "Your message was sent";
try {

//Obtain the initial JNDI context
InitialContext ctx = new InitialContext();

//Perform a JNDI lookup to obtain the resource
//reference object
Session session = (Session) ctx.lookup
("java:comp/env/mail/mymailserver");

//Construct the message
MimeMessage message = new MimeMessage(session);

//Set the from address
Address[] fromAddress =
InternetAddress.parse(from);
message.addFrom(fromAddress);

//Set the to address
Address[] toAddress = InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO,
toAddress);

//Set the subject and text
message.setSubject(subject);
message.setText(textmessage);

//Send the message
Transport.send(message);

} catch(AddressException e) {
status = "There was an error parsing the addresses"+e;
} catch(SendFailedException e) {
status = "There was an error sending the message"+e;
} catch (MessagingException e) {
status = "There was an unexpected error"+e;
} catch (NamingException e) {
status = "The mail session could not be created.";
}
System.out.println("The status is:"+ status);
return status;
}

JavaMail providers

JavaMail is extensible which means that when new protocols are developed, providers for those protocols can be added to a system and used by preexisting JavaMail enabled applications. Applications can detect which providers are available to them via the Provider Registry.

The providers that come with the JavaMail reference implementation are listed in javamail.default.providers. If you add a package containing a new provider, it should include a javamail.providers file in its META-INF directory.

To list the available providers on your system:

import javax.mail.*;
class ListProviders
{
public static void main(String[] args)
{
java.util.Properties properties =
System.getProperties();
Session session = Session.getInstance(properties,
null);
Provider[] providers = session.getProviders();
for (int i = 0; i < providers.length; ++i)
{
System.out.println(providers[i]);
}
}

 


Copyright © 2000 Sybase, Inc. All rights reserved.