1 / 27

Java Mail (Part 2)

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.

dominy
Download Presentation

Java Mail (Part 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Mail (Part 2) Presented By Lalitha Mazimdar March 12, 2001

  2. Topics javax.mail.Store javax.mail.Folder javax.mail.search.* Packages revisited History Summary Future

  3. Javax.mail.Store & javax.mail.Folder The store class models the message database and the access protocol. Hierarchical storage structure

  4. 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)

  5. Retrieve a Folder Folder getDefaultFolder() Folder getFolder(String name)

  6. Code to retrieve the Store and Folder /****** Store is retrieved ******/ Store store = session.getStore(); /**** Folder INBOX is retrieved ****/ Folder inbox = store.getDefaultFolder().getFolder(“INBOX”);

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

  8. Retrieve a Message Messages retrieved from Folder by: getMessage(int index) search(SearchTerm term) Lightweight Message objects

  9. 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);

  10. 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

  11. Flags Describe the state of a Message within its Folder System Flags and user defined flags

  12. Javax.mail.search.* BodyTerm(String pattern) FromTerm(Address address) HeaderTerm(String headername,String pattern) ReceivedDateTerm(int comparison, Date date)

  13. JavaMail keeps it Simple Creating a mail message Creating a session object Sending a mail message Retrieving a message Executing a high level command

  14. Revisiting Packages Session Message Transport Store Folder

  15. Start with the session The registry allows: Service providers to install providers Clients to use a specific provider

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

  17. Create a singlepart message Create a multipart message Display the single part messages.

  18. Display the multi part messages.

  19. Transport the message using static method like Transport.send(message) Clients can explicitly instantiate Transport objects.

  20. 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 !!!

  21. 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

  22. 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”

  23. 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.

  24. Thank You !!!

  25. 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”); }

  26. 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); }

  27. 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()); }

More Related