zoukankan      html  css  js  c++  java
  • 邮件发送接收

    string server = this.txtserver.Text;
                        MailMessage Mail = new MailMessage(this.txtFrom.Text, this.txtGet.Text);
                        Mail.Subject = this.txtSubject.Text.Trim().ToString();
                        Mail.Body = this.richTextBox1.Text.Trim().ToString();
                        SmtpClient Client = new SmtpClient("smtp.126.com",25);//实例一个SmtpClient类
    //Client.UseDefaultCredentials = false;
    Client.Credentials = new System.Net.NetworkCredential(txtusername.Text,txtpwd.Text);
    Client.DeliveryMethod = SmtpDeliveryMethod.Network;
    //Client.Host = "smtp.126.com"; //附件 if (txtAttach.Text.Length > 0) { Mail.Attachments.Add(new Attachment(txtAttach.Text, MediaTypeNames.Application.Octet)); } Client.Send(Mail);
        TcpClient server;//接服务器
            string sendstring;//用于存储POP3服务命令参数
            byte[] bufferstring;//用于存储POP3服务命令参数字节数
            NetworkStream networkstream;//接服务器与服务器进行数据交互
            StreamReader streamreader;//读取信息数据
            private void btnConn_Click(object sender, EventArgs e)
            {
                //将光标置为等待状态
                Cursor cr = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
    
                server = new TcpClient(this.txtPopserver.Text, 110);//实例TcpClient 类对象联接服务器
                networkstream = server.GetStream();//实例NetworkStream类对象接收返回发送的数据
                streamreader = new StreamReader(networkstream);//实例StreamReader类对象读取数据
    
                try
                {
                    sendstring = "USER "+this.txtUserName.Text+"
    ";//存储用户名
                    bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                    networkstream.Write(bufferstring, 0, bufferstring.Length);//将用户名发送到服务器
                    txtContent.AppendText(streamreader.ReadLine() + "
    ");//将用用户显示在 richTextBox控件中
                    sendstring = "PASS " + this.txtPwd.Text + "
    ";//存储用户密码
                    bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                    networkstream.Write(bufferstring, 0, bufferstring.Length);//将用户密码发送到服务器
                    txtContent.AppendText(streamreader.ReadLine() + "
    ");
                    sendstring = "STAT " +"
    ";//储存从服务器获得所有信息序号和字节数命令
                    bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                    networkstream.Write(bufferstring, 0, bufferstring.Length);//从服务器获得所有信息序号和字节数
                    string strResult=streamreader.ReadLine();//读取从服务器返回的数据
                    if (strResult.IndexOf('-') == -1)
                    {
                        txtContent.AppendText(strResult + "
    ");
                        sendstring = "LIST " + "
    ";//存储从服务器中获得信息列表和大小的命令
                        bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());
                        networkstream.Write(bufferstring, 0, bufferstring.Length);
    
                        string strInfo = streamreader.ReadLine();
                        string[] str = strInfo.Split(' ');
    
                        txtContent.AppendText("邮件数量:" + str[1] + "
    ");
                        txtContent.AppendText(str[1] + ":封邮件总容量为" + str[2] + "
    ");
    
                        MessageBox.Show(this.txtUserName.Text + "用户您好!!!");
                         btnAccept.Enabled = true;
                        //将光标置回原来的状态
                        Cursor.Current = cr;
                        btnDisconn.Enabled = true;
                        btnConn.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("读取信息有误,请重新登录");
                    }
                }
                catch (Exception ey)
                {
                    MessageBox.Show(ey.Message);
                }
            }
            private void Showinfo()
            {
                Cursor cr = Cursor.Current;//定义鼠标信息
                Cursor.Current = Cursors.WaitCursor;
                this.txtContent.Clear();
                try
                {
                    string strResult = "";
                    sendstring = "RETR " + this.txtEmailIndex.Text + "
    ";//存储从服务器获得一条信息的命令
                    bufferstring = Encoding.ASCII.GetBytes(sendstring.ToCharArray());
                    networkstream.Write(bufferstring, 0, bufferstring.Length);
                    strResult = streamreader.ReadLine();
                    if (strResult[0] != '-')
                    {
                        //不断地读取邮件内容,只到结束标志:英文句号
                        while (strResult != ".")
                        {
                            this.txtContent.AppendText(strResult + "
    ");
                            strResult = streamreader.ReadLine();
                        }
                    }
                    else
                    {
                        this.txtContent.AppendText("
    " + "邮件错误" + "
    ");
                    }
                }
                catch (Exception ey)
                {
                    MessageBox.Show(ey.Message);
                }
                Cursor.Current = cr;
            }
    
            private void btnDisconn_Click(object sender, EventArgs e)
            {
                //将光标置为等待状态
                Cursor cr = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                txtContent.Clear();
                
                //向服务器发送QUIT命令从而结束和POP3服务器的会话
                sendstring ="QUIT"+this.txtUserName.Text+"
    ";
                bufferstring = Encoding.ASCII.GetBytes(sendstring.ToCharArray());
                networkstream.Write(bufferstring, 0, bufferstring.Length);
                txtContent.AppendText(streamreader.ReadLine() + "
    ");
    
                //断开连接
                networkstream.Close();
                streamreader.Close();
    
                //改变按钮的状态
                btnConn.Enabled = true;
                btnDisconn.Enabled = false;
                btnAccept.Enabled = false;
    
                //将光标置回原来的状态
                Cursor.Current = cr; 
            }
  • 相关阅读:
    30.3 Collections 集合体系的工具类
    30.2 案例:ArrayList本身数据可以重复,写出去重的方法
    30 HashSet
    29.3 ArrayList、List、LinkedList(链表)优缺点
    foreach-- for循环的增强型:for(类型 变量 : 数组对象) { 使用变量 }
    <E> 泛型
    29.2 Iterator 迭代器ConcurrentModificationException:并发修改异常处理
    29.2 Iterator 迭代器
    29.1 collection层次结构 中的根接口
    29 collection 集合体系结构
  • 原文地址:https://www.cnblogs.com/wjshan0808/p/4221506.html
Copyright © 2011-2022 走看看