1 22 package org.jboss.resource.adapter.mail.inflow; 23 24 import java.util.Properties ; 25 import javax.mail.Session ; 26 import javax.mail.Store ; 27 import javax.mail.Folder ; 28 import javax.mail.MessagingException ; 29 import javax.mail.Message ; 30 31 37 public class MailFolder 38 { 39 private Session session; 40 private Store store; 41 private Folder folder; 42 private String mailServer; 43 private String folderName; 44 private String userName; 45 private String password; 46 private Properties sessionProps; 47 48 public MailFolder(MailActivationSpec spec) 49 throws Exception 50 { 51 mailServer = spec.getMailServer(); 52 String protocol = spec.getStoreProtocol(); 53 folderName = spec.getMailFolder(); 54 userName = spec.getUserName(); 55 password = spec.getPassword(); 56 57 sessionProps = new Properties (); 58 sessionProps.setProperty("mail.transport.protocol", "smtp"); 59 sessionProps.setProperty("mail.store.protocol", protocol); 60 sessionProps.setProperty("mail.smtp.host", mailServer); 61 } 62 63 public void open() 64 throws Exception 65 { 66 session = Session.getDefaultInstance(sessionProps); 68 store = session.getStore(); 70 store.connect(mailServer, userName, password); 71 folder = store.getFolder(folderName); 72 73 if (folder == null || (!this.folder.exists())) 74 { 75 MessagingException e = new MessagingException ("Failed to find folder: " + folderName); 76 throw e; 77 } 78 79 folder.open(Folder.READ_WRITE); 80 } 81 82 public Message [] getNewMessages() 83 throws Exception 84 { 85 Message msgs[] = {}; 86 90 if( folder.hasNewMessages() ) 91 { 92 int newCount = folder.getNewMessageCount(); 93 int msgCount = folder.getMessageCount(); 94 msgs = folder.getMessages(msgCount - newCount + 1, msgCount); 95 return msgs; 96 } 97 return msgs; 98 } 99 100 public void close() throws MessagingException 101 { 102 this.folder.close(true); 103 this.store.close(); 104 this.folder = null; 105 this.store = null; 106 this.session = null; 107 } 108 109 } 110 | Popular Tags |