帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
在vs3下调试无误的pop3收信程序
作者:hp.q 发布时间:2005-03-12 来源:开发者俱乐部
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();
}

}

}

  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统