1 17 package org.apache.servicemix.components.email; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.messaging.DeliveryChannel; 21 import javax.jbi.messaging.InOnly; 22 import javax.jbi.messaging.MessageExchangeFactory; 23 import javax.jbi.messaging.NormalizedMessage; 24 import javax.mail.Flags ; 25 import javax.mail.Folder ; 26 import javax.mail.Session ; 27 import javax.mail.Store ; 28 import javax.mail.internet.MimeMessage ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.servicemix.components.util.PollingComponentSupport; 33 34 41 public class MimeMailPoller extends PollingComponentSupport { 42 43 private static Log log = LogFactory.getLog(MimeMailPoller.class); 44 45 private Session session; 46 private String hostName; 47 private String userName; 48 private String password; 49 private String mailBox; 50 private boolean debug; 51 private int maxFetchSize = 5; 52 private MimeMailMarshaler marshaler = new MimeMailMarshaler(); 53 54 protected void init() throws JBIException { 55 super.init(); 56 if (session == null) { 57 log.debug("No Session informed. Using default instance"); 58 this.session = Session.getDefaultInstance(System.getProperties()); 59 } 60 if (mailBox == null) { 61 log.debug("No mailbox informed. Using INBOX"); 62 mailBox = "INBOX"; 63 } 64 if (hostName == null) { 65 throw new JBIException("HostName not informed"); 66 } 67 if (userName == null) { 68 throw new JBIException("UserName not informed"); 69 } 70 if (password == null) { 71 throw new JBIException("Password not informed"); 72 } 73 if (maxFetchSize < 1) { 74 throw new JBIException("Fetch Size must be at least 1"); 75 } 76 77 } 78 79 82 public String getHostName() { 83 return hostName; 84 } 85 86 89 public void setHostName(String hostName) { 90 this.hostName = hostName; 91 } 92 93 96 public MimeMailMarshaler getMarshaler() { 97 return marshaler; 98 } 99 100 103 public void setMarshaler(MimeMailMarshaler marshaler) { 104 this.marshaler = marshaler; 105 } 106 107 public void poll() throws Exception { 108 Store store = null; 109 Folder folder = null; 110 try { 111 session.setDebug(isDebug()); 112 store = session.getStore((mailBox.equals("INBOX")) ? "pop3" 113 : "imap"); 114 store.connect(hostName, userName, password); 115 folder = store.getFolder(mailBox); 116 if (folder == null || !folder.exists()) { 117 throw new Exception ("Folder not found or invalid: " + mailBox); 118 } 119 folder.open(Folder.READ_WRITE); 120 int msgCount = Math.min(folder.getMessageCount(),maxFetchSize); 121 DeliveryChannel channel = getDeliveryChannel(); 122 MessageExchangeFactory mef = getExchangeFactory(); 123 for(int i=1; i <= msgCount;i++) { 124 MimeMessage mailMsg = (MimeMessage ) folder.getMessage(i); 125 InOnly io = mef.createInOnlyExchange(); 126 NormalizedMessage normalizedMessage = io.createMessage(); 127 this.marshaler.prepareExchange(io,normalizedMessage,mailMsg); 128 io.setInMessage(normalizedMessage); 129 channel.send(io); 130 mailMsg.setFlag(Flags.Flag.DELETED,true); 131 } 132 } finally { 133 try { 134 if (folder != null) { 135 folder.close(true); 136 } 137 if (store != null) { 138 store.close(); 139 } 140 } catch (Exception ignored) {} 141 } 142 } 143 144 147 public boolean isDebug() { 148 return debug; 149 } 150 151 155 public void setDebug(boolean debug) { 156 this.debug = debug; 157 } 158 159 162 public String getMailBox() { 163 return mailBox; 164 } 165 166 170 public void setMailBox(String mailBox) { 171 this.mailBox = mailBox; 172 } 173 174 177 public String getPassword() { 178 return password; 179 } 180 181 185 public void setPassword(String password) { 186 this.password = password; 187 } 188 189 192 public String getUserName() { 193 return userName; 194 } 195 196 200 public void setUserName(String userName) { 201 this.userName = userName; 202 } 203 204 207 public Session getSession() { 208 return session; 209 } 210 211 215 public void setSession(Session session) { 216 this.session = session; 217 } 218 219 222 public int getMaxFetchSize() { 223 return maxFetchSize; 224 } 225 226 229 public void setMaxFetchSize(int maxFetchSize) { 230 this.maxFetchSize = maxFetchSize; 231 } 232 233 } 234 | Popular Tags |