001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.transport.mail;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import javax.mail.Authenticator;
026: import javax.mail.Flags;
027: import javax.mail.Folder;
028: import javax.mail.Message;
029: import javax.mail.MessagingException;
030: import javax.mail.PasswordAuthentication;
031: import javax.mail.Session;
032: import javax.mail.Store;
033: import javax.mail.Transport;
034: import javax.mail.internet.MimeMessage;
035: import java.io.IOException;
036: import java.util.Properties;
037:
038: public class MailClient extends Authenticator {
039: public static final int SHOW_MESSAGES = 1;
040: public static final int CLEAR_MESSAGES = 2;
041: public static final int SHOW_AND_CLEAR = SHOW_MESSAGES
042: + CLEAR_MESSAGES;
043: private static final Log log = LogFactory.getLog(MailClient.class);
044: protected PasswordAuthentication authentication;
045: protected String from;
046: protected Session session;
047:
048: public MailClient(String user, String host) {
049: this (user, host, user, false);
050: }
051:
052: public MailClient(String user, String host, String password) {
053: this (user, host, password, false);
054: }
055:
056: public MailClient(String user, String host, String password,
057: boolean debug) {
058: from = user + '@' + host;
059: authentication = new PasswordAuthentication(user, password);
060:
061: Properties props = new Properties();
062:
063: props.put("mail.user", user);
064: props.put("mail.host", host);
065: props.put("mail.debug", debug ? "true" : "false");
066: props.put("mail.store.protocol", "pop3");
067: props.put("mail.transport.protocol", "smtp");
068: session = Session.getInstance(props, this );
069: }
070:
071: public int checkInbox(int mode) throws MessagingException,
072: IOException {
073: int numMessages = 0;
074:
075: if (mode == 0) {
076: return 0;
077: }
078:
079: boolean show = (mode & SHOW_MESSAGES) > 0;
080: boolean clear = (mode & CLEAR_MESSAGES) > 0;
081: String action = (show ? "Show" : "")
082: + ((show && clear) ? " and " : "")
083: + (clear ? "Clear" : "");
084:
085: log.info(action + " INBOX for " + from);
086:
087: Store store = session.getStore();
088:
089: store.connect();
090:
091: Folder root = store.getDefaultFolder();
092: Folder inbox = root.getFolder("inbox");
093:
094: inbox.open(Folder.READ_WRITE);
095:
096: Message[] msgs = inbox.getMessages();
097:
098: numMessages = msgs.length;
099:
100: if ((msgs.length == 0) && show) {
101: log.info("No messages in inbox");
102: }
103:
104: for (int i = 0; i < msgs.length; i++) {
105: MimeMessage msg = (MimeMessage) msgs[i];
106:
107: if (show) {
108: log.info(" From: " + msg.getFrom()[0]);
109: log.info(" Subject: " + msg.getSubject());
110: log.info(" Content: " + msg.getContent());
111: }
112:
113: if (clear) {
114: msg.setFlag(Flags.Flag.DELETED, true);
115: }
116: }
117:
118: inbox.close(true);
119: store.close();
120:
121: return numMessages;
122: }
123:
124: public void sendMessage(String to, String subject, String content,
125: String soapAction) throws MessagingException {
126: log.info("SENDING message from " + from + " to " + to);
127:
128: MimeMessage msg = new MimeMessage(session);
129:
130: msg.setHeader("transport.mail.soapaction", soapAction);
131: msg.addRecipients(Message.RecipientType.TO, to);
132: msg.setSubject(subject);
133: msg.setText(content);
134: Transport.send(msg);
135: }
136:
137: public PasswordAuthentication getPasswordAuthentication() {
138: return authentication;
139: }
140: }
|