JavaMail JavaMail Classes Sending a Message Using JavaMail (MessageSend.java) Sending a Message to Multiple Recipients (SendToMany.java) Installing JavaMail
Protocols Message Store Protocols : Read messages from a server –Internet Message Access Protocol (IMAP) –Post Office Protocol (POP) Message Transport Protocols : Send messages to a server (i.e. Simple Mail Transfer Protocol (SMTP)) Multipurpose Internet Mail Extensions (MIME) : Standard for describing messages
Classes to Send Address: Abstract class representing any address Message: Abstract message class Header: Collection of attribute fields for a message Content: Object representing a part of a message (i.e. text or an attachment) Transport: Object representing a transport protocol that allows messages to be sent, implemented by a specific protocol Session: Provides an interface between the client and the network, supporting the creation of and access to Store and Transport objects
Specific Implementations InternetAddress: Object representing an address MimeMessage: Object representing a specific message
Store: Object representing database and access protocol for storing and accessing messages and folders, implemented by a specific protocol Folder: Contains messages and subfolders and supports reading and deleting them MailEvent: Can be registered with event listeners to catch events generated by Transport, Store, and Folder objects (i.e. announce arrival of new ) Other JavaMail Classes
Java.util.Properties Class Extends HashMap Designed to contain a persistent set of properties that may be saved to or loaded from a stream All keys and values must be Strings Although it supports HashMap methods for handling Objects, use of the following is recommended to ensure that it contains Strings: –public Object setProperty(String key, String value) –public String getProperty(String key)
Example 1: MessageSend.java Sends an address from one person to another import java.io.*; import java.net.InetAddress; import java.util.Properties; import java.util.Date; import javax.mail.*; import javax.mail.internet.*; address class Properties class will be discussed Directory containing abstract mail classes Internet classes
createSession() public Session createSession() { Properties p = System.getProperties(); p.setProperty("mail.transport.protocol", "smtp"); p.setProperty("mail.smtp.host","andrew.cmu.edu"); Gets the default system properties Sets the transport protocol to SMTP and sets the appropriate SMTP host for CMU
p.setProperty("mail.store.protocol","imap"); p.setProperty("mail.imap.host","cyrus.andrew.cmu.edu"); Session sess = Session.getDefaultInstance(p); return sess; } Instantiates a session using the new properties object Sets the store protocol to IMAP and sets the appropriate SMTP host for CMU (not really needed unless the application will read )
createMessage() public Message createMessage(Session sess) throws MessagingException{ Message mess = new MimeMessage(sess); Base exception class for Internet mail Default Constructor for a MimeMessage takes a session object
mess.setFrom(new mess.setRecipients(Message.RecipientType.TO, false)); mess.setSubject("Test"); mess.setText("This is a test of JavaMail's functionality."); mess.setSentDate(new Date()); return mess; } 2 Methods to create an InternetAddress: Constructor that takes a String: InternetAddress(String address) static InternetAddress parse(String listOfAddresses, boolean strict) (A false indicates the addresses may be space or comma delimited) setRecipients(MessageRecipientType type, String address) MessageRecipientType may be TO, CC, or BCC
main() public static void main(String[] args) { MessageSend send = new MessageSend(); Session sess = send.createSession(); try { Message mess = send.createMessage(sess); Transport.send(mess); } catch(MessagingException e) { System.out.println("Messaging Exception: "+e); } A static method of the Transport class saves and sends a message
Example 2:MessageSendToMany Sends a message to a group of addresses import java.io.*; import java.net.InetAddress; import java.util.Properties; import java.util.Date; import javax.mail.*; import javax.mail.internet.*; public class MessageSendToEach { public Session createSession() { Properties p = System.getProperties(); p.setProperty("mail.transport.protocol", "smtp"); p.setProperty("mail.smtp.host","andrew.cmu.edu"); p.setProperty("mail.store.protocol","imap"); p.setProperty("mail.imap.host","cyrus.andrew.cmu.edu"); Session sess = Session.getDefaultInstance(p); return sess; } Almost everything is the same
createMessage() public Message createMessage(Session sess)throws MessagingException, UnsupportedEncodingException { Message mess = new MimeMessage(sess); InternetAddress[] recip = new InternetAddress[6]; InternetAddress[] reply = new InternetAddress[1]; reply [0] = new “Danielle Medvan”); Note the additional exception being thrown Another constructor for an InternetAddress: InternetAddress(String address, String identifyingName) This throws an UnsupportedEncodingException if the software does not support the character encoding in which the name is provided
recip[0]= new "Gary"); recip[1]= new “Martin"); recip[2]= new “Rebecca"); recip[3]= new “Mark"); recip[4]= new “Gina"); recip[5]= new “Cameron"); mess.setFrom(new
mess.setReplyTo(reply); mess.setRecipients(Message.RecipientType.TO,recip); mess.setSubject("Test"); mess.setText("This is a test of JavaMail's functionality."); mess.setSentDate(new Date()); return mess; } The “reply-to” address is set with setReplyTo(Address[] addresses) We previously saw a method to set a single recipient. That method is overloaded. Now we see setRecipients(MessageRecipientType type, Address[] addresses)
Installation Instructions 1.Download the JavaMail API Implementation Version 1.2 at 2.Download the JavaBeans Application Framework (JAF) at: 3.Unzip both files. 4.Add the following files to your classpath: –mail.jar (JavaMail) –activation.jar (JAF file)