频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

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

来源: 作者: 出处:巧巧读书 2006-11-24 进入讨论组

  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 + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
sr.ReadLine();
//Console.WriteLine(sr.ReadLine() );

input = "pass " + pwd + " ";
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" + " ";
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" + " ";
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() + " ";
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 + " ";
}
return body;
}
private void DeleteMessage(int messagenumber)
{
Byte[] outbytes;
string input;

try
{
input = "dele " + messagenumber.ToString() + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
}
catch(Exception e)
{
Console.WriteLine(e.ToString() );
Console.ReadLine();
}

}

}

文章地址: http://www.qqread.com/asp/2006/11/e281402.html 更多文章 更多内容请看POP3协议专题,或进入讨论组讨论。
更多专题 【深 度 阅 读】 相 关 文 章
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章