270 likes | 428 Views
Java Mail (Part 2). Presented By Lalitha Mazimdar March 12, 2001. Topics. javax.mail.Store javax.mail.Folder javax.mail.search.* Packages revisited History Summary Future. Javax.mail.Store & javax.mail.Folder.
E N D
Java Mail (Part 2) Presented By Lalitha Mazimdar March 12, 2001
Topics javax.mail.Store javax.mail.Folder javax.mail.search.* Packages revisited History Summary Future
Javax.mail.Store & javax.mail.Folder The store class models the message database and the access protocol. Hierarchical storage structure
Retrieve a Store Just like Transport, there are 5 different ways of retrieving a Store (or Folder) object via the Session. Store getStore() Store getStore(String Protocol) Store getStore(URLName url) Store getStore(Provider provider) Folder getFolder(URLName url)
Retrieve a Folder Folder getDefaultFolder() Folder getFolder(String name)
Code to retrieve the Store and Folder /****** Store is retrieved ******/ Store store = session.getStore(); /**** Folder INBOX is retrieved ****/ Folder inbox = store.getDefaultFolder().getFolder(“INBOX”);
Some Methods in class Folder boolean exists() boolean create(int type) void open(int mode) void close(boolean expunge) boolean renameTo(Folder folder) boolean delete(boolean recurse)
Retrieve a Message Messages retrieved from Folder by: getMessage(int index) search(SearchTerm term) Lightweight Message objects
Code to Retrieve a Message void fetchMessages(String filename) { Store store = session.getStore(“imap”); store.connect(); Folder folder = store.getDefaultFolder().getFolder(“INBOX”); folder.open(Folder.READ_ONLY);
int count = folder.getMessageCount(); for(int i = 0; i <= count; i++) { Message msg = folder.getMessage(i); displayMessage(msg); } folder.close(false); store.close(); } //Method ends here
Flags Describe the state of a Message within its Folder System Flags and user defined flags
Javax.mail.search.* BodyTerm(String pattern) FromTerm(Address address) HeaderTerm(String headername,String pattern) ReceivedDateTerm(int comparison, Date date)
JavaMail keeps it Simple Creating a mail message Creating a session object Sending a mail message Retrieving a message Executing a high level command
Revisiting Packages Session Message Transport Store Folder
Start with the session The registry allows: Service providers to install providers Clients to use a specific provider
Create a session object with this information. Create a store object Connect to the store using a username password authentication Create a folder object and retrieve the defaultfolder and the folders beneath the defaultfolder.
Create a singlepart message Create a multipart message Display the single part messages.
Transport the message using static method like Transport.send(message) Clients can explicitly instantiate Transport objects.
Create a class XYZ implements HttpSessionBindingListener This class will have all the code that we just discussed. Create a JSP that will use this class XYZ and is also used to control the look and feel of the mail handling system. !!! That is it !!!
History Initial prototype started in late 1996 JavaMail API design started in early 1997 Public review in late 1997 JavaMail Specification 1.0 FCS on Dec23, 1997 JavaMail implementation 1.0 FCS April1, 1998 JavaMail 1.0.1 and 1.0.2 in early 1998 JavaMail 1.1 Specification and Implementation FCS August 1998 Current version is 1.1.3
Summary Mail-enable applications easily Allow complex, innovative uses of Mail technology Fits well with existing standards, protocols and implementation Allow for future ideas “Simple things are easy and sophisticated things are possible”
Future A future version of the JavaMail API will provide support for digitally signed and encrypted messages. Security features will rely heavily upon Java 1.2 security features.
Creating a single part message void createMessage(Session session) { MimeMessage msg = new MimeMessage(session) msg.setFrom(); msg.setSubject(“The JavaMail API”); InternetAddress a = new InternetAddress(“abc@def.org”); msg.setRecipient(Message.RecipientType.To, new InternetAddress(“xyz@wrox.com”); msg.setText(“check out the javamail”); }
Creating a multi part message void createMultipartMessage(Session session) { MimeMessage msg = new MimeMessage(session) //set headers as before MimeBodyPart bp1 = new MimeBodyPart(bp1.setText(“text in part1 “); MimeBodyPart bp2 = new MimeBodyPart(bp2.setText(“text in part2 “); MimeMultipart mp = new MimiMultipart(); mp.addBodyPart(bp1); mp.addBodyPart(bp2); msg.setContent(mp); }
Creating a session Properties prop = System.getProperties(); prop.put(“mail.transport.protocol”, “smtp”); prop.put(“mail.smtp.host”, “mail.your-isp.com”); session = Session.getInstance(prop, null); //return an array of installed store protocols Provider[] prov = session.getProviders(); List storeProtocols = new ArrayList(); for(int I=0; I<prov.length; ++I) { if(prov[I].getType == Provider.Type.STORE) storeProtocols.add(prov[I].getProtocol()); }