KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > EmailReceiver


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Runtime state of the engine
17  */

18 package org.apache.axis2.transport;
19
20 import org.apache.axis2.engine.AxisFault;
21
22 import javax.mail.*;
23 import java.util.Properties JavaDoc;
24
25 /**
26  * @author hemapani
27  *
28  * To change the template for this generated type comment go to
29  * Window>Preferences>Java>Code Generation>Code and Comments
30  */

31 public class EmailReceiver {
32
33     private String JavaDoc user;
34     private String JavaDoc host;
35     private String JavaDoc popPort;
36     private String JavaDoc password;
37     private Store store;
38     private Folder inbox;
39
40     public EmailReceiver(String JavaDoc user, String JavaDoc host, String JavaDoc popPort, String JavaDoc 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 JavaDoc props = new Properties JavaDoc();
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