switch(encodeMethod) { case "7bit": break; case "8bit": break; case "binary": break; case "quoted-printable": strTmp=new paladin.Decode().quoted_decode(strTmp); break; case "base64": strTmp=new paladin.Decode().base64_Decode(strTmp); break; default: break; }
if ((pointerend==-1) && (pointerfront==-1)) { break; } if ((pointerend==-1) && (pointerfront!=-1)) { pointerend=strSource.Length-boundary.Length; other=true; } if (other==true) { strTmp=strSource.Substring(pointerfront+boundary.Length); } else { strTmp=strSource.Substring(pointerfront+boundary.Length,pointerend-pointerfront-boundary.Length); } // analyseBody(strTmp,id); analyseBody(strTmp); } } else { switch (contentType) { case "text/plain": case "text/html": if (filename=="") { // new bizLogic .DBMyMail().update_outMail(id,getContent(strSource)); returnValue+=getContent(strSource); } else { // new bizLogic.DBMyMail().add_acc(id,filename,getAttachment(strSource),true); returnValue+=filename+getAttachment(strSource); // new bizLogic .DBMyMail().add_acc(id,filename,Encoding.ASCII.GetBytes(strSource),true); } break ; case "application/octet-stream": if (filename!="") { // new bizLogic.DBMyMail().add_acc(id,filename,getAttachment(strSource),true); returnValue+=filename+getAttachment(strSource); // new bizLogic .DBMyMail().add_acc(id,filename,Encoding.ASCII.GetBytes(strSource),true); } break ; default:
if (filename!="") { // new bizLogic.DBMyMail().add_acc(id,filename,getAttachment(strSource),true); returnValue+=filename+getAttachment(strSource); // new bizLogic .DBMyMail().add_acc(id,filename,Encoding.ASCII.GetBytes(strSource),true); } break; } }
Convert.FromBase64String(string)
Encoding.GetEncoding(int)
用 HtmlTextWriter 将第一部的结果生成html文件,用webbrowser控件显示,这样偷懒点
帮你贴一段,但是解析的不全。
private void btnReceive_Click(object sender, System.EventArgs e)
{
string popServer="",userName="",password="";
int mailQuantity=0;
Paladin.Email.EPop3Mail myEmail=new Paladin.Email.EPop3Mail();
//参数设置
popServer="pop3.sina.com.cn";
userName="happy_jun_2000";
password="19801012";
//pop3连接
myEmail.connectPop3(popServer,userName,password);
//邮件数目大于0时
if (myEmail.mailQuantity>=0)
{
// myEmail.readall(chkReserve.Checked,int.Parse(ViewState["userid"].ToString()),mailRule);
mailQuantity+=myEmail.mailQuantity;
}
Response.Write("<pre>邮件数目\r\n"+mailQuantity.ToString()+"\r\n");
Response.Write(myEmail.readall(false)+"</pre>"); //邮件内容
myEmail.disconnectPop3();
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System;
using System.Data.SqlClient;namespace Paladin.Email
{
/// <summary>
/// EMail 的摘要说明。
/// </summary>
public class EPop3Mail
{
public EPop3Mail()
{
//
// TODO: 在此处添加构造函数逻辑
//
} private TcpClient Server;
private NetworkStream NetStrm;
private StreamReader RdStrm;
private string Data;
private byte[] szData;
private string CRLF = "\r\n";
public int mailQuantity;
// private int userId=0;
//指针,在分析邮件时使用
// private int Position;
// 连接服务器,返回邮件的数量
/// <summary>
///
/// </summary>
/// <param name="pop3Server"></param>
/// <param name="userName"></param>
/// <param name="Password"></param>
public void connectPop3(string pop3Server,string userName,string Password)
{
// TcpClient Server;
// NetworkStream NetStrm;
// StreamReader RdStrm;
// string Data;
// byte[] szData;
// string CRLF = "\r\n";
string mailInfor;
Server = new TcpClient(pop3Server,110);
try
{
//初始化
NetStrm = Server.GetStream();
RdStrm= new StreamReader(Server.GetStream(),Encoding.Default);
mailInfor=RdStrm.ReadLine();
// Status.Items.Add(RdStrm.ReadLine()); //登录服务器过程
Data = "USER "+ userName+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
mailInfor=RdStrm.ReadLine();
Data = "PASS "+ Password+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
mailInfor=RdStrm.ReadLine(); //向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小
Data = "STAT"+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length); mailInfor=RdStrm.ReadLine().Trim();
if (mailInfor.IndexOf("+OK")!=-1)
{
mailQuantity=int.Parse(mailInfor.Substring(mailInfor.IndexOf(" "),mailInfor.LastIndexOf(" ")-(mailInfor.IndexOf(" "))));
paladin.common.setMessage("共收取了"+mailQuantity.ToString()+"邮件!");
}
else
{
mailQuantity=-1; }
// return mailQuantity;
}
catch (Exception err)
{
throw new Exception(err.ToString());
} } /// <summary>
/// 从邮件服务器上下载邮件
/// </summary>
/// <param name="mailID">邮件的序号</param>
/// <param name="Reserve">是否在服务器上保留备份</param>
/// <returns>邮件得全部内容</returns>
// public string receiveMail(int mailID,bool Reserve)
public string receiveMail(int mailID)
{
string szTemp;
StringBuilder OutBuffer=new StringBuilder(); try
{
//根据邮件编号从服务器获得相应邮件
Data = "RETR "+ mailID+CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
szTemp = RdStrm.ReadLine(); if(szTemp[0]!='-')
{
//不断地读取邮件内容,只到结束标志:英文句号
while(szTemp!=".")
{
OutBuffer.Append(szTemp+"\r\n");
szTemp = RdStrm.ReadLine();
} //若BackupChBox未选中,则收取邮件后,删除保留在服务器上的邮件
// if(Reserve== false)
// {
// Data = "DELE" + mailID + CRLF;
// szData =Encoding.ASCII.GetBytes(Data.ToCharArray());
// NetStrm.Write(szData,0,szData.Length);
// // Status.Items.Add(RdStrm.ReadLine());
// }
}
// else
// {
// Status.Items.Add(szTemp);
// }
return OutBuffer.ToString();
//将光标置回原来状态
}
catch (Exception err)
{
throw new Exception(err.ToString());
} } private void deleteMail(int mailID)
{
Data = "DELE " + mailID + CRLF;
szData =Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length); }
/// <summary>
/// 断开和POP3服务器的连接
/// </summary>
public void disconnectPop3()
{
Data = "QUIT"+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
// Status.Items.Add(RdStrm.ReadLine()); //断开连接
NetStrm.Close();
RdStrm.Close();
Server.Close();
} /// <summary>
/// 分析邮件,得到邮件的发件人
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns> private void getMailInfor(string strSource,ref string From,ref string NickName,ref string To,ref string Subject)
{
string strFrom="",strTo="",strSubject="";
string strDate=DateTime.Now.ToString();
int intCRLF;
int PositionFront;
string strCRLF="\r\n",LWSP=",";
// char[] test=strSource.Substring(0,strSource.Length ).ToCharArray();
int keyFrom=strSource.IndexOf("From:");
if (keyFrom!=-1)
{
intCRLF=strSource.IndexOf(strCRLF,keyFrom);
strFrom=strSource.Substring(keyFrom+5,intCRLF-keyFrom-5).Trim();
if (strFrom.IndexOf(" ")!=-1)
{
PositionFront=strFrom.IndexOf(" ");
string Name=strFrom.Substring(0,PositionFront);
string Address=strFrom.Substring(PositionFront+1);
From=getAddress(Address);
NickName=getResult(Name);
}
else
{
From=getAddress(strFrom); }
}
int keyTo=strSource.IndexOf("To:");
if (keyTo!=-1)
{
intCRLF=strSource.IndexOf(strCRLF,keyTo);
while (strSource.Substring(intCRLF-1,1)==LWSP)
{
intCRLF=strSource.IndexOf(strCRLF,intCRLF+2);
}
strTo=strSource.Substring(keyTo+3,intCRLF-keyTo-3).Trim();
To=getReceive(strTo);
}
int keySubject=strSource.IndexOf("Subject:");
if (keySubject!=-1)
{
intCRLF=strSource.IndexOf(strCRLF,keySubject);
while ((strSource.Substring(intCRLF+2,1)=="\t") || (strSource.Substring(intCRLF+2,1)==" "))
{
intCRLF=strSource.IndexOf(strCRLF,intCRLF+2); }
strSubject=strSource.Substring(keySubject+8,intCRLF-keySubject-8).Trim();
// bizLogic.common.setMessage(strSubject);
Subject=getSubject(strSubject);
}
}
/// <summary>
/// 分析主题,和发件人
/// </summary>
/// <param name="Source"></param>
/// <returns></returns>
private string getResult(string strSubject)
{
int PositionFront,PositionBehind;
string strTmp="";
if (strSubject.IndexOf("=?")!=-1)
{
PositionFront=strSubject.IndexOf('?',2);
string charset=strSubject.Substring(2,PositionFront-2);
string EncodeMethod=strSubject.Substring(PositionFront+1,1);
//指针向前移动三个字符
PositionFront+=3;
PositionBehind=strSubject.IndexOf("=?",PositionFront);
if (PositionBehind==-1)
{
PositionBehind=strSubject.IndexOf("?=",PositionFront);
}
string finalSubject=strSubject.Substring(PositionFront,PositionBehind-PositionFront);
switch (EncodeMethod)
{
case "q":
case "Q":
strTmp=new paladin.Decode().quoted_decode(finalSubject);
break;
case "b":
case "B":
strTmp=new paladin.Decode().base64_Decode(finalSubject);
break;
}
}
else
{
strTmp=strSubject;
}
return strTmp;
}
private string getAddress(string Source)
{
if (Source.IndexOf("<")!=-1)
{
if (Source.IndexOf(">")!=-1)
{
return Source.Substring(Source.IndexOf("<")+1,Source.IndexOf(">")-Source.IndexOf("<")-1);
}
}
return Source;
}
///
/// </summary>
/// <param name="reserve"></param>
/// <param name="userId"></param>
/// <param name="mailRule"></param>
public string readall(bool reserve)
{
string returnValue="";
string strTmp="",strHead="",strBody="",contentType="";
string From="",To="",Subject="",NickName="",Content="",noUse="",boxId="01";
int Position;
bool isRefuse=false,isRead=false,isDelServer=true;
// bizLogic.DBUser serchRule=new bizLogic.DBUser();
// DataView mailRule=new bizLogic.DBMailRule().select_mailRule(userId);
for (int i=1;i<=mailQuantity;i++)
{
strTmp=receiveMail(i);
// strTmp=receiveMail(i,reserve);
Position=strTmp.IndexOf("Content-Type:");
if (Position!=-1)
{
contentType=getContentType(strTmp,ref noUse);
strHead=strTmp.Substring(0,Position);
strBody=strTmp.Substring(Position);
getMailInfor(strHead,ref From,ref NickName,ref To,ref Subject);
//把邮件的头信息存入到数据库中
// for (int k=0;k<mailRule.Count;k++)
// {
// if ((bool.Parse(mailRule[k]["isApply"].ToString())) && serchRule.getUniqueUser(mailRule[k]["receiver"].ToString(),userId).IndexOf((object) From)!=-1)
// {
// if (!Convert.IsDBNull(mailRule[k]["isRead"]))
// {
// isRead=bool.Parse(mailRule[k]["isRead"].ToString());
// }
// if (!Convert.IsDBNull(mailRule[k]["isRefuse"]))
// {
// isRefuse=bool.Parse(mailRule[k]["isRefuse"].ToString());
// }
// if (!Convert.IsDBNull(mailRule[k]["isRead"]))
// {
// isDelServer=bool.Parse(mailRule[k]["isDelServer"].ToString());
// }
// if (!Convert.IsDBNull(mailRule[k]["boxId"]))
// {
// boxId=mailRule[k]["boxId"].ToString();
// }
// break;
// }
// }
if (!isRefuse)
{
// int mailId=new bizLogic.DBMyMail().add_outMail(userId,To,From,Subject,DateTime.Now.ToString(),Content,boxId,isRead);
// analyseBody(strBody,mailId);
returnValue+=analyseBody(strBody);
isRead=false;
isRefuse=false;
boxId="01";
}
}
else
{
Position=strTmp.IndexOf(CRLF+CRLF);
if (Position!=-1)
{
strHead=strTmp.Substring(0,Position);
strBody=strTmp.Substring(Position);
getMailInfor(strHead,ref From,ref NickName,ref To,ref Subject);
//把邮件的头信息存入到数据库中
// for (int k=0;k<mailRule.Count;k++)
// {
// if ((bool.Parse(mailRule[k]["isApply"].ToString())) && serchRule.getUniqueUser(mailRule[k]["receiver"].ToString(),userId).IndexOf((object) From)!=-1)
// {
// if (!Convert.IsDBNull(mailRule[k]["isRead"]))
// {
// isRead=bool.Parse(mailRule[k]["isRead"].ToString());
// }
// if (!Convert.IsDBNull(mailRule[k]["isRefuse"]))
// {
// isRefuse=bool.Parse(mailRule[k]["isRefuse"].ToString());
// }
// if (!Convert.IsDBNull(mailRule[k]["isRead"]))
// {
// isDelServer=bool.Parse(mailRule[k]["isDelServer"].ToString());
// }
// if (!Convert.IsDBNull(mailRule[k]["boxId"]))
// {
// boxId=mailRule[k]["boxId"].ToString();
// }
// break;
// }
//
// }
if (!isRefuse)
{
// int mailId=new bizLogic.DBMyMail().add_outMail(userId,To,From,Subject,DateTime.Now.ToString(),Content,boxId,isRead);
// new bizLogic.DBMyMail().update_outMail(mailId,strBody);
returnValue+=analyseBody(strBody);
isRead=false;
isRefuse=false;
boxId="01"; }
}
}
//删除邮件
// if (isDelServer)
// {
// deleteMail(i);
// }
// else if (!reserve)
// {
// deleteMail(i);
// }
// returnValue+=" from "+From+" to "+To+" subject "+Subject+" nickname "+NickName+" content "+Content;
}
return returnValue;
} /// <summary>
/// 取得ContentType
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
private string getContentType(string strSource,ref string fileName)
{
string contentType="",result="";
int end,headEnd;
headEnd=strSource.IndexOf(CRLF+CRLF);
headEnd+=4;
string strTmp=strSource.Substring(0,headEnd);
int Position=strTmp.IndexOf("Content-Type:");
if (Position!=-1)
{
end=strTmp.IndexOf(CRLF,Position);
contentType=strTmp.Substring(Position+13,end-(Position+13)).Trim();
} if (contentType.IndexOf("text/html")!=-1)
{
result="text/html";
}
if (contentType.IndexOf("text/plain")!=-1)
{
result="text/plain";
}
if (contentType.IndexOf("application/octet-stream")!=-1)
{
result="application/octet-stream";
}
Position=strTmp.IndexOf("filename");
if (Position!=-1)
{
end=strTmp.IndexOf(CRLF,Position);
fileName=getResult(strTmp.Substring(Position+10,(end-1)-(Position+10)).Trim());
}
else
{
fileName="";
}
return result;
} /// <summary>
/// 取得Content
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
private string getContent(string strSource)
{
string strTmp="",encodeMethod="";
int Position,end;
Position=strSource.IndexOf("Content-Transfer-Encoding:");
if (Position!=-1)
{
end=strSource.IndexOf(CRLF,Position);
encodeMethod=strSource.Substring(Position+27,end-(Position+27));
Position=strSource.IndexOf(CRLF+CRLF,Position);
end=strSource.Length-1;
if (end==-1)
{
strTmp="";
}
else
{
strTmp=strSource.Substring(Position+4,end-(Position+4));
} }
else
{
Position=strSource.IndexOf(CRLF+CRLF); if (Position!=-1)
{
end=strSource.Length-1;
strTmp=strTmp=strSource.Substring(Position+4,end-(Position+4));
}
}
switch(encodeMethod)
{
case "7bit":
break;
case "8bit":
break;
case "binary":
break;
case "quoted-printable":
strTmp=new paladin.Decode().quoted_decode(strTmp);
break;
case "base64":
strTmp=new paladin.Decode().base64_Decode(strTmp);
break;
default:
break;
}
return strTmp; } private byte[] getAttachment(string strSource)
{
string strTmp="",encodeMethod;
int Position,end;
byte[] outBuffer=new byte[strSource.Length];
Position=strSource.IndexOf("Content-Transfer-Encoding:");
if (Position!=-1)
{
end=strSource.IndexOf(CRLF,Position);
encodeMethod=strSource.Substring(Position+27,end-(Position+27));
Position=strSource.IndexOf(CRLF+CRLF,Position);
end=strSource.IndexOf(CRLF+CRLF,Position+4);
if (end==-1)
{
strTmp="";
}
else
{
strTmp=strSource.Substring(Position+4,end-(Position+4));
}
switch(encodeMethod)
{
case "7bit":
break;
case "8bit":
break;
case "binary":
break;
case "quoted-printable":
outBuffer=new paladin.Decode().quoted_decode_toByte(strTmp);
break;
case "base64":
outBuffer=new paladin.Decode().base64_Decode_toByte(strTmp);
break;
default:
break;
}
} return outBuffer; } private string getBoundary(string strSource,ref int end)
{
string boundary="";
int headEnd;
headEnd=strSource.IndexOf(CRLF+CRLF);
headEnd+=4;
string strTmp=strSource.Substring(0,headEnd);
int Position=strTmp.IndexOf("boundary=");
if (Position!=-1)
{
end=strTmp.IndexOf(CRLF,Position);
boundary=strTmp.Substring(Position+10,(end-1)-(Position+10)).Trim();
return boundary;
}
else
{
return boundary;
}
}
private string analyseBody(string strSource)
{
string returnValue="";
string boundary="", contentType="",strTmp="",filename="";
int pointerfront=0,pointerend=0;
bool other=false;
contentType=getContentType(strSource,ref filename);
boundary=getBoundary(strSource,ref pointerend);
if (boundary!="")
{
while (pointerfront!=-1)
{
pointerfront=strSource.IndexOf(boundary,pointerend);
pointerend=strSource.IndexOf(boundary,pointerfront+boundary.Length);
if ((pointerend==-1) && (pointerfront==-1))
{
break;
} if ((pointerend==-1) && (pointerfront!=-1))
{
pointerend=strSource.Length-boundary.Length;
other=true;
}
if (other==true)
{
strTmp=strSource.Substring(pointerfront+boundary.Length);
}
else
{
strTmp=strSource.Substring(pointerfront+boundary.Length,pointerend-pointerfront-boundary.Length);
}
// analyseBody(strTmp,id);
analyseBody(strTmp);
}
}
else
{
switch (contentType)
{
case "text/plain": case "text/html":
if (filename=="")
{
// new bizLogic .DBMyMail().update_outMail(id,getContent(strSource));
returnValue+=getContent(strSource);
}
else
{
// new bizLogic.DBMyMail().add_acc(id,filename,getAttachment(strSource),true);
returnValue+=filename+getAttachment(strSource);
// new bizLogic .DBMyMail().add_acc(id,filename,Encoding.ASCII.GetBytes(strSource),true); }
break ; case "application/octet-stream":
if (filename!="")
{
// new bizLogic.DBMyMail().add_acc(id,filename,getAttachment(strSource),true);
returnValue+=filename+getAttachment(strSource);
// new bizLogic .DBMyMail().add_acc(id,filename,Encoding.ASCII.GetBytes(strSource),true);
}
break ;
default:
if (filename!="")
{
// new bizLogic.DBMyMail().add_acc(id,filename,getAttachment(strSource),true);
returnValue+=filename+getAttachment(strSource);
// new bizLogic .DBMyMail().add_acc(id,filename,Encoding.ASCII.GetBytes(strSource),true);
}
break;
} }
// return "";
if(returnValue.Trim()=="")
{
returnValue=strSource;
}
return returnValue; } private string getEncodingMethod(string strSource)
{
string strTmp="",encodeMethod="";
int Position,end,headEnd;
headEnd=strSource.IndexOf(CRLF+CRLF);
headEnd+=4;
strTmp=strSource.Substring(0,headEnd);
Position=strTmp.IndexOf("Content-Transfer-Encoding:");
if (Position!=-1)
{
end=strSource.IndexOf(CRLF,Position);
encodeMethod=strSource.Substring(Position+27,end-(Position+27));
} return encodeMethod; } private string getReceive(string strSource)
{
char[] separator={','};
int PositionFront;
string From="";
string[] multipart=strSource.Split(separator,20);
for (int i=0;i<multipart.Length;i++)
{
if (multipart[i].IndexOf(" ")!=-1)
{
PositionFront=multipart[i].IndexOf(" ");
string Address=multipart[i].Substring(PositionFront+1);
From+=getAddress(Address)+" ";
}
else
{
From+=getAddress(multipart[i])+" "; }
}
return From.Trim();
}
private string getSubject(string strSource)
{
char[] separator={'\t',' '};
string Subject="";
string[] multipart=strSource.Split(separator,20);
for (int i=0;i<multipart.Length;i++)
{
Subject+=getResult(multipart[i]); }
return Subject.Trim(); }
}
}
=================================================
HTTP://WWW.ITZYK.NET IT资源库--->IT人的专业资源库
=================================================
http://www.itzyk.net 中国IT人的技术资源网站。网站刚刚起步,希望广大IT网友们加入我们,我们会一直把这个网站做大,做全起来。现在网站分为两大部分:http://bbs.itzyk.net -->专业讨论DoNet技术的论坛社区 http://down.itzyk.net -->各种程序源码的下载站。现在论坛区正处于发展壮大期,诚邀各位有志于IT技术方面的朋友加入我们的论坛来负责版主一职,发挥你们的能力让我们把这个论坛壮大起来吧!
版本:0.97
版权所有 张小龙 www.aerofox.com飞狐邮件服务器是一套基于Windows NT的Internet/Intranet电子邮件服务器,
提供Internet标准的SMTP和POP3协议服务,与Internet电子邮件完全兼容。注意事项:1, 这是测试版,仅为测试目的,请将试用结果email给:
[email protected],文档请见:http://www.aerofox.com/mta/index.htm。3,如果您有什么问题,请先参考上面网站的“疑难解答”,如不能解决,
请将问题email给我们。谢谢您的测试!