![]() |
Quadcap Embeddable Server |
00001 package com.quadcap.pop3.client; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Date; 00042 import java.util.Hashtable; 00043 import java.util.Vector; 00044 00045 import java.io.IOException; 00046 import java.io.InputStream; 00047 00048 import com.quadcap.util.Debug; 00049 00050 import com.quadcap.util.threads.Command; 00051 00052 /** 00053 * This simple Pop3 agent transfers all new messages in a remote Pop3 00054 * mailbox into the specified folder in the quadcap message store. 00055 * 00056 * <p>A more sophisticated agent would scan the headers first and apply 00057 * filters to certain messages without having to fetch the bodies. 00058 * 00059 * @author Stan Bailes 00060 */ 00061 public class Agent implements Runnable { 00062 String host; 00063 String user; 00064 String password; 00065 Session pop3 = null; 00066 00067 /** 00068 * Create a new Agent with the specified profile name 00069 * 00070 * @param profileName the name of the profile. 00071 */ 00072 public Agent(String host, String user, String password) { 00073 this.host = host; 00074 this.user = user; 00075 this.password = password; 00076 } 00077 00078 /** 00079 * The Command callback object which invokes this agent. 00080 */ 00081 public void run() { 00082 Debug.println("[" + user + "] begin"); 00083 try { 00084 if (pop3 == null) pop3 = new Session(host, 110); 00085 pop3.connect(); 00086 00087 if (pop3.user(user) != Session.OK) { 00088 throw new IOException("user failed: " + user); 00089 } 00090 if (pop3.pass(password) != Session.OK) { 00091 throw new IOException("password failed: " + user); 00092 } 00093 getAndDeleteMail(); 00094 } catch (IOException e) { 00095 Debug.print(e); 00096 00097 } finally { 00098 try { 00099 pop3.quit(); 00100 } catch (IOException e) { 00101 Debug.print(e); 00102 } 00103 // ---- we're about to "go to sleep", potentially for a long time, 00104 // ---- so allow these objects to be gc'ed. 00105 pop3 = null; 00106 } 00107 Debug.println("[" + user + "] done"); 00108 } 00109 00110 /** 00111 * The simple <i>delete from server</i> case. Get all of the mail, 00112 * put it in the right folder, then delete it from the server. 00113 */ 00114 void getAndDeleteMail() throws IOException { 00115 Vector r = pop3.stat(); 00116 int cnt = Integer.parseInt(r.elementAt(1).toString()); 00117 byte[] buf = new byte[16384]; 00118 for (int i = 1; i <= cnt; i++) { 00119 Debug.println("[" + user + "] getting message " + i); 00120 InputStream is = pop3.retr(i); 00121 if (is != null) { 00122 int xcnt = 0; 00123 while ((xcnt = is.read(buf)) > 0) continue; 00124 is.close(); 00125 } 00126 pop3.dele(i); 00127 } 00128 Debug.println("[" + user + "] deleted " + cnt + " messages"); 00129 } 00130 00131 public static void main(String[] args) { 00132 while (true) { 00133 for (int i = 0; i < 100; i++) { 00134 Agent agent = new Agent("nt1", "stan"+i, "stan"+i); 00135 agent.run(); 00136 } 00137 } 00138 } 00139 } 00140