在C#中如何在客户端接收信件


所属类别:.NET

文章作者:未知

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


在C#中如何在客户端接收信件创建界面类型对象名Text属性LabelLabel1LabelLabel2用户名:LabelLabel3密码TextBoxTextBox1TextBoxTextBox2ButtonButton1接收ButtonButton2退出当发送的验证通过Label1的text属性就会显示成功的消息当发送的验证没有通过Label1的text属性就会显示失败的消息显示代码1.在代码前端添加代码using System.Threading;using System.Net;using System.Net.Sockets;using System.IO;2.添加字段代码private TcpClient mailclient; private NetworkStream ns;private StreamReader sr;private StreamWriter sw;3.双击“接收”按钮private void Button1_Click(object sender, System.EventArgs e) { login(); } private int login() { string m_response; //从服务器返回的信息变量 int totmessages;

//指明在你的邮箱中有几封 try { //连接服务器,其中第一参数代表服务器地址,第二参数代表pop3的端口数 mailclient=new TcpClient("pop.163.com",110); } catch(Exception e) { Label1.Text="error";//服务器连接失败 return 2; } ns=mailclient.GetStream();//返回服务器字节流 sr=new StreamReader(ns); sw=new StreamWriter(ns); m_response=sr.ReadLine();//读取服务器字节流字符 //发送用户名给服务器 sw.WriteLine("User "+TextBox1.Text); sw.Flush(); //验证用户名是否正确 m_response=sr.ReadLine(); if(m_response.Substring(0,3)=="-ER") { Label1.Text="User error";//用户连接失败 return 2; } //发送密码给服务器 sw.WriteLine("Pass "+TextBox2.Text); sw.Flush(); try { //验证密码是否正确 m_response=sr.ReadLine(); } catch(Exception e) { Label1.Text="Password error";//密码连接失败 return 2; } if(m_response.Substring(0,4)=="-ERR") { Label1.Text="Password error";//密码错误 return 2; } sw.WriteLine("Stat");//执行pop3权限检查命令 sw.Flush(); m_response=sr.ReadLine(); string[] nummess=m_response.Split(' '); totmessages=Convert.ToInt16(nummess[1]);//获取服务器中有几封信件 if(totmessages>0) { Label1.Text="you have "+totmessages+" messages"; } else { Label1.Text="you have no messages"; } return 1; }4.双击“退出”按钮private void Button2_Click(object sender, System.EventArgs e) { if(ns!=null) { sw.Close(); sr.Close(); ns.Close(); mailclient.Close(); } }注意点:Stat代表权限检查命令,如果要得到标题和发件人信息要用top执行命令,要得到文本信息要用retr执行命令,这些内容还需我总结一下,我在下个礼拜会详细介绍。在星期四我写了一篇关于“在C#中如何发送信件”的文章写的不怎么样,因此今天我另外写了篇“在C#中如何接收信件”,在下个礼拜我会总结在C#中整个发送Email和接收Email的基本内容请大家光顾咯谢谢关闭本页

相关信息

· ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!

· Linux系统平台上安装和配置Ruby on Rails

· 如何理Y料熘械Null

· Rails创始人回应Zed Shaw抨击








....

8914 11130