diff --git a/net-pop3/ChangeLog b/net-pop3/ChangeLog new file mode 100755 index 00000000..ff58cf62 --- /dev/null +++ b/net-pop3/ChangeLog @@ -0,0 +1,6 @@ +2006-07-14 Zac Bowling + src/POP3Connect.cs: Patch from Lars Brubaker + +2005-03-19 Zac Bowling + + * * : Intial commit of Mono.Net.POP3 diff --git a/net-pop3/MIT.X11 b/net-pop3/MIT.X11 new file mode 100755 index 00000000..065fd4d6 --- /dev/null +++ b/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. diff --git a/net-pop3/Makefile b/net-pop3/Makefile new file mode 100755 index 00000000..ef529e7e --- /dev/null +++ b/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 diff --git a/net-pop3/Mono.Net.Pop3.snk b/net-pop3/Mono.Net.Pop3.snk new file mode 100755 index 00000000..c1c32749 Binary files /dev/null and b/net-pop3/Mono.Net.Pop3.snk differ diff --git a/net-pop3/README b/net-pop3/README new file mode 100755 index 00000000..ab1816c5 --- /dev/null +++ b/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. +> mono .exe diff --git a/net-pop3/samples/GetLastMessage.cs b/net-pop3/samples/GetLastMessage.cs new file mode 100755 index 00000000..5c87ae9e --- /dev/null +++ b/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(); + + } +} +} + diff --git a/net-pop3/samples/LastMessageHeaders.cs b/net-pop3/samples/LastMessageHeaders.cs new file mode 100755 index 00000000..d20dd6aa --- /dev/null +++ b/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(); + + } +} +} + diff --git a/net-pop3/samples/ListIDs.cs b/net-pop3/samples/ListIDs.cs new file mode 100755 index 00000000..a45b1c08 --- /dev/null +++ b/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(); + + } +} +} + diff --git a/net-pop3/samples/ListSubjects.cs b/net-pop3/samples/ListSubjects.cs new file mode 100755 index 00000000..2740db4d --- /dev/null +++ b/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(); + + } +} +} + diff --git a/net-pop3/src/AssemblyInfo.cs b/net-pop3/src/AssemblyInfo.cs new file mode 100755 index 00000000..f02d7b72 --- /dev/null +++ b/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")] diff --git a/net-pop3/src/POP3Connection.cs b/net-pop3/src/POP3Connection.cs new file mode 100755 index 00000000..751bfc20 --- /dev/null +++ b/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); + } + + /// + ///read header of the message + /// + 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