// Author: Bill Farley // Date: 4/8/2001 // Time: 11:58:06 AM //Compile: csc /r:System.Net.dll /r:System.IO.dll /r:message.dll /target:library POP3.cs using System.Net.Sockets; using System.Collections; using System.IO; using System.Net; using System; public class POP3 { string POPServer; string user; string pwd; NetworkStream ns; StreamReader sr; public POP3(){} public POP3(string server, string _user, string _pwd) { POPServer = server; user = _user; pwd = _pwd; } private void Connect() { TCPClient sender = new TCPClient(POPServer,110); Byte[] outbytes; string input; try{ ns = sender.GetStream(); sr = new StreamReader(ns); sr.ReadLine(); //Console.WriteLine(sr.ReadLine() ); input = "user " + user + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); //Console.WriteLine(sr.ReadLine() ); input = "pass " + pwd + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); //Console.WriteLine(sr.ReadLine() ); } catch(InvalidOperationException ioe){ Console.WriteLine("Could not connect to mail server"); } } private void Disconnect() { string input = "quit" + "\r\n"; Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length); //Console.WriteLine(sr.ReadLine() ); ns.Close(); } public int GetNumberOfNewMessages() { Byte[] outbytes; string input; try{ Connect(); input = "stat" + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length); string resp = sr.ReadLine(); //Console.WriteLine(resp); string[] tokens = resp.Split(new Char[] {' '}); Disconnect(); return tokens[1].ToInt32(); } catch(InvalidOperationException ioe){ Console.WriteLine("Could not connect to mail server"); return 0; } } public ArrayList GetNewMessages(string subj) { int newcount; ArrayList newmsgs = new ArrayList(); try{ newcount = GetNumberOfNewMessages(); Connect(); for(int n=1; n'}); } } return "None"; } private string GetMessageBody(ArrayList msglines) { string body = ""; string line = " "; IEnumerator msgenum = msglines.GetEnumerator(); while(line.CompareTo("") != 0){ msgenum.MoveNext(); line = (string)msgenum.Current; } while (msgenum.MoveNext() ){ body = body + (string)msgenum.Current + "\r\n"; } return body; } private void DeleteMessage(int messagenumber) { Byte[] outbytes; string input; try{ input = "dele " + messagenumber.ToString() + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length); } catch(Exception e){ Console.WriteLine(e.ToString() ); Console.ReadLine(); } } }