邮件发送接收
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+"\r\n";//存储用户名 bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray()); networkstream.Write(bufferstring, 0, bufferstring.Length);//将用户名发送到服务器 txtContent.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);//将用户密码发送到服务器 txtContent.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) { txtContent.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(' '); txtContent.AppendText("邮件数量:" + str[1] + "\r\n"); txtContent.AppendText(str[1] + ":封邮件总容量为" + str[2] + "\r\n"); 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 + "\r\n";//存储从服务器获得一条信息的命令 bufferstring = Encoding.ASCII.GetBytes(sendstring.ToCharArray()); networkstream.Write(bufferstring, 0, bufferstring.Length); strResult = streamreader.ReadLine(); if (strResult[0] != '-') { //不断地读取邮件内容,只到结束标志:英文句号 while (strResult != ".") { this.txtContent.AppendText(strResult + "\r\n"); strResult = streamreader.ReadLine(); } } else { this.txtContent.AppendText("\r\n" + "邮件错误" + "\r\n"); } } 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+"\r\n"; bufferstring = Encoding.ASCII.GetBytes(sendstring.ToCharArray()); networkstream.Write(bufferstring, 0, bufferstring.Length); txtContent.AppendText(streamreader.ReadLine() + "\r\n"); //断开连接 networkstream.Close(); streamreader.Close(); //改变按钮的状态 btnConn.Enabled = true; btnDisconn.Enabled = false; btnAccept.Enabled = false; //将光标置回原来的状态 Cursor.Current = cr; }