随笔 - 30, 文章 - 0, 评论 - 324, 引用 - 11
数据加载中……

在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 阅读(2860) 评论(15)  编辑 收藏 网摘

评论

#1楼[楼主]   回复  引用  查看    

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

#2楼   回复  引用  查看    

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

#3楼[楼主]   回复  引用  查看    

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

#4楼[楼主]   回复  引用  查看    

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

#5楼   回复  引用  查看    

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

#6楼   回复  引用  查看    

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

{

login();

}

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

#7楼   回复  引用    

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

#8楼   回复  引用    

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

#9楼   回复  引用  查看    

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

#10楼   回复  引用    

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

#11楼   回复  引用    

抄来抄去!@
2005-04-17 17:37 | 将计就计

#12楼   回复  引用    

sdf
2005-08-24 11:35 | sdf[未注册用户]

#13楼   回复  引用    

怎么下载附件????
2007-02-28 15:55 | 郁闷中[未注册用户]

#14楼   回复  引用    

totmessages=Convert.ToInt16(nummess[1]);//获取服务器中有几封信件

错了,输入的字符串格式不正确。。
2007-04-14 20:34 | feng[未注册用户]

#15楼   回复  引用    

http://www.message_ortrrolt.com/
2009-05-15 13:40 | nick_ernoro[未注册用户]



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 46243




相关文章:

相关链接: