您的位置:首页 > 编程语言 > C#

C#打造邮件接受器VS2005版

2009-12-27 20:32 267 查看
//代码来源:百度搜索关键字--C#打造邮件接受器

//改编原由:希望打造一个收取和处理POP3网易和新浪等常用邮局的邮件的自己的邮件备份软件

//代码修正地方源代码中的str1.Substring(0, 2)修正为str1[0].Substring(0, 2)。估计原文没有测试。

//修正后的代码请在我的上传文件中找寻,命名空间PopEmailManager

//Form1.Designer.cs代码

namespace PopEmailManager
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.textBox3);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(16, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(696, 144);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "连接选项";
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
this.button2.Location = new System.Drawing.Point(584, 96);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 32);
this.button2.TabIndex = 7;
this.button2.Text = "断开连接";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
this.button1.Location = new System.Drawing.Point(584, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 32);
this.button1.TabIndex = 6;
this.button1.Text = "建立连接";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(160, 112);
this.textBox3.Name = "textBox3";
this.textBox3.PasswordChar = '*';
this.textBox3.Size = new System.Drawing.Size(384, 21);
this.textBox3.TabIndex = 5;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(160, 64);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(384, 21);
this.textBox2.TabIndex = 4;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(160, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(384, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "Pop.163.com";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(120, 23);
this.label3.TabIndex = 2;
this.label3.Text = "密码";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(120, 23);
this.label2.TabIndex = 1;
this.label2.Text = "用户名";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 23);
this.label1.TabIndex = 0;
this.label1.Text = "P0P3邮件服务器";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.richTextBox1);
this.groupBox2.Location = new System.Drawing.Point(24, 192);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(544, 224);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "邮件内容";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(520, 184);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.button4);
this.groupBox3.Controls.Add(this.button3);
this.groupBox3.Controls.Add(this.listBox1);
this.groupBox3.Location = new System.Drawing.Point(584, 192);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(136, 224);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "邮件信息";
//
// button4
//
this.button4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
this.button4.Enabled = false;
this.button4.Location = new System.Drawing.Point(72, 192);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(56, 23);
this.button4.TabIndex = 2;
this.button4.Text = "删除";
this.button4.UseVisualStyleBackColor = false;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
this.button3.Enabled = false;
this.button3.Location = new System.Drawing.Point(8, 192);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 23);
this.button3.TabIndex = 1;
this.button3.Text = "阅读";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// listBox1
//
this.listBox1.Enabled = false;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 160);
this.listBox1.TabIndex = 0;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.listBox2);
this.groupBox4.Location = new System.Drawing.Point(24, 424);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(696, 96);
this.groupBox4.TabIndex = 3;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "收信状态";
//
// listBox2
//
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(16, 16);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(672, 76);
this.listBox2.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
this.ClientSize = new System.Drawing.Size(741, 539);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "C#打造邮件接受器VS2005版";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.ListBox listBox2;

}
}



// Form1.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;

namespace PopEmailManager
{
public partial class Form1 : Form
{
public TcpClient Server;
public StreamReader sr;
public StreamWriter sw;
public string Data, content;
public string CRLF = "/r/n";

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
this.listBox2.Items.Clear();
try
{
Server = new TcpClient(this.textBox1.Text, 110);
sr = new StreamReader(Server.GetStream(), System.Text.Encoding.ASCII);
sw = new StreamWriter(Server.GetStream(), System.Text.Encoding.ASCII);
this.listBox2.Items.Add(sr.ReadLine());
Data = "USER " + this.textBox2.Text;
sw.WriteLine(Data);
sw.Flush();
this.listBox2.Items.Add(sr.ReadLine());
Data = "PASS " + this.textBox3.Text;
sw.WriteLine(Data);
sw.Flush();
this.listBox2.Items.Add(sr.ReadLine());
Data = "STAT";
sw.WriteLine(Data);
sw.Flush();
string str = sr.ReadLine();
this.listBox2.Items.Add(str);
str = str.Substring(4, str.IndexOf(" ", 5) - 4);
int count = Int32.Parse(str);
if (count > 0)
{
this.listBox1.Enabled = true;
this.button4.Enabled = true;
this.button3.Enabled = true;
this.listBox1.Items.Clear();
for (int i = 0; i < count; i++)
{
this.listBox1.Items.Add("第" + (i + 1) + "封邮件");
}
this.listBox1.SelectedIndex = 0;
}
else
{
this.listBox1.Items.Add("没有新邮件");
this.listBox1.Enabled = false;
this.button3.Enabled = false;
this.button4.Enabled = false;
}
this.button1.Enabled = false;
this.button2.Enabled = true;
}
catch
{
this.listBox2.Items.Add("有错误发生,请检查");
Server.Close();
sw.Close();
sr.Close();
this.button3.Enabled = false;
this.button4.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;
}

}

private void button2_Click(object sender, EventArgs e)
{
try
{
Data = "QUIT";
sw.WriteLine(Data);
sw.Flush();
this.listBox2.Items.Add(sr.ReadLine());
sw.Close();
sr.Close();
this.richTextBox1.Clear();
this.listBox1.Items.Clear();
this.listBox1.Enabled = false;
this.button3.Enabled = false;
this.button4.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;
Server.Close();
}
catch
{
sw.Close();
sr.Close();
this.richTextBox1.Clear();
this.listBox1.Items.Clear();
this.listBox1.Enabled = false;
this.button3.Enabled = false;
this.button4.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;
Server.Close();
}

}

private void button3_Click(object sender, EventArgs e)
{
String strTemp;
content = "";
this.richTextBox1.Clear();
try
{
string str = this.listBox1.SelectedItem.ToString();
str = str.Substring(1, str.IndexOf("封") - 1);
Data = "RETR " + str;
sw.WriteLine(Data);
sw.Flush();
strTemp = sr.ReadLine();
if (strTemp[0] != '-')
{
while (strTemp != ".")
{
content += strTemp + CRLF;
strTemp = sr.ReadLine();
}
}
else
{
this.listBox2.Items.Add(strTemp);
}
char[] seperator = { '/n' };
string[] str1 = content.Trim().Split(seperator);
for (int i = 0; i <= 9; i++)
{
if (str1.Length != 0)
{
if (str1[0].Substring(0, 2).ToLower() == "to")
{
this.richTextBox1.Text += "发送到" + str1[0].Substring(2, str1.Length - 2);
}
if (str1[1].Substring(0, 4).ToLower() == "from")
{
this.richTextBox1.Text += "来自" + str1[1].Substring(4, str1.Length - 4);
}
if (str1[2].Substring(0, 7).ToLower() == "subject")
{
this.richTextBox1.Text += "标题" + str1[20].Substring(7, str1.Length - 7);
}
if (str1[3].Substring(0, 4).ToLower() == "date")
{
this.richTextBox1.Text += "日期" + str1[3].Substring(4, str1.Length - 4);
}
}
}
this.richTextBox1.Text += "内容: " + CRLF;
for (int j = 12; j < str1.Length; j++)
{
if (str1[j].Trim().Length != 0)
{
this.richTextBox1.Text += str1[j];
}
}
}
catch
{
this.listBox2.Items.Add("ERROR!");
}

}

private void button4_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
try
{
string str = this.listBox1.SelectedItem.ToString();
str = str.Substring(1, str.IndexOf("封") - 1);
Data = "DELE " + str;
sw.WriteLine(Data);
sw.Flush();
this.listBox2.Items.Add(sr.ReadLine());
int i = this.listBox1.SelectedIndex;
this.listBox1.Items.Remove(this.listBox1.Items.ToString());
MessageBox.Show("删除成功", "操作成功");
}
catch
{
this.listBox1.Items.Add("ERROR!");
}

}
public static string FromUnicodeByteArray(byte[] characters)
{
UnicodeEncoding encoding = new UnicodeEncoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
public static string FromASCIIByteArray(byte[] characters)
{
ASCIIEncoding encoding = new ASCIIEncoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
sr.Close();
sw.Close();
Server.Close();
}
catch
{
}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: