动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Asp → 阅读文章

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

作者:hp.q 来源:开发者俱乐部 
阅读 5381 人次 , 2002-1-4 

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

}

}
  

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:有关重复记录的删除(SQL SERVER)
· 下一篇:WINDOWS2000服务器账号登陆身份验证
· 利用c#制作简单的留言板(3)
· ASP中FSO的神奇功能 - 简介
· ASP3.0中的流控制能力(3)
· ASP进阶教程:数据库版本的留言簿
· 图片渐渐显示(谈入)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077