[Schmitzm-commits] r2200 - trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Sat Jan 26 18:34:11 CET 2013
Author: mojays
Date: 2013-01-26 18:34:11 +0100 (Sat, 26 Jan 2013)
New Revision: 2200
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Pop3Client.java
Log:
new class Pop3Client
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Pop3Client.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Pop3Client.java (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Pop3Client.java 2013-01-26 17:34:11 UTC (rev 2200)
@@ -0,0 +1,202 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.net.mail;
+
+import java.util.Properties;
+
+import javax.mail.Flags.Flag;
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.NoSuchProviderException;
+import javax.mail.Session;
+import javax.mail.Store;
+
+/**
+ * Client to fetch mails from a POP3 Server.
+ * @author Martin O.J. Schmitz
+ */
+public class Pop3Client {
+
+ private String server;
+ private int port;
+
+ protected Store pop3store = null;
+ protected Folder inboxFolder = null;
+
+
+ /**
+ * Creates a new POP3 client with default port.
+ * @param server name of POP3 server
+ */
+ public Pop3Client(String server) {
+ this(server,-1);
+ }
+
+ /**
+ * Creates a new POP3 client.
+ * @param server name of POP3 server
+ * @param port port of POP3 service (-1 for default)
+ */
+ public Pop3Client(String server, int port) {
+ this.server = server;
+ this.port = port;
+ }
+
+ /**
+ * Returns the POP3 server name.
+ */
+ public String getServer() {
+ return server;
+ }
+
+ /**
+ * Returns the POP3 server port.
+ * @return -1 if default port is configured
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /**
+ * Connects to POP3 server and opens the "inbox" folder in read-only mode.
+ * @param user user to establish the connection for
+ * @param pw password used for authentication
+ * @param mode indicates whether to open folder {@link Folder#READ_ONLY} or {@link Folder#READ_WRITE}
+ */
+ public void connect(String user, String pw) throws MessagingException {
+ connect(user, pw, Folder.READ_ONLY);
+ }
+ /**
+ * Connects to POP3 server and opens the "inbox" folder.
+ * @param user user to establish the connection for
+ * @param pw password used for authentication
+ * @param mode indicates whether to open folder {@link Folder#READ_ONLY} or {@link Folder#READ_WRITE}
+ */
+ public void connect(String user, String pw, int mode) throws MessagingException {
+ try {
+ // Get system properties
+ Properties properties = System.getProperties();
+ // Get the default Session object.
+ Session session = Session.getDefaultInstance(properties);
+ // Get a Store object that implements the specified protocol.
+ pop3store = session.getStore("pop3");
+ // Connect to the host using the specified username and password.
+ pop3store.connect(getServer(), getPort(), user, pw);
+
+ // Create a Folder object corresponding to the given name.
+ inboxFolder = pop3store.getFolder("inbox");
+ // Open the Folder.
+ inboxFolder.open(mode);
+ } catch (MessagingException err) {
+ disconnect();
+ throw err;
+ }
+ }
+
+ /**
+ * Closes the "inbox" foldes and the disconnects from server.
+ * If "inbox" folder was opened in read-write mode, messages which were marked as
+ * {@link Flag#DELETED} are deleted.
+ * */
+ public void disconnect() throws MessagingException {
+ if ( inboxFolder != null && inboxFolder.isOpen() )
+ inboxFolder.close(true);
+ if ( pop3store != null && pop3store.isConnected() )
+ pop3store.close();
+
+ inboxFolder = null;
+ pop3store = null;
+ }
+
+ /**
+ * Checks whether connection to POP3 server is established and
+ * "inbox" folder is opened.
+ */
+ public boolean isConnected() {
+ return inboxFolder != null && inboxFolder.isOpen()
+ && pop3store != null && pop3store.isConnected();
+ }
+
+ /**
+ * Returns the total number of messages in "inbox" folder.
+ */
+ public int getMessageCount() throws MessagingException {
+ if (!isConnected())
+ throw new UnsupportedOperationException("POP3 server is not connected!");
+ return inboxFolder.getMessageCount();
+ }
+
+ /**
+ * Fetches mails from inbox folder.
+ * @param firstMessNo number of first/oldest mail to retrieve (starting with 1!)
+ * @param lastMessNo number of last/newest mail to retrieve (if -1, all messages upto the newest
+ * will be retrieved)
+ */
+ public Message[] fetchMessages(int firstMessNo, int lastMessNo) throws MessagingException {
+ if (!isConnected())
+ throw new UnsupportedOperationException("POP3 server is not connected!");
+ if ( firstMessNo <= 0 )
+ firstMessNo = 1;
+ if ( lastMessNo <= 0 )
+ lastMessNo = getMessageCount();
+ return inboxFolder.getMessages(firstMessNo,lastMessNo);
+ }
+
+ /**
+ * Fetches the newest X mails from inbox folder.
+ * @param maxMessCount maximum number of mails that will be retrieved (if -1, all messages
+ * will be retrieved)
+ */
+ public Message[] fetchMessages(int maxMessCount) throws MessagingException {
+ // Get the messages from the server
+ Message[] messages = null;
+ if ( maxMessCount <= 0 )
+ messages = inboxFolder.getMessages();
+ else {
+ int messCount = inboxFolder.getMessageCount();
+ int firstMessNo = Math.max(messCount-maxMessCount+1, 1);
+ int lastMessNo = messCount;
+ messages = inboxFolder.getMessages(firstMessNo,lastMessNo);
+ }
+ return messages;
+ }
+
+ /**
+ * Returns the inbox folder.
+ * @return {@code null} if connection is closed.
+ */
+ public Folder getInboxFolder() {
+ return inboxFolder;
+ }
+
+}
+
+
More information about the Schmitzm-commits
mailing list