|
技术资料 > .Net专区 > E-mail专题 : 在vs3下调试无误的pop3收信程序 |
在vs3下调试无误的pop3收信程序 March 25,2004 |
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 + "rn";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
sr.ReadLine();
//Console.WriteLine(sr.ReadLine() );
input = "pass " + pwd + "rn";
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" + "rn";
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" + "rn";
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() + "rn";
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 + "rn";
}
return body;
}
private void DeleteMessage(int messagenumber)
{
Byte[] outbytes;
string input;
try
{
input = "dele " + messagenumber.ToString() + "rn";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
}
catch(Exception e)
{
Console.WriteLine(e.ToString() );
Console.ReadLine();
}
}
}
|
|
Copyright © 2001-2008 Shenzhen Hiblue Software Team All rights reserved