InnerhalbNach weiteren Dokumenten suchenSupport-Ressourcen | Dieses Buch im PDF-Format herunterladen (2033 KB)
Chapter 19 Using the JavaMail APIThis chapter describes how to use the JavaMailTM API, which provides a set of abstract classes defining objects that comprise a mail system. This chapter contains the following sections: Introducing JavaMailThe JavaMail API defines classes such as Message, Store, and Transport. The API can be extended and can be subclassed to provide new protocols and to add functionality when necessary. In addition, the API provides concrete subclasses of the abstract classes. These subclasses, including MimeMessage and MimeBodyPart, implement widely used Internet mail protocols and conform to the RFC822 and RFC2045 specifications. The JavaMail API includes support for the IMAP4, POP3, and SMTP protocols. The JavaMail architectural components are as follows:
For more information, see Chapter 5, Configuring JavaMail Resources, in Sun Java System Application Server 9.1 Administration Guide and the JavaMail specification at http://java.sun.com/products/javamail/. A useful JavaMail tutorial is located at http://java.sun.com/developer/onlineTraining/JavaMail/. Creating a JavaMail SessionYou can create a JavaMail session in the following ways:
JavaMail Session PropertiesYou can set properties for a JavaMail Session object. Every property name must start with a mail- prefix. The Application Server changes the dash (-) character to a period (.) in the name of the property and saves the property to the MailConfiguration and JavaMail Session objects. If the name of the property doesn’t start with mail-, the property is ignored. For example, if you want to define the property mail.from in a JavaMail Session object, first define the property as follows:
Looking Up a JavaMail SessionThe standard Java Naming and Directory Interface (JNDI) subcontext for JavaMail sessions is java:comp/env/mail. Registering JavaMail sessions in the mail naming subcontext of a JNDI namespace, or in one of its child subcontexts, is standard. The JNDI namespace is hierarchical, like a file system’s directory structure, so it is easy to find and nest references. A JavaMail session is bound to a logical JNDI name. The name identifies a subcontext, mail, of the root context, and a logical name. To change the JavaMail session, you can change its entry in the JNDI namespace without having to modify the application. The resource lookup in the application code looks like this: InitialContext ic = new InitialContext(); String snName = "java:comp/env/mail/MyMailSession"; Session session = (Session)ic.lookup(snName); For more information about the JNDI API, see Chapter 17, Using the Java Naming and Directory Interface. Sending and Reading Messages Using JavaMailThe following sections describe how to send and read messages using the JavaMail API:
|