01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.transport.mail;
21:
22: import org.apache.axis2.AxisFault;
23:
24: import javax.mail.Folder;
25: import javax.mail.Message;
26: import javax.mail.MessagingException;
27: import javax.mail.NoSuchProviderException;
28: import javax.mail.Session;
29: import javax.mail.Store;
30: import javax.mail.URLName;
31: import java.util.Properties;
32:
33: public class EmailReceiver {
34:
35: private URLName urlName;
36: private Properties pop3Properties;
37: private Folder folder;
38: /* This store could be either POP3Store or POP3SSLStore */
39: private Store store;
40:
41: public EmailReceiver() {
42: }
43:
44: public void setUrlName(URLName urlName) {
45: this .urlName = urlName;
46: }
47:
48: public void setPop3Properties(Properties pop3Properties) {
49: this .pop3Properties = pop3Properties;
50: }
51:
52: public void connect() throws AxisFault {
53: try {
54:
55: Session session = Session.getInstance(pop3Properties, null);
56: store = session.getStore(urlName);
57:
58: store.connect();
59:
60: folder = store.getDefaultFolder();
61:
62: folder = folder.getFolder("inbox");
63: } catch (NoSuchProviderException e) {
64: throw new AxisFault(e.getMessage(), e);
65: } catch (MessagingException e) {
66: throw new AxisFault(e.getMessage(), e);
67: }
68: }
69:
70: public void disconnect() throws AxisFault {
71: try {
72: folder.close(true);
73: store.close();
74: } catch (MessagingException e) {
75: throw new AxisFault(e.getMessage(), e);
76: }
77: }
78:
79: public Message[] receiveMessages() throws AxisFault {
80: try {
81: folder.open(Folder.READ_WRITE);
82:
83: Message[] msgs = folder.getMessages();
84:
85: if (msgs.length == 0) {
86: return null;
87: } else {
88: return msgs;
89: }
90: } catch (NoSuchProviderException e) {
91: throw new AxisFault(e.getMessage(), e);
92: } catch (MessagingException e) {
93: throw new AxisFault(e.getMessage(), e);
94: }
95: }
96: }
|