Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaMail JavaMail Classes Sending a Message Using JavaMail (MessageSend.java) Sending a Message to Multiple Recipients (SendToMany.java) Installing JavaMail.

Similar presentations


Presentation on theme: "JavaMail JavaMail Classes Sending a Message Using JavaMail (MessageSend.java) Sending a Message to Multiple Recipients (SendToMany.java) Installing JavaMail."— Presentation transcript:

1 JavaMail JavaMail Classes Sending a Message Using JavaMail (MessageSend.java) Sending a Message to Multiple Recipients (SendToMany.java) Installing JavaMail

2 E-mail 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 e-mail messages

3 Classes to Send E-mail Address: Abstract class representing any e-mail 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 e-mail messages to be sent, implemented by a specific protocol Session: Provides an interface between the e-mail client and the network, supporting the creation of and access to Store and Transport objects

4 Specific Implementations InternetAddress: Object representing an e-mail address MimeMessage: Object representing a specific e-mail message

5 Store: Object representing database and access protocol for storing and accessing messages and folders, implemented by a specific protocol Folder: Contains e-mail 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 e-mail) Other JavaMail Classes

6 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)

7 Example 1: MessageSend.java Sends an e-mail 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.*; E-mail address class Properties class will be discussed Directory containing abstract mail classes Internet e-mail classes

8 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

9 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 e-mail)

10 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

11 mess.setFrom(new InternetAddress("mm6@andrew.cmu.edu")); mess.setRecipients(Message.RecipientType.TO, InternetAddress.parse("bob@andrew.cmu.edu", 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

12 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

13 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

14 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 InternetAddress("dmedvan@andrew.cmu.edu“, “Danielle Medvan”); Note the additional exception being thrown Another constructor for an InternetAddress: InternetAddress(String address, String identifyingName) This throws an UnsupportedEncodingException if the e-mail software does not support the character encoding in which the name is provided

15 recip[0]= new InternetAddress("garya@andrew.cmu.edu", "Gary"); recip[1]= new InternetAddress(“ma3q@andrew.cmu.edu”, “Martin"); recip[2]= new InternetAddress(“romoff@andrew.cmu.edu”, “Rebecca"); recip[3]= new InternetAddress(“marks215@home.com”, “Mark"); recip[4]= new InternetAddress(“ginas@andrew.cmu.edu”, “Gina"); recip[5]= new InternetAddress(“cameronw@andrew.cmu.edu”, “Cameron"); mess.setFrom(new InternetAddress("mm6@andrew.cmu.edu"));

16 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)

17 Installation Instructions 1.Download the JavaMail API Implementation Version 1.2 at http://java.sun.com/products/javamail/index.html 2.Download the JavaBeans Application Framework (JAF) at: http://java.sun.com/products/javabeans/glasgow/jaf.html 3.Unzip both files. 4.Add the following files to your classpath: –mail.jar (JavaMail) –activation.jar (JAF file)


Download ppt "JavaMail JavaMail Classes Sending a Message Using JavaMail (MessageSend.java) Sending a Message to Multiple Recipients (SendToMany.java) Installing JavaMail."

Similar presentations


Ads by Google