在vs3下调试无误的pop3收信程序

王朝other·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

using System.Net.Sockets;

using System.Collections;

using System.IO;

using System.Net;

using System;

using System.Web.Mail;

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 Convert.ToInt32(tokens[1]);

}

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<newcount+1; n++)

{

ArrayList msglines = GetRawMessage(n);

string msgsubj = GetMessageSubject(msglines);

if(msgsubj.CompareTo(subj) == 0)

{

System.Web.Mail.MailMessage msg = new MailMessage();

msg.Subject = msgsubj;

msg.From = GetMessageFrom(msglines);

msg.Body = GetMessageBody(msglines);

newmsgs.Add(msg);

DeleteMessage(n);

}

}

Disconnect();

return newmsgs;

}

catch(Exception e)

{

Console.WriteLine(e.ToString() );

Console.ReadLine();

return newmsgs;

}

}

private ArrayList GetRawMessage (int messagenumber)

{

Byte[] outbytes;

string input;

string line = "";

input = "retr " + messagenumber.ToString() + "\r\n";

outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());

ns.Write(outbytes,0,outbytes.Length);

ArrayList msglines = new ArrayList();

do

{

line = sr.ReadLine();

msglines.Add(line);

} while (line != ".");

msglines.RemoveAt(msglines.Count-1);

return msglines;

}

private string GetMessageSubject(ArrayList msglines)

{

string[] tokens;

IEnumerator msgenum = msglines.GetEnumerator();

while (msgenum.MoveNext() )

{

string line = (string)msgenum.Current;

if(line.StartsWith("Subject:") )

{

tokens = line.Split(new Char[] {' '});

return tokens[1].Trim();

}

}

return "None";

}

private string GetMessageFrom (ArrayList msglines)

{

string[] tokens;

IEnumerator msgenum = msglines.GetEnumerator();

while (msgenum.MoveNext() )

{

string line = (string)msgenum.Current;

if(line.StartsWith("Return-Path:") )

{

tokens = line.Split(new Char[] {'<'});

return tokens[1].Trim(new Char[] {'<','>'});

}

}

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();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航