C#使用POP3接收电子邮件

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 EmailReceiver
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
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.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private TcpClient Server; 
private string SendString;
private byte[] BufferString;
private NetworkStream networkstream;
private StreamReader streamreader;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label4;
 
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
 
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
 
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
 
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}
 
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 16);
this.label1.TabIndex = 0;
this.label1.Text = "POP3 Server:";
// 
// label2
// 
this.label2.Location = new System.Drawing.Point(16, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 1;
this.label2.Text = "User Name:";
// 
// label3
// 
this.label3.Location = new System.Drawing.Point(16, 72);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 16);
this.label3.TabIndex = 2;
this.label3.Text = "Password:";
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(96, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
// 
// textBox2
// 
this.textBox2.Location = new System.Drawing.Point(96, 40);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(168, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
// 
// textBox3
// 
this.textBox3.Location = new System.Drawing.Point(96, 72);
this.textBox3.Name = "textBox3";
this.textBox3.PasswordChar = '*';
this.textBox3.Size = new System.Drawing.Size(168, 21);
this.textBox3.TabIndex = 5;
this.textBox3.Text = "";
// 
// textBox4
// 
this.textBox4.Location = new System.Drawing.Point(136, 152);
this.textBox4.Multiline = true;
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(64, 24);
this.textBox4.TabIndex = 6;
this.textBox4.Text = "";
this.textBox4.WordWrap = false;
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(64, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 7;
this.button1.Text = "Connect";
this.button1.Click += new System.EventHandler(this.ConnectBtn_click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(176, 112);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 24);
this.button2.TabIndex = 8;
this.button2.Text = "Disconnect";
this.button2.Click += new System.EventHandler(this.DisconBtn_click);
// 
// richTextBox1
// 
this.richTextBox1.Location = new System.Drawing.Point(8, 184);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
this.richTextBox1.Size = new System.Drawing.Size(304, 168);
this.richTextBox1.TabIndex = 11;
this.richTextBox1.Text = "";
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(216, 152);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 24);
this.button3.TabIndex = 12;
this.button3.Text = "Receive";
this.button3.Click += new System.EventHandler(this.ReceiveBtn_click);
// 
// label4
// 
this.label4.Location = new System.Drawing.Point(8, 152);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(112, 24);
this.label4.TabIndex = 13;
this.label4.Text = "Enter the number:";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(328, 366);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
 this.label4,
 this.button3,
 this.richTextBox1,
 this.button2,
 this.button1,
 this.textBox4,
 this.textBox3,
 this.textBox2,
 this.textBox1,
 this.label3,
 this.label2,
 this.label1});
this.Name = "Form1";
this.Text = "EmailReceiver";
this.ResumeLayout(false);
 
}
#endregion
 
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
 
private void ConnectBtn_click(object sender, System.EventArgs e)
{
Server=new TcpClient(textBox1.Text,110);
networkstream= Server.GetStream();
streamreader=new StreamReader(networkstream);
try
{
SendString="USER "+textBox2.Text+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
richTextBox1.AppendText(streamreader.ReadLine()+"\r\n");
 
SendString="PASS "+textBox3.Text+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
richTextBox1.AppendText(streamreader.ReadLine()+"\r\n");
 
SendString="STAT"+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
richTextBox1.AppendText(streamreader.ReadLine()+"\r\n");
 
SendString="LIST"+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
richTextBox1.AppendText(streamreader.ReadLine()+"\r\n");
}
catch(InvalidOperationException err)
{MessageBox.Show(err.ToString());}
}
 
private void DisconBtn_click(object sender, System.EventArgs e)
{
try
{
SendString="QUIT\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
richTextBox1.AppendText(streamreader.ReadLine()+"\r\n");
networkstream.Close();
streamreader.Close();
}
catch{MessageBox.Show("The Connection has not exist!");}
 
}
 
private void ReceiveBtn_click(object sender, System.EventArgs e)
{
string TempString;
TempString=" ";
richTextBox1.Clear();
SendString="RETR "+textBox4.Text+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
TempString=streamreader.ReadLine();
if(TempString[0]!='-')
{
while(TempString!=".")
{
TempString=streamreader.ReadLine();
}
TempString=streamreader.ReadLine();
while(TempString!=".")
{
richTextBox1.AppendText(TempString+"\r\n");
TempString=streamreader.ReadLine();
}
}
else
{
richTextBox1.AppendText("\r\n Error!\r\n");
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值