| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1734 人关注过本帖
标题:[分享][原创][讨论][开源][求助]pop3接收邮件代码
只看楼主 加入收藏
核心科技0001
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-31
收藏
得分:0 
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using
//Download by http://www.
namespace Ex418
{
   
    public partial class Form1 : Form
    {
        string yy;
        public Form1()
        {
            InitializeComponent();
        }
        TcpClient server;//接服务器
        string sendstring;//用于存储POP3服务命令参数
        byte[] bufferstring;//用于存储POP3服务命令参数字节数
        NetworkStream networkstream;//接服务器与服务器进行数据交互
        StreamReader streamreader;//读取信息数据
        private void button2_Click(object sender, EventArgs e)
        {
            //将光标置为等待状态
            Cursor cr = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            server = new TcpClient(this.popserver.Text, 110);//实例TcpClient 类对象联接服务器
            networkstream = server.GetStream();//实例NetworkStream类对象接收返回发送的数据
            streamreader = new StreamReader(networkstream);//实例StreamReader类对象读取数据

            try
            {
                sendstring = "USER "+this.txtusername.Text+"\r\n";//存储用户名
                bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                networkstream.Write(bufferstring, 0, bufferstring.Length);//将用户名发送到服务器
                richTextBox1.AppendText(streamreader.ReadLine() + "\r\n");//将用用户显示在 richTextBox控件中
                sendstring = "PASS " + this.txtpwd.Text + "\r\n";//存储用户密码
                bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                networkstream.Write(bufferstring, 0, bufferstring.Length);//将用户密码发送到服务器
                richTextBox1.AppendText(streamreader.ReadLine() + "\r\n");
                sendstring = "STAT " +"\r\n";//储存从服务器获得所有信息序号和字节数命令
                bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                networkstream.Write(bufferstring, 0, bufferstring.Length);//从服务器获得所有信息序号和字节数
                string strResult=streamreader.ReadLine();//读取从服务器返回的数据
                if (strResult.IndexOf('-') == -1)
                {
                    richTextBox1.AppendText(strResult + "\r\n");
                    sendstring = "LIST " + "\r\n";//存储从服务器中获得信息列表和大小的命令
                    bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                    networkstream.Write(bufferstring, 0, bufferstring.Length);

                    string strInfo = streamreader.ReadLine();
                    string[] str = strInfo.Split(' ');

                    richTextBox1.AppendText("邮件数量:" + str[1] + "\r\n");
                    richTextBox1.AppendText(str[1] + ":封邮件总容量为" + str[2] + "\r\n");

                    MessageBox.Show(this.txtusername.Text + "用户您好!!!");
                     button1.Enabled = true;
                    //将光标置回原来的状态
                    Cursor.Current = cr;
                    button4.Enabled = true;
                    button2.Enabled = false;

                }
                else
                {
                    MessageBox.Show("读取信息有误,请重新登录");
                }
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
        }
        private void Showinfo()
        {
         
            Cursor cr = Cursor.Current;//定义鼠标信息
            Cursor.Current = Cursors.WaitCursor;
            this.richTextBox1.Clear();
            try
            {
               
                string strResult = "";
                sendstring = "RETR " + this.textBox4.Text + "\r\n";//存储从服务器获得一条信息的命令
                bufferstring = Encoding.ASCII.GetBytes(sendstring.ToCharArray());
                networkstream.Write(bufferstring, 0, bufferstring.Length);
                strResult = streamreader.ReadLine();
                if (strResult[0] != '-')
                {
                    //不断地读取邮件内容,只到结束标志:英文句号
                    yy = "";
                    while (strResult != ".")
                    {
                       
                        yy = yy + strResult + "\r\n";
                        this.richTextBox1.AppendText(strResult + "\r\n");
                        strResult = streamreader.ReadLine();
                       
                    }
                 
                }
                else
                {
                    this.richTextBox1.AppendText("\r\n" + "邮件错误" + "\r\n");
                }
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
            Cursor.Current = cr;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Showinfo();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.button4.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //将光标置为等待状态
            Cursor cr = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            richTextBox1.Clear();
            
            //向服务器发送QUIT命令从而结束和POP3服务器的会话
            sendstring ="QUIT"+this.txtusername.Text+"\r\n";
            bufferstring = Encoding.ASCII.GetBytes(sendstring.ToCharArray());
            networkstream.Write(bufferstring, 0, bufferstring.Length);
            richTextBox1.AppendText(streamreader.ReadLine() + "\r\n");

            //断开连接
            networkstream.Close();
            streamreader.Close();

            //改变按钮的状态
            button2.Enabled = true;
            button4.Enabled = false;
            button1.Enabled = false;

            //将光标置回原来的状态
            Cursor.Current = cr;



        }
    }
}
2012-10-31 21:58
快速回复:[分享][原创][讨论][开源][求助]pop3接收邮件代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029296 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved