邮件操作:POP3协议
邮件操作,用POP3协议实现
1private void GetMail()
2{
3//连接邮件服务器
4System.Net.Sockets.TcpClient TC = new System.Net.Sockets.TcpClient("pop.163.com", 110);
5//发送用户名
6byte[] A_Array = Encoding.ASCII.GetBytes("USER robbine111\r\n");
7//发送密码
8byte[] B_Array = Encoding.ASCII.GetBytes("PASS 201103\r\n");
9//察看邮件列表:统计信息
10byte[] C_Array = Encoding.ASCII.GetBytes("STAT\r\n");
11//察看邮件列表:邮件数量和大小
12byte[] D_Array = Encoding.ASCII.GetBytes("LIST\r\n");
13//关闭连接
14byte[] E_Array = Encoding.ASCII.GetBytes("QUIT\r\n");
15//缓存数组
16byte[] Buff = new byte[256];
17//缓存字符串
18string Buff_Str = "";
19//读取计数
20int i = 0;
21//获取网络流
22NetworkStream NS = TC.GetStream();
23//写入第一条命令
24NS.Write(A_Array, 0, A_Array.Length);
25//读取响应
26i = NS.Read(Buff, 0, 256);
27//字节解码
28Buff_Str += Encoding.UTF8.GetString(Buff, 0, i);
29i = 0;
30//刷新流
31NS.Flush();
32//写入第二条命令
33NS.Write(B_Array, 0, B_Array.Length);
34i = NS.Read(Buff, 0, 256);
35//读取响应
36Buff_Str += Encoding.UTF8.GetString(Buff, 0, i);
37i = 0;
38//刷新流
39NS.Flush();
40//写入第三条命令
41NS.Write(C_Array, 0, C_Array.Length);
42//读取大数据量
43string MailCountMsg = "";
44do
45{
46i = NS.Read(Buff, 0, 256);
47MailCountMsg = Encoding.UTF8.GetString(Buff, 0, i);
48Buff_Str += MailCountMsg;
49} while (i >= 256);
50获取所有的邮件
60i = 0;
61//分析邮件列表
62NS.Write(D_Array, 0, D_Array.Length);
63//读取邮件列表
64do
65{
66i = NS.Read(Buff, 0, 256);
67Buff_Str += Encoding.GetEncoding("GB2312").GetString(Buff, 0, i);
68} while (i >= 256);
69//写入结束命令
70NS.Write(E_Array, 0, E_Array.Length);
71NS.Close();
72MailContentBox.AppendText(Buff_Str);
73}
2{
3//连接邮件服务器
4System.Net.Sockets.TcpClient TC = new System.Net.Sockets.TcpClient("pop.163.com", 110);
5//发送用户名
6byte[] A_Array = Encoding.ASCII.GetBytes("USER robbine111\r\n");
7//发送密码
8byte[] B_Array = Encoding.ASCII.GetBytes("PASS 201103\r\n");
9//察看邮件列表:统计信息
10byte[] C_Array = Encoding.ASCII.GetBytes("STAT\r\n");
11//察看邮件列表:邮件数量和大小
12byte[] D_Array = Encoding.ASCII.GetBytes("LIST\r\n");
13//关闭连接
14byte[] E_Array = Encoding.ASCII.GetBytes("QUIT\r\n");
15//缓存数组
16byte[] Buff = new byte[256];
17//缓存字符串
18string Buff_Str = "";
19//读取计数
20int i = 0;
21//获取网络流
22NetworkStream NS = TC.GetStream();
23//写入第一条命令
24NS.Write(A_Array, 0, A_Array.Length);
25//读取响应
26i = NS.Read(Buff, 0, 256);
27//字节解码
28Buff_Str += Encoding.UTF8.GetString(Buff, 0, i);
29i = 0;
30//刷新流
31NS.Flush();
32//写入第二条命令
33NS.Write(B_Array, 0, B_Array.Length);
34i = NS.Read(Buff, 0, 256);
35//读取响应
36Buff_Str += Encoding.UTF8.GetString(Buff, 0, i);
37i = 0;
38//刷新流
39NS.Flush();
40//写入第三条命令
41NS.Write(C_Array, 0, C_Array.Length);
42//读取大数据量
43string MailCountMsg = "";
44do
45{
46i = NS.Read(Buff, 0, 256);
47MailCountMsg = Encoding.UTF8.GetString(Buff, 0, i);
48Buff_Str += MailCountMsg;
49} while (i >= 256);
50获取所有的邮件
60i = 0;
61//分析邮件列表
62NS.Write(D_Array, 0, D_Array.Length);
63//读取邮件列表
64do
65{
66i = NS.Read(Buff, 0, 256);
67Buff_Str += Encoding.GetEncoding("GB2312").GetString(Buff, 0, i);
68} while (i >= 256);
69//写入结束命令
70NS.Write(E_Array, 0, E_Array.Length);
71NS.Close();
72MailContentBox.AppendText(Buff_Str);
73}