1 18 package org.apache.axis2.transport; 19 20 import org.apache.axis2.engine.AxisFault; 21 22 import javax.mail.*; 23 import java.util.Properties ; 24 25 31 public class EmailReceiver { 32 33 private String user; 34 private String host; 35 private String popPort; 36 private String password; 37 private Store store; 38 private Folder inbox; 39 40 public EmailReceiver(String user, String host, String popPort, String password) { 41 this.user = user; 42 this.host = host; 43 this.popPort = popPort; 44 this.password = password; 45 } 46 47 48 public void connect() throws AxisFault{ 49 try { 50 final PasswordAuthentication authentication = 51 new PasswordAuthentication(user, password); 52 Properties props = new Properties (); 53 props.put("mail.user", user); 54 props.put("mail.host", host); 55 props.put("mail.store.protocol", "pop3"); 56 props.put("mail.transport.protocol", "smtp"); 57 props.put("mail.pop3.port", popPort); 58 Session session = Session.getInstance(props, new Authenticator() { 59 protected PasswordAuthentication getPasswordAuthentication() { 60 return authentication; 61 } 62 }); 63 64 store = session.getStore(); 65 store.connect(); 66 Folder root = store.getDefaultFolder(); 67 inbox = root.getFolder("inbox"); 68 69 70 } catch (NoSuchProviderException e) { 71 throw new AxisFault(e); 72 } catch (MessagingException e) { 73 throw new AxisFault(e); 74 } 75 76 } 77 78 public void disconnect() throws AxisFault{ 79 try { 80 inbox.close(true); 81 store.close(); 82 } catch (MessagingException e) { 83 throw new AxisFault(e); 84 } 85 } 86 87 88 public Message[] receive() throws AxisFault { 89 try{ 90 inbox.open(Folder.READ_WRITE); 91 Message[] msgs = inbox.getMessages(); 92 93 int numMessages = msgs.length; 94 if (msgs.length == 0) { 95 return null; 96 } else { 97 return msgs; 98 } 99 100 } catch (NoSuchProviderException e) { 101 throw new AxisFault(e); 102 } catch (MessagingException e) { 103 throw new AxisFault(e); 104 } 105 } 106 107 } 108 | Popular Tags |