chiname

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

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

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

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

一、创建界面

 

类型

对象名

Text属性

Label

Label1

 

Label

Label2

用户名:

Label

Label3

密码

TextBox

TextBox1

 

TextBox

TextBox2

 

Button

Button1

接收

Button

Button

退出

 

当发送的验证通过Label1text属性就会显示成功的消息

当发送的验证没有通过Label1text属性就会显示失败的消息

 

二、显示代码

 

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的基本内容请大家光顾咯谢谢

 

posted on 2004-09-24 13:12 surprise 阅读(1212) 评论(11)  编辑 收藏

评论

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

不好意思,在公司上传图片不行,我回到家里上传一下
2004-09-24 13:19 | surprise

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

好像有点错误, private int Login() 如果在button1的事件中调用?
而且命名尽量规范.
2004-09-24 13:49 | CoolBug

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

兄弟,这样写是可以的,我用了一个小时写完的,我已经运行过了
2004-09-24 13:57 | surprise

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

好的,今后的代码我尽量规范点
2004-09-24 14:01 | surprise

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

return 2;可以删掉
2004-09-24 14:18 | CoolBug

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

你的 return 1,2 给 private void Button1_Click(object sender, System.EventArgs e)

{

login();

}

干什么呀?
2004-09-24 14:20 | CoolBug

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

确实没什么意义,只不过已经写上去了不想该了。
2004-09-24 15:05 | surprise

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

但用了
private void Button1_Click(object sender, System.EventArgs e)
{
login();
}
可以分离代码 ,让Button1只做简单的处理,如果做大项目那就派上用处了。
2004-09-24 15:07 | surprise

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

大家可以看看我写的sourceforge.net/projects/hpop/
2004-09-24 17:01 | 灵感之源

# re: 在C#中如何在客户端接收信件。问题是怎么拆分附件呢   

问题是怎么拆分附件呢
2004-10-19 09:29 | very

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

抄来抄去!@
2005-04-17 17:37
posted on 2005-07-11 17:45  把我的欢乐带给你  阅读(177)  评论(0)    收藏  举报