Form1.cs
资源名称:yjal.rar [点击查看]
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:7k
源码类别:
Email客户端
开发平台:
Visual C++
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Text;
- namespace Vc_编写邮件接收程序
- {
- public partial class Form1 : Form
- {
- public TcpClient Server; //用以和服务器建立TCP连接
- public NetworkStream NetStrm; //客户机和服务器会话使用的数据流
- public StreamReader RdStrm; //用以操作数据流,从数据流中获取从服务器反馈的数据
- public string Data;
- public byte[] szData;
- public string CRLF = "rn";
- public Form1()
- {
- InitializeComponent();
- }
- //连接服务器
- private void ConnectBtn_Click(object sender, EventArgs e)
- {
- Cursor cr = Cursor.Current;
- Cursor.Current = Cursors.WaitCursor;
- Server = new TcpClient(POPServ.Text, 110);
- //建立和POP3的TCP连接
- string RecString;
- try
- {
- NetStrm = Server.GetStream();
- //获取客户机和服务器会话的数据流
- RdStrm = new StreamReader(Server.GetStream(), Encoding.GetEncoding("gb2312"));
- RecString = RdStrm.ReadLine();
- MessageBox.Show(RecString);
- Data = "USER " + User.Text + CRLF;
- //定义传送帐户名称的命令
- szData = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Data.ToCharArray());
- NetStrm.Write(szData, 0, szData.Length);
- //向服务器传送帐户
- RecString = RdStrm.ReadLine();
- //接收服务器反馈数据
- MessageBox.Show(RecString);
- Data = "PASS " + Passw.Text + CRLF;
- //定义帐户对应的口令的命令
- szData = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Data.ToCharArray());
- NetStrm.Write(szData, 0, szData.Length);
- //向服务器传送帐户的口令
- RecString = RdStrm.ReadLine();
- //接收服务器反馈数据
- MessageBox.Show(RecString);
- Data = "STAT" + CRLF;
- //定义获取邮箱中邮件数目和占用空间的命令
- szData = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Data.ToCharArray());
- NetStrm.Write(szData, 0, szData.Length);
- //传送胡琴邮箱中邮件数目和占用空间的命令
- RecString = RdStrm.ReadLine();
- MessageBox.Show(RecString);
- //接收服务器反馈的数据
- String[] Num = RecString.Split(' ');
- if (Int32.Parse(Num[1]) > 0)
- {
- statusBar1.Text = "邮件数目:" + Num[1] + ",所占空间:" + Num[2] + "字节";
- numericUpDown1.Maximum = Int32.Parse(Num[1]);
- ConnectBtn.Enabled = false;
- DisconnectBtn.Enabled = true;
- RetrieveBtn.Enabled = true;
- }
- else
- MessageBox.Show("邮箱中没有邮件!");
- //以上是解析STAT命令后服务器反馈的数据,并通过状态条显示出来
- }
- catch (InvalidOperationException err)
- {
- statusBar1.Text = "错误: " + err.ToString();
- }
- Cursor.Current = cr;
- }
- //断开服务器
- private void DisconnectBtn_Click(object sender, EventArgs e)
- {
- string RecString;
- Cursor cr = Cursor.Current;
- Cursor.Current = Cursors.WaitCursor;
- Data = "QUIT" + CRLF;
- //定义关闭会话命令
- szData = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Data.ToCharArray());
- NetStrm.Write(szData, 0, szData.Length);
- //向服务器发送命令
- RecString = RdStrm.ReadLine();
- //接收服务器反馈数据
- NetStrm.Close();
- RdStrm.Close();
- //关闭资源
- ConnectBtn.Enabled = true;
- DisconnectBtn.Enabled = false;
- RetrieveBtn.Enabled = false;
- Cursor.Current = cr;
- }
- //接收邮件中的内容
- private void RetrieveBtn_Click(object sender, EventArgs e)
- {
- Cursor cr = Cursor.Current;
- Cursor.Current = Cursors.WaitCursor;
- string szTemp;
- String[] arrTemp;
- bool strBody = false;
- String[] arrRet = new String[10];
- Message.Clear();
- //清除邮件内容显示区
- try
- {
- Data = "RETR " + numericUpDown1.Value.ToString() + CRLF;
- // 定义接收邮箱中的第几封邮件的命令
- szData = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Data.ToCharArray());
- NetStrm.Write(szData, 0, szData.Length);
- //向服务器发送接收邮件命令
- szTemp = RdStrm.ReadLine();
- MessageBox.Show(szTemp);
- Message.Text=szTemp;
- /*if (szTemp[0] != '-')
- {
- //解析接收的邮件内容,并按条目不同显示
- while (szTemp != ".")
- {
- arrTemp = szTemp.Split(":".ToCharArray());
- if (szTemp == "")
- strBody = true;
- if (arrTemp[0] == "Date")
- arrRet[1] = arrTemp[1];
- if (arrTemp[0] == "From")
- arrRet[2] = arrTemp[1];
- if (arrTemp[0] == "To")
- arrRet[3] = arrTemp[1];
- if (arrTemp[0] == "Subject")
- arrRet[4] = arrTemp[1];
- if (strBody)
- arrRet[5] = arrRet[5] + szTemp;
- Message.Text = "发送时间:" + arrRet[1] + CRLF;
- //显示邮件发送时间信息
- Message.Text = Message.Text + "发送者:" + arrRet[2] + CRLF;
- //显示发信人
- Message.Text = Message.Text + "接收者:" + arrRet[3] + CRLF;
- //显示目的地址
- Message.Text = Message.Text + "主题:" + arrRet[4] + CRLF;
- //显示邮件主题
- Message.Text = Message.Text + "内容:" + arrRet[5] + CRLF;
- //显示邮件内容
- szTemp = RdStrm.ReadLine();
- }
- }*/
- Cursor.Current = cr;
- //回复光标
- }
- catch (InvalidOperationException err)
- {
- statusBar1.Text = "错误: " + err.ToString();
- }
- }
- }
- }