c# 收取邮件 解析_c#抓取邮件内容解析 (转)

这是一个C#编写的POP3邮件接收程序,通过连接指定的POP3服务器,输入用户名和密码登录,获取邮件信息,选择邮件编号后可以收取并显示邮件内容。程序还提供了解析邮件头信息的功能,包括发件人、收件人、邮件主题和日期。
摘要由CSDN通过智能技术生成

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;

namespace MailReceiver

{

///

/// Form1 的摘要说明。

///

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.Label label4;

private System.Windows.Forms.Label label5;

private System.Windows.Forms.Label label6;

private System.Windows.Forms.TextBox PopServer;

private System.Windows.Forms.TextBox Username;

private System.Windows.Forms.TextBox Password;

private System.Windows.Forms.RichTextBox Message;

private System.Windows.Forms.TextBox MailNum;

private System.Windows.Forms.ListBox Status;

private System.Windows.Forms.Button Connect;

private System.Windows.Forms.Button Disconnect;

private System.Windows.Forms.Button Retrieve;

private System.Windows.Forms.CheckBox BackupChBox;

public TcpClient Server;

public NetworkStream NetStrm;

public StreamReader  RdStrm;

public string Data;

public byte[] szData;

public string CRLF = "\r\n";

private System.Windows.Forms.Button BtnExit;

private System.Windows.Forms.Label label7;

private System.Windows.Forms.Label label8;

private System.Windows.Forms.Label label9;

private System.Windows.Forms.Label label10;

private System.Windows.Forms.Label label11;

private System.Windows.Forms.TextBox txtMailFrom;

private System.Windows.Forms.TextBox txtMailCharSet;

private System.Windows.Forms.TextBox txtMailTitle;

private System.Windows.Forms.Label label12;

private System.Windows.Forms.Label label13;

private System.Windows.Forms.RichTextBox txtMailContent;

private System.Windows.Forms.Button btnExplit;

private System.Windows.Forms.TextBox txtMailDate;

private System.Windows.Forms.TextBox txtMailTo;

///

/// 必需的设计器变量。

///

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

///

/// 清理所有正在使用的资源。

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.label4 = new System.Windows.Forms.Label();

this.label5 = new System.Windows.Forms.Label();

this.label6 = new System.Windows.Forms.Label();

this.PopServer = new System.Windows.Forms.TextBox();

this.Username = new System.Windows.Forms.TextBox();

this.Password = new System.Windows.Forms.TextBox();

this.MailNum = new System.Windows.Forms.TextBox();

this.Connect = new System.Windows.Forms.Button();

this.Disconnect = new System.Windows.Forms.Button();

this.Retrieve = new System.Windows.Forms.Button();

this.Message = new System.Windows.Forms.RichTextBox();

this.Status = new System.Windows.Forms.ListBox();

this.BackupChBox = new System.Windows.Forms.CheckBox();

this.BtnExit = new System.Windows.Forms.Button();

this.label7 = new System.Windows.Forms.Label();

this.label8 = new System.Windows.Forms.Label();

this.label9 = new System.Windows.Forms.Label();

this.label10 = new System.Windows.Forms.Label();

this.label11 = new System.Windows.Forms.Label();

this.txtMailFrom = new System.Windows.Forms.TextBox();

this.txtMailDate = new System.Windows.Forms.TextBox();

this.txtMailTitle = new System.Windows.Forms.TextBox();

this.txtMailTo = new System.Windows.Forms.TextBox();

this.txtMailCharSet = new System.Windows.Forms.TextBox();

this.label12 = new System.Windows.Forms.Label();

this.label13 = new System.Windows.Forms.Label();

this.txtMailContent = new System.Windows.Forms.RichTextBox();

this.btnExplit = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// label1

//

this.label1.Location = new System.Drawing.Point(16, 16);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(112, 23);

this.label1.TabIndex = 0;

this.label1.Text = "POP3 服务器地址:";

this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// label2

//

this.label2.Location = new System.Drawing.Point(16, 48);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(56, 16);

this.label2.TabIndex = 1;

this.label2.Text = "用户名:";

this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// label3

//

this.label3.Location = new System.Drawing.Point(216, 48);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(48, 16);

this.label3.TabIndex = 2;

this.label3.Text = "密码:";

this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// label4

//

this.label4.Location = new System.Drawing.Point(304, 80);

this.label4.Name = "label4";

this.label4.Size = new System.Drawing.Size(72, 23);

this.label4.TabIndex = 3;

this.label4.Text = "邮件编号:";

this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// label5

//

this.label5.Location = new System.Drawing.Point(16, 80);

this.label5.Name = "label5";

this.label5.Size = new System.Drawing.Size(100, 16);

this.label5.TabIndex = 4;

this.label5.Text = "信息:";

this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// label6

//

this.label6.Location = new System.Drawing.Point(16, 584);

this.label6.Name = "label6";

this.label6.Size = new System.Drawing.Size(100, 16);

this.label6.TabIndex = 5;

this.label6.Text = "状态:";

this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// PopServer

//

this.PopServer.Location = new System.Drawing.Point(136, 16);

this.PopServer.Name = "PopServer";

this.PopServer.Size = new System.Drawing.Size(168, 21);

this.PopServer.TabIndex = 0;

this.PopServer.Text = "pop.126.com";

//

// Username

//

this.Username.Location = new System.Drawing.Point(96, 48);

this.Username.Name = "Username";

this.Username.Size = new System.Drawing.Size(104, 21);

this.Username.TabIndex = 1;

this.Username.Text = "Hnxqf222";

//

// Password

//

this.Password.Location = new System.Drawing.Point(280, 48);

this.Password.Name = "Password";

this.Password.PasswordChar = '*';

this.Password.Size = new System.Drawing.Size(104, 21);

this.Password.TabIndex = 2;

this.Password.Text = "iwillwin";

//

// MailNum

//

this.MailNum.Location = new System.Drawing.Point(384, 80);

this.MailNum.Name = "MailNum";

this.MailNum.Size = new System.Drawing.Size(40, 21);

this.MailNum.TabIndex = 4;

this.MailNum.Text = "";

//

// Connect

//

this.Connect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

this.Connect.Location = new System.Drawing.Point(448, 8);

this.Connect.Name = "Connect";

this.Connect.Size = new System.Drawing.Size(75, 24);

this.Connect.TabIndex = 7;

this.Connect.Text = "连接邮局";

this.Connect.Click += new System.EventHandler(this.Connect_Click);

//

// Disconnect

//

this.Disconnect.Enabled = false;

this.Disconnect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

this.Disconnect.Location = new System.Drawing.Point(552, 8);

this.Disconnect.Name = "Disconnect";

this.Disconnect.Size = new System.Drawing.Size(75, 24);

this.Disconnect.TabIndex = 8;

this.Disconnect.Text = "断开连接";

this.Disconnect.Click += new System.EventHandler(this.Disconnect_Click);

//

// Retrieve

//

this.Retrieve.Enabled = false;

this.Retrieve.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

this.Retrieve.Location = new System.Drawing.Point(616, 80);

this.Retrieve.Name = "Retrieve";

this.Retrieve.Size = new System.Drawing.Size(72, 24);

this.Retrieve.TabIndex = 9;

this.Retrieve.Text = "收取邮件";

this.Retrieve.Click += new System.EventHandler(this.Retrieve_Click);

//

// Message

//

this.Message.Location = new System.Drawing.Point(8, 112);

this.Message.Name = "Message";

this.Message.ShowSelectionMargin = true;

this.Message.Size = new System.Drawing.Size(704, 136);

this.Message.TabIndex = 3;

this.Message.Text = "";

//

// Status

//

this.Status.HorizontalScrollbar = true;

this.Status.ItemHeight = 12;

this.Status.Location = new System.Drawing.Point(8, 608);

this.Status.Name = "Status";

this.Status.Size = new System.Drawing.Size(704, 136);

this.Status.Sorted = true;

this.Status.TabIndex = 6;

//

// BackupChBox

//

this.BackupChBox.Checked = true;

this.BackupChBox.CheckState = System.Windows.Forms.CheckState.Checked;

this.BackupChBox.Location = new System.Drawing.Point(440, 80);

this.BackupChBox.Name = "BackupChBox";

this.BackupChBox.Size = new System.Drawing.Size(160, 24);

this.BackupChBox.TabIndex = 5;

this.BackupChBox.Text = "在邮件服务器上保留备份";

//

// BtnExit

//

this.BtnExit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

this.BtnExit.Location = new System.Drawing.Point(648, 8);

this.BtnExit.Name = "BtnExit";

this.BtnExit.Size = new System.Drawing.Size(75, 24);

this.BtnExit.TabIndex = 11;

this.BtnExit.Text = "退出程序";

this.BtnExit.Click += new System.EventHandler(this.BtnExit_Click);

//

// label7

//

this.label7.Location = new System.Drawing.Point(8, 264);

this.label7.Name = "label7";

this.label7.Size = new System.Drawing.Size(64, 23);

this.label7.TabIndex = 12;

this.label7.Text = "发件人:";

//

// label8

//

this.label8.Location = new System.Drawing.Point(8, 296);

this.label8.Name = "label8";

this.label8.Size = new System.Drawing.Size(72, 23);

this.label8.TabIndex = 13;

this.label8.Text = "发信时间:";

//

// label9

//

this.label9.Location = new System.Drawing.Point(8, 328);

this.label9.Name = "label9";

this.label9.Size = new System.Drawing.Size(72, 23);

this.label9.TabIndex = 14;

this.label9.Text = "邮件标题:";

//

// label10

//

this.label10.Location = new System.Drawing.Point(304, 264);

this.label10.Name = "label10";

this.label10.Size = new System.Drawing.Size(72, 23);

this.label10.TabIndex = 15;

this.label10.Text = "收件人:";

//

// label11

//

this.label11.Location = new System.Drawing.Point(304, 296);

this.label11.Name = "label11";

this.label11.Size = new System.Drawing.Size(72, 23);

this.label11.TabIndex = 16;

this.label11.Text = "内容编码:";

//

// txtMailFrom

//

this.txtMailFrom.Location = new System.Drawing.Point(96, 264);

this.txtMailFrom.Name = "txtMailFrom";

this.txtMailFrom.Size = new System.Drawing.Size(184, 21);

this.txtMailFrom.TabIndex = 17;

this.txtMailFrom.Text = "";

//

// txtMailDate

//

this.txtMailDate.Location = new System.Drawing.Point(96, 296);

this.txtMailDate.Name = "txtMailDate";

this.txtMailDate.Size = new System.Drawing.Size(184, 21);

this.txtMailDate.TabIndex = 18;

this.txtMailDate.Text = "";

//

// txtMailTitle

//

this.txtMailTitle.Location = new System.Drawing.Point(96, 328);

this.txtMailTitle.Name = "txtMailTitle";

this.txtMailTitle.Size = new System.Drawing.Size(392, 21);

this.txtMailTitle.TabIndex = 19;

this.txtMailTitle.Text = "";

//

// txtMailTo

//

this.txtMailTo.Location = new System.Drawing.Point(384, 264);

this.txtMailTo.Name = "txtMailTo";

this.txtMailTo.Size = new System.Drawing.Size(184, 21);

this.txtMailTo.TabIndex = 20;

this.txtMailTo.Text = "";

//

// txtMailCharSet

//

this.txtMailCharSet.Location = new System.Drawing.Point(384, 296);

this.txtMailCharSet.Name = "txtMailCharSet";

this.txtMailCharSet.TabIndex = 21;

this.txtMailCharSet.Text = "";

//

// label12

//

this.label12.Location = new System.Drawing.Point(496, 296);

this.label12.Name = "label12";

this.label12.Size = new System.Drawing.Size(72, 23);

this.label12.TabIndex = 22;

this.label12.Text = "邮件长度";

//

// label13

//

this.label13.Location = new System.Drawing.Point(8, 392);

this.label13.Name = "label13";

this.label13.Size = new System.Drawing.Size(80, 16);

this.label13.TabIndex = 24;

this.label13.Text = "邮件内容:";

this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// txtMailContent

//

this.txtMailContent.Location = new System.Drawing.Point(8, 416);

this.txtMailContent.Name = "txtMailContent";

this.txtMailContent.ShowSelectionMargin = true;

this.txtMailContent.Size = new System.Drawing.Size(704, 152);

this.txtMailContent.TabIndex = 25;

this.txtMailContent.Text = "";

//

// btnExplit

//

this.btnExplit.Enabled = false;

this.btnExplit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

this.btnExplit.Location = new System.Drawing.Point(624, 264);

this.btnExplit.Name = "btnExplit";

this.btnExplit.Size = new System.Drawing.Size(72, 24);

this.btnExplit.TabIndex = 26;

this.btnExplit.Text = "解析内容";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(728, 753);

this.Controls.Add(this.btnExplit);

this.Controls.Add(this.txtMailContent);

this.Controls.Add(this.label13);

this.Controls.Add(this.label12);

this.Controls.Add(this.txtMailCharSet);

this.Controls.Add(this.txtMailTo);

this.Controls.Add(this.txtMailTitle);

this.Controls.Add(this.txtMailDate);

this.Controls.Add(this.txtMailFrom);

this.Controls.Add(this.label11);

this.Controls.Add(this.label10);

this.Controls.Add(this.label9);

this.Controls.Add(this.label8);

this.Controls.Add(this.label7);

this.Controls.Add(this.BtnExit);

this.Controls.Add(this.BackupChBox);

this.Controls.Add(this.Status);

this.Controls.Add(this.Message);

this.Controls.Add(this.Retrieve);

this.Controls.Add(this.Disconnect);

this.Controls.Add(this.Connect);

this.Controls.Add(this.MailNum);

this.Controls.Add(this.Password);

this.Controls.Add(this.Username);

this.Controls.Add(this.PopServer);

this.Controls.Add(this.label6);

this.Controls.Add(this.label5);

this.Controls.Add(this.label4);

this.Controls.Add(this.label3);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.MaximizeBox = false;

this.Name = "Form1";

this.Text = "POP3邮件接收程序";

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

///

/// 应用程序的主入口点。

///

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Connect_Click(object sender, System.EventArgs e)

{

//将光标置为等待状态

Cursor cr = Cursor.Current;

Cursor.Current = Cursors.WaitCursor;

//用110端口新建POP3服务器连接

Server = new TcpClient(PopServer.Text,110);

Status.Items.Clear();

try

{

//初始化

NetStrm = Server.GetStream();

RdStrm= new StreamReader(Server.GetStream());

Status.Items.Add(RdStrm.ReadLine());

//登录服务器过程

Data = "USER "+ Username.Text+CRLF;

szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());

NetStrm.Write(szData,0,szData.Length);

Status.Items.Add(RdStrm.ReadLine());

Data = "PASS "+ Password.Text+CRLF;

szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());

NetStrm.Write(szData,0,szData.Length);

Status.Items.Add(RdStrm.ReadLine());

//向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小

Data = "STAT"+CRLF;

szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());

NetStrm.Write(szData,0,szData.Length);

Status.Items.Add(RdStrm.ReadLine());

//改变按钮的状态

Connect.Enabled = false;

Disconnect.Enabled = true;

Retrieve.Enabled = true;

//将光标置回原来状态

Cursor.Current = cr;

}

catch(InvalidOperationException err)

{

Status.Items.Add("Error: "+err.ToString());

}

}

private void Disconnect_Click(object sender, System.EventArgs e)

{

//将光标置为等待状态

Cursor cr = Cursor.Current;

Cursor.Current = Cursors.WaitCursor;

//向服务器发送QUIT命令从而结束和POP3服务器的会话

Data = "QUIT"+CRLF;

szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());

NetStrm.Write(szData,0,szData.Length);

Status.Items.Add(RdStrm.ReadLine());

//断开连接

NetStrm.Close();

RdStrm.Close();

//改变按钮的状态

Connect.Enabled = true;

Disconnect.Enabled = false;

Retrieve.Enabled = false;

//将光标置回原来状态

Cursor.Current = cr;

}

private void Retrieve_Click(object sender, System.EventArgs e)

{

//将光标置为等待状态

Cursor cr = Cursor.Current;

Cursor.Current = Cursors.WaitCursor;

string szTemp;

string MailContent;

MailContent="";

Message.Clear();

try

{

//根据邮件编号从服务器获得相应邮件

Data = "RETR "+ MailNum.Text+CRLF;

szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());

NetStrm.Write(szData,0,szData.Length);

szTemp = RdStrm.ReadLine();

string MailFrom="";

string MailTo="";

string MailTitle="";

string MailDate="";

if(szTemp[0]!='-')

{

//不断地读取邮件内容,只到结束标志:英文句号

int k=0;

while(szTemp!=".")

{

//Message.Text +="第"+i.ToString()+"行"+ szTemp + "\n";

MailContent+=szTemp+"\n";

szTemp = RdStrm.ReadLine();

if (k<10&&k>4)

{

txtMailContent.Text+=k.ToString();

if(szTemp.Substring(0,2).ToLower()=="to")

{

MailTo="发送到"+szTemp.Substring(2,szTemp.Length-2);

}

if(szTemp.Substring(0,4).ToLower()=="from")

{

MailFrom="来自"+szTemp.Substring(4,szTemp.Length-4);

}

if(szTemp.Substring(0,7).ToLower()=="subject")

{

MailTitle="标题"+szTemp.Substring(7,szTemp.Length-7);

}

if(szTemp.Substring(0,4).ToLower()=="date")

{

MailDate+="日期"+szTemp.Substring(4,szTemp.Length-4);

}

}

k=k+1;

}

Message.Text=MailContent;

//txtMailContent=MailContent;

txtMailFrom.Text=MailFrom;

txtMailTo.Text=MailTo;

txtMailTitle.Text=MailTitle;

txtMailDate.Text=MailDate;

//若BackupChBox未选中,则收取邮件后,删除保留在服务器上的邮件

if(BackupChBox.Checked == false)

{

Data = "DELE" + MailNum.Text + CRLF;

szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());

NetStrm.Write(szData,0,szData.Length);

Status.Items.Add(RdStrm.ReadLine());

}

/*

//解析抓取过来的各种邮件部分

//

//txtMailContent.Clear();

char[] seperator = {'\n'};

string EmailContent;

EmailContent=content.ToString().Trim();

string[] str1 =EmailContent.Split(seperator,10);

for(int i=0;i<=9;i++)

{

//txtMailContent.Text+="\n"+str1.ToString();

/*126邮局的前面9行的内容

+OK 1161 octets

Received: from xqf222 (unknown [222.248.201.237])

by smtp3 (Coremail) with SMTP id wKjSjUpAVAFnNKJE39QhAA==.15858S2;

Wed, 28 Jun 2006 15:48:56 +0800 (CST)

thread-index: Acaah0d07Liy0sFNTsK6weudE0ML7A==

Thread-Topic: =?gb2312?B?1eLA78zu0LS499Pv1tbTyrz+serM4g==?=

From:

To:

Subject: =?gb2312?B?1eLA78zu0LS499Pv1tbTyrz+serM4g==?=

if(str1.ToString().Trim().Length!=0)

{

txtMailContent.Text+="\n解析内容"+str1.ToString();

if(str1.ToString().Substring(0,2).ToLower()=="to")

{

txtMailContent.Text+="发送到"+str1.ToString().Substring(2,str1.ToString().Length-2);

}

else

{

txtMailContent.Text+="\n没有抓取到收件人信息"+str1.ToString().Substring(0,2).ToLower();

}

if(str1.ToString().Substring(0,4).ToLower()=="from")

{

txtMailContent.Text+="来自"+str1.ToString().Substring(4,str1.ToString().Length-4);

}

else

{

txtMailContent.Text+="\n没有抓取到发件人信息"+str1.ToString().Substring(0,4).ToLower();

}

if(str1.ToString().Substring(0,7).ToLower()=="subject")

{

txtMailContent.Text+="标题"+str1.ToString().Substring(7,str1.ToString().Length-7);

}

else

{

txtMailContent.Text+="\n没有抓取到邮件标题信息"+str1.ToString().Substring(0,7).ToLower();

}

if(str1.ToString().Substring(0,4).ToLower()=="date")

{

txtMailContent.Text+="日期"+str1.ToString().Substring(4,str1.ToString().Length-4);

}

else

{

txtMailContent.Text+="\n没有抓取到邮件日期信息"+str1.ToString().Substring(0,4).ToLower();

}

txtMailContent.Text+="\n循环数组时候结束";

}

else

{

txtMailContent.Text="\n没有抓取到任何数据";

}

}

/*txtMailContent.Text+="内容: "+CRLF;

for(int j=12;j

{

if(str1[j].Trim().Length!=0)

{

txtMailContent.Text+=str1[j];

}

}

*/

//

}

else

{

Status.Items.Add(szTemp);

}

//将光标置回原来状态

Cursor.Current = cr;

//解析内容按钮显示

btnExplit.Enabled=true;

}

catch(InvalidOperationException err)

{

Status.Items.Add("Error: "+err.ToString());

}

}

private void BtnExit_Click(object sender, System.EventArgs e)

{

Application.Exit();

}

private void Form1_Load(object sender, System.EventArgs e)

{

}

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值