Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move to old-code

svn path=/old-code/; revision=156225
  • Loading branch information...
commit 5910f165ac8793a36c4c2243675f58d49a23daa1 1 parent d27dce8
@migueldeicaza migueldeicaza authored
View
6 net-pop3/ChangeLog
@@ -0,0 +1,6 @@
+2006-07-14 Zac Bowling <zac@zacbowling.com>
+ src/POP3Connect.cs: Patch from Lars Brubaker
+
+2005-03-19 Zac Bowling <zac@zacbowling.com>
+
+ * * : Intial commit of Mono.Net.POP3
View
21 net-pop3/MIT.X11
@@ -0,0 +1,21 @@
+Copyright (c) 2005 Zac Bowling, Ximian, Inc and the individuals listed
+on the ChangeLog entries.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
15 net-pop3/Makefile
@@ -0,0 +1,15 @@
+CSC=mcs
+#PROGFILES=`cygpath \`cygpath -m -s "$(PROGRAMFILES)"\``
+#CSC=$(PROGFILES)/Mono-1.1.3/bin/mcs.bat /d:WIN32
+#ROOT=/cygdrive/$(subst \,/,$(subst :\,/,$(SYSTEMROOT)))
+#CSC=$(ROOT)/microsoft.net/framework/v1.1.4322/csc.exe /d:WIN32 /optimize+
+
+default:
+ $(CSC) /target:library ./src/POP3Connection.cs ./src/POP3Message.cs ./src/AssemblyInfo.cs /out:Mono.Net.POP3.dll
+ $(CSC) /target:exe ./samples/GetLastMessage.cs /r:Mono.Net.POP3.dll /out:./GetLastMessage.exe
+ $(CSC) /target:exe ./samples/ListSubjects.cs /r:Mono.Net.POP3.dll /out:./ListSubjects.exe
+ $(CSC) /target:exe ./samples/ListIDs.cs /r:Mono.Net.POP3.dll /out:./ListIDs.exe
+ $(CSC) /target:exe ./samples/LastMessageHeaders.cs /r:Mono.Net.POP3.dll /out:./LastMessageHeaders.exe
+
+clean:
+ rm Mono.Net.POP3.dll GetLastMessage.exe ListIDs.exe ListSubjects.exe LastMessageHeaders.exe
View
BIN  net-pop3/Mono.Net.Pop3.snk
Binary file not shown
View
24 net-pop3/README
@@ -0,0 +1,24 @@
+Mono.Net.POP3
+Copyright 2005(C) Zac Bowling
+zac@zacbowling.com
+http://zacbowling.com
+
+Licenced under the terms of the MIT X11 licence.
+Read the MIT.X11 for more information.
+
+FEATURES:
+* List support
+* Top support with full message fall back.
+* Decodes headers and messages for you.
+* Delete support
+* Lots of nice helper functions
+* On-demand connection ablity
+* Really simpile and lightweight
+* Uses nothing more then System.Net
+
+KNOWN ISSUE:
+* None so far :-)
+
+SAMPLES:
+
+All of the samples are in the following syntax.
View
31 net-pop3/samples/GetLastMessage.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Net;
+using Mono.Net.POP3;
+
+namespace Mono.Net.POP3.Samples
+{
+public class Test1
+{
+ public static void Main(string[] args)
+ {
+ POP3Connection mails = new POP3Connection(
+ args[0], args[1], args[2]);
+ mails.Open();
+ short messcount = mails.MessageCount();
+ Console.WriteLine("MESSAGES COUNT: {0}",messcount);
+
+ short[] q = mails.List();
+ POP3Message msg = mails.Retr(q[q.GetUpperBound(0)]);
+
+ Console.WriteLine("MESSAGE TO:\t {0}", msg.To);
+ Console.WriteLine("MESSAGE FROM:\t {0}", msg.From);
+ Console.WriteLine("MESSAGE SUBJECT: {0}", msg.Subject);
+ Console.WriteLine("MESSAGE DATE:\t {0}", msg.Date);
+ Console.WriteLine("--- MESSAGE ---\n {0}", msg.Message);
+
+ mails.Close();
+
+ }
+}
+}
+
View
29 net-pop3/samples/LastMessageHeaders.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Net;
+using Mono.Net.POP3;
+using System.Collections;
+using System.Collections.Specialized;
+
+namespace Mono.Net.POP3.Samples
+{
+public class Test1
+{
+ public static void Main(string[] args)
+ {
+ POP3Connection mails = new POP3Connection(
+ args[0], args[1], args[2]);
+ mails.Open();
+ short messcount = mails.MessageCount();
+ Console.WriteLine("MESSAGES COUNT: {0}",messcount);
+
+ short[] q = mails.List();
+ POP3Message msg = mails.Retr(q[q.GetUpperBound(0)]);
+ foreach ( DictionaryEntry de in msg.Headers )
+ Console.WriteLine( "\n***********\n{0}\n-----------\n{1}", de.Key, de.Value );
+
+ mails.Close();
+
+ }
+}
+}
+
View
27 net-pop3/samples/ListIDs.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Net;
+using Mono.Net.POP3;
+
+namespace Mono.Net.POP3.Samples
+{
+public class Test2
+{
+ public static void Main(string[] args)
+ {
+ POP3Connection mails = new POP3Connection(
+ args[0], args[1], args[2]);
+
+ mails.Open();
+ int messcount = mails.MessageCount();
+ Console.WriteLine("MESSAGES COUNT: {0}",messcount);
+
+ foreach (short s in mails.List())
+ Console.WriteLine(s);
+
+
+ mails.Close();
+
+ }
+}
+}
+
View
30 net-pop3/samples/ListSubjects.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Net;
+using Mono.Net.POP3;
+
+namespace Mono.Net.POP3.Samples
+{
+public class Test2
+{
+ public static void Main(string[] args)
+ {
+ POP3Connection mails = new POP3Connection(
+ args[0], args[1], args[2]);
+
+ mails.Open();
+ int messcount = mails.MessageCount();
+ Console.WriteLine("MESSAGES COUNT: {0}",messcount);
+
+ POP3Message[] msgs = mails.GetMessageRange(messcount-21,messcount-1,false);
+
+ foreach (POP3Message msg in msgs)
+ {
+ Console.WriteLine("{0} \n{1} \n{2} \n\n", msg.From.Trim(), msg.Subject.Trim(), msg.Date.Trim());
+ }
+
+ mails.Close();
+
+ }
+}
+}
+
View
6 net-pop3/src/AssemblyInfo.cs
@@ -0,0 +1,6 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+[assembly:AssemblyVersion("1.0.0.0")]
+[assembly:AssemblyDelaySign(false)]
+[assembly:AssemblyKeyFile("Mono.Net.Pop3.snk")]
View
396 net-pop3/src/POP3Connection.cs
@@ -0,0 +1,396 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Collections;
+
+namespace Mono.Net.POP3
+{
+ public class POP3Connection
+ {
+ private TcpClient mailclient = null;
+ private NetworkStream ns = null;
+ protected StreamReader BaseReader = null;
+ protected StreamWriter BaseWriter = null;
+
+ private bool connected = false;
+ private int port = 110;
+ private string server = "";
+ private string username = "";
+ private string password = "";
+
+
+ public POP3Connection()
+ {
+ //Maybe something can go here?
+ }
+
+ public POP3Connection(string sUsername, string sPassword, string sServer)
+ : this (sUsername,sPassword,sServer,110)
+ {
+ }
+
+ public POP3Connection(string sUsername, string sPassword, string sServer, int sPort)
+ {
+ username = sUsername;
+ password = sPassword;
+ server = sServer;
+ port = sPort;
+ }
+
+ public bool Connected()
+ {
+ bool retval;
+ if ( mailclient != null && connected != false )
+ retval = true;
+ else
+ retval = false;
+ return retval;
+ }
+
+ public string Username
+ {
+ set
+ {
+ if (!Connected())
+ username = value;
+ else
+ throw new System.MethodAccessException();
+ }
+ get
+ {
+ return username;
+ }
+ }
+
+ public string Password
+ {
+ set
+ {
+ if (!Connected())
+ password = value;
+ else
+ throw new System.MethodAccessException();
+ }
+ get
+ {
+ return password;
+ }
+ }
+
+ public string Server
+ {
+ set
+ {
+ if (!Connected())
+ server = value;
+ else
+ throw new System.MethodAccessException();
+ }
+ get
+ {
+ return server;
+ }
+ }
+
+ public int Port
+ {
+ set
+ {
+ if (!Connected())
+ port = value;
+ else
+ throw new System.MethodAccessException();
+ }
+ get
+ {
+ return port;
+ }
+ }
+
+
+ public void Open()
+ {
+ string response;
+ mailclient = new TcpClient(server, port); //Blocking and may throw exception also
+
+ ns = mailclient.GetStream(); // Lock and load :-)
+ BaseReader = new StreamReader(ns);
+ BaseWriter = new StreamWriter(ns);
+
+ BaseReader.ReadLine(); //Ingore the POP3 opening banner
+
+ BaseWriter.WriteLine("User " + username); //Send username;
+ BaseWriter.Flush();
+
+ response = BaseReader.ReadLine();
+ if (response.Substring(0,1) == "-")
+ throw new System.UnauthorizedAccessException();
+
+ BaseWriter.WriteLine("Pass " + password); //Send password;
+ BaseWriter.Flush();
+
+ response = BaseReader.ReadLine();
+ if (response.Substring(0,1) == "-")
+ throw new System.UnauthorizedAccessException();
+
+
+ connected = true;
+ }
+
+ public void Close()
+ {
+ BaseWriter.WriteLine("quit");
+ BaseWriter.Flush();
+ ns.Close();
+ }
+
+ public short MessageCount()
+ {
+ string response;
+ bool disconnect = false;
+
+ //If we are not connected then connect and disconnect when done
+ if (!Connected()){
+ disconnect = true;
+ Open();
+ }
+
+ //Send stat command to get number of messages
+ BaseWriter.WriteLine("stat");
+ BaseWriter.Flush();
+
+ response = BaseReader.ReadLine();
+ string[] nummess = response.Split(' ');
+ short totmessages;
+ totmessages = Convert.ToInt16(nummess[1]);
+
+ if (disconnect)
+ Close();
+
+ return totmessages;
+ }
+
+ public POP3Message Top(short msgID)
+ {
+ return this.Top(msgID, 0);
+ }
+
+ ///<description>
+ ///read header of the message
+ ///</description>
+ public POP3Message Top(short msgID,int lines)
+ {
+ string response;
+
+ BaseWriter.WriteLine("top " + msgID.ToString() + " " + lines.ToString());
+ BaseWriter.Flush();
+ StringBuilder sb = new StringBuilder();
+ response = BaseReader.ReadLine();
+ if ( response.StartsWith("-") )
+ return null;
+ else {
+ //sb.Append(response);
+ while ((response = BaseReader.ReadLine()).Trim() !=".")
+ sb.Append(response + "\n");
+ return new POP3Message(sb.ToString());
+ }
+ }
+
+ public POP3Message Retr(short msgID)
+ {
+ string response;
+
+ BaseWriter.WriteLine("retr " + msgID.ToString());
+ BaseWriter.Flush();
+ StringBuilder sb = new StringBuilder();
+ response = BaseReader.ReadLine();
+ if ( response.StartsWith("-") )
+ return null;
+ else {
+ while ((response = BaseReader.ReadLine()).Trim() !=".")
+ sb.Append(response + "\n");
+ return new POP3Message(sb.ToString());
+ }
+
+ }
+
+
+ public String RetrRaw(short msgID)
+ {
+ string response;
+
+ BaseWriter.WriteLine("retr " + msgID.ToString());
+ BaseWriter.Flush();
+ StringBuilder sb = new StringBuilder();
+ response = BaseReader.ReadLine();
+ if (response.StartsWith("-"))
+ {
+ return null;
+ }
+ else
+ {
+ while ((response = BaseReader.ReadLine()).Trim() != ".")
+ {
+ sb.Append(response + "\n");
+ }
+ return sb.ToString();
+ }
+ }
+
+
+ public bool Delete (short msgID)
+ {
+ string response;
+
+ BaseWriter.WriteLine("dele " + msgID.ToString());
+ BaseWriter.Flush();
+
+ response = BaseReader.ReadLine();
+ return response.StartsWith("-");
+ }
+
+ public POP3Message FirstMessage()
+ {
+ Int16[] q = this.List();
+ return Retr((short) q[q.GetLowerBound(0)]);
+ }
+
+ public POP3Message LastMessage()
+ {
+ Int16[] q = this.List();
+ return Retr((short) q[q.GetUpperBound(0)]);
+ }
+
+ public POP3Message[] GetAllMessages( bool delete )
+ {
+ Int16[] q = this.List();
+ return GetMessageRange( q.GetLowerBound(0), q.GetUpperBound(0), delete );
+ }
+
+ public POP3Message[] GetHeaderRange( int min, int max, bool delete )
+ {
+ return GetMessageRange(min,max,delete,true);
+ }
+
+ public POP3Message[] GetMessageRange( int min, int max, bool delete )
+ {
+ return GetMessageRange(min,max,delete,false);
+ }
+
+ public POP3Message[] GetMessageRange( int min, int max, bool delete, bool headerOnly )
+ {
+ //string response;
+ bool disconnect = false;
+
+ //If we are not connected then connect and disconnect when done
+ if (!Connected()){
+ disconnect = true;
+ Open();
+ }
+ Int16[] q = this.List();
+ ArrayList msgBuff = new ArrayList();
+ POP3Message msg;
+ for (int i = min; i<max; i++)
+ {
+ try{
+ if (headerOnly) msg = this.Top(q[i]);
+ else msg = this.Retr(q[i]);
+ msgBuff.Add(msg);
+ if (delete) Delete((short)q[i]);
+ }
+ catch{
+ // this is bad but we are going to ignore errors for missing message now until list support
+ }
+ }
+
+ if (disconnect)
+ Close();
+
+ return (POP3Message[]) msgBuff.ToArray(typeof(POP3Message));
+ }
+
+ public short[] List()
+ {
+ ArrayList list = new ArrayList();
+ string response;
+
+ BaseWriter.WriteLine("list");
+ BaseWriter.Flush();
+ response = BaseReader.ReadLine();
+ if ( response.StartsWith("-") )
+ return new short[] {}; //This is better then return an error
+ else {
+ //sb.Append(response);
+ while ((response = BaseReader.ReadLine()).Trim() !=".")
+ list.Add(Convert.ToInt16(response.Split(' ')[0]));
+ return (short[]) list.ToArray(typeof(short));
+ }
+
+ }
+
+ public static short MessageCount(string sUsername,
+ string sPassword,
+ string sServer)
+ {
+ return MessageCount(sUsername, sPassword, sServer, 110);
+ }
+
+
+ public static short MessageCount(string sUsername,
+ string sPassword,
+ string sServer,
+ int sPort)
+ {
+ POP3Connection conn = new POP3Connection(sUsername, sPassword, sServer, sPort);
+ return conn.MessageCount();
+ }
+
+ public static POP3Message[] GetHeaderRange(string sUsername,
+ string sPassword,
+ string sServer,
+ int min, int max)
+ {
+ return GetMessageRange(sUsername, sPassword, sServer, 110, min, max, false, true);
+ }
+
+ public static POP3Message[] GetMessageRange(string sUsername,
+ string sPassword,
+ string sServer,
+ int min, int max, bool delete)
+ {
+ return GetMessageRange(sUsername, sPassword, sServer, 110, min, max, delete, false);
+ }
+
+
+ public static POP3Message[] GetMessageRange(string sUsername,
+ string sPassword,
+ string sServer,
+ int sPort,
+ int min, int max, bool delete, bool HeaderOnly)
+ {
+ POP3Connection conn = new POP3Connection(sUsername, sPassword, sServer, sPort);
+ return conn.GetMessageRange(min,max,delete,HeaderOnly);
+ }
+
+ public static POP3Message[] GetAllMessages(string sUsername,
+ string sPassword,
+ string sServer,
+ bool delete)
+ {
+ return GetAllMessages(sUsername, sPassword, sServer, 110, delete);
+ }
+
+ public static POP3Message[] GetAllMessages(string sUsername,
+ string sPassword,
+ string sServer,
+ int sPort,
+ bool delete)
+ {
+ POP3Connection conn = new POP3Connection(sUsername, sPassword, sServer, sPort);
+ return conn.GetAllMessages(delete);
+ }
+ }
+
+}
View
99 net-pop3/src/POP3Message.cs
@@ -0,0 +1,99 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Collections;
+using System.Collections.Specialized;
+
+namespace Mono.Net.POP3
+{
+ public class POP3Message
+ {
+ public readonly string To = "";
+ public readonly string CC = "";
+ public readonly string From = "";
+ public readonly string ReplyTo = "";
+ public readonly string Subject = "";
+ public readonly string ContentType = "";
+ public readonly string Status = "";
+ public readonly string Date;
+ public readonly StringDictionary Headers = new StringDictionary();
+ public readonly string Message = "";
+
+ public POP3Message(string rawMsg)
+ {
+#if DEBUG
+ Console.WriteLine("Length: {0}", rawMsg.Length);
+ Console.WriteLine("POP3Message creation");
+ Console.Write("..........\n{0}", rawMsg);
+#endif
+
+ StringReader reader = new StringReader(rawMsg);
+ string currentKey = "";
+ string currentItem = "";
+
+ while ( true )
+ {
+ string hline = reader.ReadLine();
+ if ( hline.StartsWith("\t") || hline.StartsWith(" ")){
+ currentItem += "\n" + hline.Trim();
+ }
+ else {
+ if (currentItem != "")
+ {
+ switch(currentKey.ToLower()){
+ case "to":
+ if (this.To.Length == 0)
+ this.To = currentItem;
+ else
+ this.To += "\n" + currentItem;
+ break;
+ case "from":
+ if (this.From.Length == 0)
+ this.From = currentItem;
+ else
+ this.From += "\n" + currentItem;
+ break;
+ case "reply-to":
+ case "replyto":
+ if (this.ReplyTo.Length == 0)
+ this.ReplyTo = currentItem;
+ else
+ this.ReplyTo += "\n" + currentItem;
+ break;
+ case "cc":
+ if (this.CC.Length == 0)
+ this.CC = currentItem;
+ else
+ this.CC += "\n" + currentItem;
+ break;
+ case "subject":
+ this.Subject = currentItem;
+ break;
+ case "content-type":
+ case "contenttype":
+ this.ContentType = currentItem;
+ break;
+ case "date":
+ this.Date = currentItem;
+ break;
+ };
+ if (Headers.ContainsKey(currentKey))
+ Headers[currentKey] += "\n\n" + currentItem;
+ else
+ Headers.Add(currentKey,currentItem);
+ }
+ if (hline.Trim() == "" || hline.Trim() == ".")
+ break;
+ string[] items = hline.Split(new Char[] {':'},2);
+ currentKey = items[0];
+ currentItem = items[1];
+ }
+ if (hline.Trim() == "" || hline.Trim() == ".")
+ break;
+ }
+ this.Message = reader.ReadToEnd();
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.