Pop3接收数据,参见
http://www.aspalliance.com/chrisg/default.asp?article=93
改成c#代码,并修改了一些bug
如
1.用的是ASCII,并不适合国内的邮件读取
解决方法:改为Encoding.GetEncoding(936)
2.GetResponse并不是堵塞方式,没有完全下载数据
解决方法:设置一个strEnd标志,来判断是否结束
3.GetResponse每次都要返回ReceiveBufferSize长的数据
解决方法:根据接收到数据长度,返回缓冲中的对应长度的数据
等等..
System.Net.Sockets.TcpClient tcpC;
System.Net.Sockets.NetworkStream netStream;
string SendCommand(string sToSend){
byte[] bData=Encoding.GetEncoding(936).GetBytes(sToSend+Environment.NewLine);
netStream.Write(bData,0,bData.Length);
return GetResponse();
}
string GetResponse(){
byte[] bData=new byte[tcpC.ReceiveBufferSize];
int iRec=netStream.Read(bData, 0, bData.Length);
return Encoding.GetEncoding(936).GetString(bData,0,iRec);
}
string ReadMail(string ps,string un,string pw){
tcpC=new System.Net.Sockets.TcpClient(ps,110);
netStream = tcpC.GetStream();
string strResponse=GetResponse();
string strNL=Environment.NewLine;
string strEnd=strNL+"."+strNL+"+OK "+strNL;
SendCommand("user "+un);
SendCommand("pass "+pw);
strResponse=SendCommand("stat");
int iCount=Int32.Parse(strResponse.Split(' ')[1]);
Response.Write(iCount + " Messages");
for(int i=1;i<iCount;i++)strResponse+=SendCommand("top "+i+" 0");
strResponse+=SendCommand("QUIT");
while(!strResponse.EndsWith(strEnd))strResponse+=GetResponse();
tcpC.Close();
return strResponse;
}
调用方法:
ReadMail(pop3Server,username,password)
ps.简化了代码,取消了一些异常的捕捉,是为了让大家看得清楚明白.
http://www.aspalliance.com/chrisg/default.asp?article=93
改成c#代码,并修改了一些bug
如
1.用的是ASCII,并不适合国内的邮件读取
解决方法:改为Encoding.GetEncoding(936)
2.GetResponse并不是堵塞方式,没有完全下载数据
解决方法:设置一个strEnd标志,来判断是否结束
3.GetResponse每次都要返回ReceiveBufferSize长的数据
解决方法:根据接收到数据长度,返回缓冲中的对应长度的数据
等等..
System.Net.Sockets.TcpClient tcpC;
System.Net.Sockets.NetworkStream netStream;
string SendCommand(string sToSend){
byte[] bData=Encoding.GetEncoding(936).GetBytes(sToSend+Environment.NewLine);
netStream.Write(bData,0,bData.Length);
return GetResponse();
}
string GetResponse(){
byte[] bData=new byte[tcpC.ReceiveBufferSize];
int iRec=netStream.Read(bData, 0, bData.Length);
return Encoding.GetEncoding(936).GetString(bData,0,iRec);
}
string ReadMail(string ps,string un,string pw){
tcpC=new System.Net.Sockets.TcpClient(ps,110);
netStream = tcpC.GetStream();
string strResponse=GetResponse();
string strNL=Environment.NewLine;
string strEnd=strNL+"."+strNL+"+OK "+strNL;
SendCommand("user "+un);
SendCommand("pass "+pw);
strResponse=SendCommand("stat");
int iCount=Int32.Parse(strResponse.Split(' ')[1]);
Response.Write(iCount + " Messages");
for(int i=1;i<iCount;i++)strResponse+=SendCommand("top "+i+" 0");
strResponse+=SendCommand("QUIT");
while(!strResponse.EndsWith(strEnd))strResponse+=GetResponse();
tcpC.Close();
return strResponse;
}
调用方法:
ReadMail(pop3Server,username,password)
ps.简化了代码,取消了一些异常的捕捉,是为了让大家看得清楚明白.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现