jmail收取POP3邮件代码-C#应用程序

来源:互联网 发布:.net php java对比 编辑:程序博客网 时间:2024/05/22 06:40

安装JMAIL4.4 PROFESSION

工程引用JMAIL4.4 LIBRARY

JMAIL收邮件代码.能读所有的邮件数目,然后选择下拉菜单中的数目可以查看不同的邮件内容.
缺点,text/html邮件显示的是HTML代码的.只能收取开放了POP3功能的邮件

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Web;
using jmail;
namespace JmailMamage
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  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.Button button2;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.ComboBox comboBox1;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Label label8;
  private System.Windows.Forms.TextBox txtMailTime;
  private System.Windows.Forms.TextBox txtMailError;
  private System.Windows.Forms.TextBox txtMailContent;
  private System.Windows.Forms.TextBox txtMailFrom;
  private System.Windows.Forms.TextBox txtMailTitle;
  private System.Windows.Forms.TextBox txtMailAttack;
  private System.Windows.Forms.Label label9;
  private System.Windows.Forms.TextBox txtMailSize;
  private System.Windows.Forms.Label label10;
  private System.Windows.Forms.Label label11;
  private System.Windows.Forms.TextBox txtMailFormat;
  private System.Windows.Forms.Label label12;
  private System.Windows.Forms.TextBox txtMailMime;
  private System.Windows.Forms.TextBox txEncoding;
  private System.Windows.Forms.TextBox txtMailCharset;
  private System.Windows.Forms.Label label13;
  private System.Windows.Forms.TextBox txtTotal;
  private System.Windows.Forms.Label label14;
  private System.Windows.Forms.Label label15;
  private System.Windows.Forms.Label label16;
  private System.Windows.Forms.Label label17;
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.TextBox txtPop3;
  private System.Windows.Forms.TextBox txtPassword;
  private System.Windows.Forms.TextBox txtPort;
  private System.Windows.Forms.TextBox txtUserName;
  /// <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 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.txtMailFrom = new System.Windows.Forms.TextBox();
   this.label2 = new System.Windows.Forms.Label();
   this.txtMailTitle = new System.Windows.Forms.TextBox();
   this.label3 = new System.Windows.Forms.Label();
   this.txtMailContent = new System.Windows.Forms.TextBox();
   this.label4 = new System.Windows.Forms.Label();
   this.txtMailAttack = new System.Windows.Forms.TextBox();
   this.button2 = new System.Windows.Forms.Button();
   this.label5 = new System.Windows.Forms.Label();
   this.txtMailError = new System.Windows.Forms.TextBox();
   this.label6 = new System.Windows.Forms.Label();
   this.txtMailTime = new System.Windows.Forms.TextBox();
   this.comboBox1 = new System.Windows.Forms.ComboBox();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.label8 = new System.Windows.Forms.Label();
   this.txtMailCharset = new System.Windows.Forms.TextBox();
   this.txtMailSize = new System.Windows.Forms.TextBox();
   this.label9 = new System.Windows.Forms.Label();
   this.txEncoding = new System.Windows.Forms.TextBox();
   this.label10 = new System.Windows.Forms.Label();
   this.label11 = new System.Windows.Forms.Label();
   this.txtMailFormat = new System.Windows.Forms.TextBox();
   this.txtMailMime = new System.Windows.Forms.TextBox();
   this.label12 = new System.Windows.Forms.Label();
   this.label13 = new System.Windows.Forms.Label();
   this.txtTotal = new System.Windows.Forms.TextBox();
   this.label14 = new System.Windows.Forms.Label();
   this.txtPop3 = new System.Windows.Forms.TextBox();
   this.label15 = new System.Windows.Forms.Label();
   this.txtUserName = new System.Windows.Forms.TextBox();
   this.label16 = new System.Windows.Forms.Label();
   this.txtPort = new System.Windows.Forms.TextBox();
   this.label17 = new System.Windows.Forms.Label();
   this.txtPassword = new System.Windows.Forms.TextBox();
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.groupBox1.SuspendLayout();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(408, 392);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "测试";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 144);
   this.label1.Name = "label1";
   this.label1.TabIndex = 1;
   this.label1.Text = "发件人";
   //
   // txtMailFrom
   //
   this.txtMailFrom.Location = new System.Drawing.Point(136, 144);
   this.txtMailFrom.Name = "txtMailFrom";
   this.txtMailFrom.Size = new System.Drawing.Size(432, 21);
   this.txtMailFrom.TabIndex = 2;
   this.txtMailFrom.Text = "";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(8, 184);
   this.label2.Name = "label2";
   this.label2.TabIndex = 3;
   this.label2.Text = "邮件标题";
   //
   // txtMailTitle
   //
   this.txtMailTitle.Location = new System.Drawing.Point(136, 184);
   this.txtMailTitle.Name = "txtMailTitle";
   this.txtMailTitle.Size = new System.Drawing.Size(432, 21);
   this.txtMailTitle.TabIndex = 4;
   this.txtMailTitle.Text = "";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(8, 392);
   this.label3.Name = "label3";
   this.label3.TabIndex = 5;
   this.label3.Text = "邮件内容";
   //
   // txtMailContent
   //
   this.txtMailContent.Location = new System.Drawing.Point(0, 416);
   this.txtMailContent.Multiline = true;
   this.txtMailContent.Name = "txtMailContent";
   this.txtMailContent.Size = new System.Drawing.Size(576, 168);
   this.txtMailContent.TabIndex = 6;
   this.txtMailContent.Text = "";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(8, 224);
   this.label4.Name = "label4";
   this.label4.TabIndex = 7;
   this.label4.Text = "附件数量";
   //
   // txtMailAttack
   //
   this.txtMailAttack.Location = new System.Drawing.Point(136, 224);
   this.txtMailAttack.Name = "txtMailAttack";
   this.txtMailAttack.Size = new System.Drawing.Size(432, 21);
   this.txtMailAttack.TabIndex = 8;
   this.txtMailAttack.Text = "";
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(504, 392);
   this.button2.Name = "button2";
   this.button2.TabIndex = 9;
   this.button2.Text = "收信";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // label5
   //
   this.label5.Location = new System.Drawing.Point(32, 592);
   this.label5.Name = "label5";
   this.label5.TabIndex = 10;
   this.label5.Text = "错误报告";
   //
   // txtMailError
   //
   this.txtMailError.Location = new System.Drawing.Point(136, 592);
   this.txtMailError.Multiline = true;
   this.txtMailError.Name = "txtMailError";
   this.txtMailError.Size = new System.Drawing.Size(440, 72);
   this.txtMailError.TabIndex = 11;
   this.txtMailError.Text = "";
   //
   // label6
   //
   this.label6.Location = new System.Drawing.Point(8, 264);
   this.label6.Name = "label6";
   this.label6.TabIndex = 12;
   this.label6.Text = "发信时间";
   //
   // txtMailTime
   //
   this.txtMailTime.Location = new System.Drawing.Point(136, 264);
   this.txtMailTime.Name = "txtMailTime";
   this.txtMailTime.Size = new System.Drawing.Size(104, 21);
   this.txtMailTime.TabIndex = 13;
   this.txtMailTime.Text = "";
   //
   // comboBox1
   //
   this.comboBox1.Location = new System.Drawing.Point(240, 392);
   this.comboBox1.Name = "comboBox1";
   this.comboBox1.Size = new System.Drawing.Size(80, 20);
   this.comboBox1.TabIndex = 14;
   this.comboBox1.Text = "1";
   this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(160, 392);
   this.button3.Name = "button3";
   this.button3.TabIndex = 15;
   this.button3.Text = "打开连接";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(328, 392);
   this.button4.Name = "button4";
   this.button4.TabIndex = 16;
   this.button4.Text = "关闭连接";
   //
   // label8
   //
   this.label8.Location = new System.Drawing.Point(328, 264);
   this.label8.Name = "label8";
   this.label8.TabIndex = 18;
   this.label8.Text = "语言编码";
   //
   // txtMailCharset
   //
   this.txtMailCharset.Location = new System.Drawing.Point(432, 264);
   this.txtMailCharset.Name = "txtMailCharset";
   this.txtMailCharset.TabIndex = 19;
   this.txtMailCharset.Text = "";
   //
   // txtMailSize
   //
   this.txtMailSize.Location = new System.Drawing.Point(432, 296);
   this.txtMailSize.Name = "txtMailSize";
   this.txtMailSize.TabIndex = 20;
   this.txtMailSize.Text = "";
   //
   // label9
   //
   this.label9.Location = new System.Drawing.Point(328, 296);
   this.label9.Name = "label9";
   this.label9.TabIndex = 21;
   this.label9.Text = "邮件大小";
   //
   // txEncoding
   //
   this.txEncoding.Location = new System.Drawing.Point(136, 328);
   this.txEncoding.Name = "txEncoding";
   this.txEncoding.TabIndex = 22;
   this.txEncoding.Text = "";
   //
   // label10
   //
   this.label10.Location = new System.Drawing.Point(8, 328);
   this.label10.Name = "label10";
   this.label10.TabIndex = 23;
   this.label10.Text = "Encoding";
   //
   // label11
   //
   this.label11.Location = new System.Drawing.Point(8, 296);
   this.label11.Name = "label11";
   this.label11.TabIndex = 24;
   this.label11.Text = "内容格式";
   //
   // txtMailFormat
   //
   this.txtMailFormat.Location = new System.Drawing.Point(136, 296);
   this.txtMailFormat.Name = "txtMailFormat";
   this.txtMailFormat.TabIndex = 25;
   this.txtMailFormat.Text = "";
   //
   // txtMailMime
   //
   this.txtMailMime.Location = new System.Drawing.Point(432, 360);
   this.txtMailMime.Name = "txtMailMime";
   this.txtMailMime.TabIndex = 26;
   this.txtMailMime.Text = "";
   //
   // label12
   //
   this.label12.Location = new System.Drawing.Point(328, 360);
   this.label12.Name = "label12";
   this.label12.TabIndex = 27;
   this.label12.Text = "Mime Ver";
   //
   // label13
   //
   this.label13.Location = new System.Drawing.Point(328, 328);
   this.label13.Name = "label13";
   this.label13.TabIndex = 28;
   this.label13.Text = "邮件总数";
   //
   // txtTotal
   //
   this.txtTotal.Location = new System.Drawing.Point(432, 328);
   this.txtTotal.Name = "txtTotal";
   this.txtTotal.TabIndex = 29;
   this.txtTotal.Text = "";
   //
   // label14
   //
   this.label14.Location = new System.Drawing.Point(16, 32);
   this.label14.Name = "label14";
   this.label14.TabIndex = 30;
   this.label14.Text = "POP3地址";
   //
   // txtPop3
   //
   this.txtPop3.Location = new System.Drawing.Point(144, 32);
   this.txtPop3.Name = "txtPop3";
   this.txtPop3.TabIndex = 31;
   this.txtPop3.Text = "pop3.163.com";
   //
   // label15
   //
   this.label15.Location = new System.Drawing.Point(16, 64);
   this.label15.Name = "label15";
   this.label15.TabIndex = 32;
   this.label15.Text = "用户名";
   //
   // txtUserName
   //
   this.txtUserName.Location = new System.Drawing.Point(144, 64);
   this.txtUserName.Name = "txtUserName";
   this.txtUserName.TabIndex = 33;
   this.txtUserName.Text = "hnxqf222";
   //
   // label16
   //
   this.label16.Location = new System.Drawing.Point(296, 32);
   this.label16.Name = "label16";
   this.label16.TabIndex = 34;
   this.label16.Text = "端口";
   //
   // txtPort
   //
   this.txtPort.Location = new System.Drawing.Point(432, 32);
   this.txtPort.Name = "txtPort";
   this.txtPort.TabIndex = 35;
   this.txtPort.Text = "110";
   //
   // label17
   //
   this.label17.Location = new System.Drawing.Point(296, 64);
   this.label17.Name = "label17";
   this.label17.TabIndex = 36;
   this.label17.Text = "密码";
   //
   // txtPassword
   //
   this.txtPassword.Location = new System.Drawing.Point(432, 64);
   this.txtPassword.Name = "txtPassword";
   this.txtPassword.PasswordChar = '*';
   this.txtPassword.TabIndex = 37;
   this.txtPassword.Text = "";
   //
   // groupBox1
   //
   this.groupBox1.Controls.Add(this.txtPassword);
   this.groupBox1.Controls.Add(this.label17);
   this.groupBox1.Controls.Add(this.txtPort);
   this.groupBox1.Controls.Add(this.label16);
   this.groupBox1.Controls.Add(this.label15);
   this.groupBox1.Controls.Add(this.txtUserName);
   this.groupBox1.Controls.Add(this.txtPop3);
   this.groupBox1.Controls.Add(this.label14);
   this.groupBox1.Location = new System.Drawing.Point(0, 8);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(576, 104);
   this.groupBox1.TabIndex = 38;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "P0P3收件人信箱设置";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(584, 669);
   this.Controls.Add(this.groupBox1);
   this.Controls.Add(this.txtTotal);
   this.Controls.Add(this.label13);
   this.Controls.Add(this.label12);
   this.Controls.Add(this.txtMailMime);
   this.Controls.Add(this.txtMailFormat);
   this.Controls.Add(this.label11);
   this.Controls.Add(this.label10);
   this.Controls.Add(this.txEncoding);
   this.Controls.Add(this.label9);
   this.Controls.Add(this.txtMailSize);
   this.Controls.Add(this.txtMailCharset);
   this.Controls.Add(this.label8);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.comboBox1);
   this.Controls.Add(this.txtMailTime);
   this.Controls.Add(this.label6);
   this.Controls.Add(this.txtMailError);
   this.Controls.Add(this.label5);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.txtMailAttack);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.txtMailContent);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.txtMailTitle);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.txtMailFrom);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.groupBox1.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   jmail.POP3 jEmail=new jmail.POP3Class();
   jmail.Message jMessage=new jmail.MessageClass();
   string UserName="csharp222";
    string Password="123456";
   string MailServer="pop3.126.com";
   jEmail.Connect(UserName, Password, MailServer, 110);
   txtMailTitle.Text=jEmail.Count.ToString();
   jEmail.Disconnect();

  // if(jEmail.Size>0)
  // {
           
    txtMailTitle.Text=jMessage.FromName;
    txtMailTitle.Text=jMessage.Subject;
    txtMailTime.Text=jMessage.Date.ToString();
    txtMailCharset.Text=jMessage.Body;
      
  // }
  

  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   jmail.POP3   mypop3   =   new   jmail.POP3Class();  
   try  
   {  
    mypop3.Connect("hnxqf222","iwillwin","pop3.163.com",110);     
    
   }  
   catch(Exception ex)  
   {  
    string   str   =   "";  
    str   =   ex.Message.ToString();
    txtMailContent.Text=str;
   }
   
   mypop3.Logging=true;   
   jmail.MessageClass Mail=new MessageClass(); 
 
   Mail=(MessageClass)mypop3.Messages[1];  
   txtMailTitle.Text= Mail.FromName;//发件人
   txtMailTitle.Text=Mail.Subject;//邮件主题
   txtMailContent.Text=Mail.Body;//邮件内容

   //txtMailCode.Text=Mail.Attachments.Count.ToString();//附件数
   /*

   int Ncount = mypop3.Messages.Count; 
   for(int  mailNum=1;mailNum<Ncount;mailNum++)  
   {  
    Mail=(MessageClass)mypop3.Messages[mailNum];  
    txtMailTitle.Text= Mail.FromName;//发件人
    txtMailTitle.Text=Mail.Subject;//邮件主题
    txtMailTime.Text=Mail.Body;//邮件内容
    txtMailCode.Text=Mail.Attachments.Count.ToString();//附件数
    
   }
   */

  }

  private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   
    int ENumber=int.Parse(comboBox1.Text);
    GetPOP3Email(ENumber);
   
   
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   GetPOP3Email(1);

   //txtMailCode.Text=Mail.Attachments.Count.ToString();//附件数
   /*

   int Ncount = mypop3.Messages.Count; 
   for(int  mailNum=1;mailNum<Ncount;mailNum++)  
   {  
    Mail=(MessageClass)mypop3.Messages[mailNum];  
    txtMailTitle.Text= Mail.FromName;//发件人
    txtMailTitle.Text=Mail.Subject;//邮件主题
    txtMailTime.Text=Mail.Body;//邮件内容
    txtMailCode.Text=Mail.Attachments.Count.ToString();//附件数
    
   }
   */

   

  }
  public void GetPOP3Email(int EmailNum)
  {
   jmail.POP3   mypop3   =   new   jmail.POP3Class();  
   try  
   {  
    //mypop3.Connect("hnxqf222","iwillwin","pop3.163.com",110);
    string MailUserName=txtUserName.Text;
    string MailPassword=txtPassword.Text;
    string MailPop3Address=txtPop3.Text;
    int MailPop3Port=int.Parse(txtPort.Text);
        
    mypop3.Connect(MailUserName,MailPassword,MailPop3Address,MailPop3Port);  
    
   }  
   catch(Exception ex)  
   {  
    string   str   =   "";  
    str   =   ex.Message.ToString();
    txtMailError.Text=str;
   }
   
   mypop3.Logging=true;   
   jmail.MessageClass Mail=new MessageClass(); 
   Mail=(MessageClass)mypop3.Messages[EmailNum];  
   txtMailFrom.Text= Mail.From;//发件人
   txtMailTitle.Text=Mail.Subject;//邮件主题
   txtMailCharset.Text=Mail.Charset;
   txtMailAttack.Text=Mail.Attachments.Count.ToString();
   txtMailTime.Text=Mail.Date.ToString();
   txtMailContent.Text=Mail.Text;//邮件内容
   txtMailSize.Text=Mail.Size.ToString();
   txEncoding.Text=Mail.Encoding.ToString();
   txtMailMime.Text=Mail.MimeVersion;
   txtMailFormat.Text=Mail.ContentType;
   int total=mypop3.Messages.Count;
   
   if (comboBox1.Items.Count==0)
   {
    txtTotal.Text=(mypop3.Messages.Count-1).ToString();
    for(int i=1;i<total;i++)
    {
     comboBox1.Items.Add(i);   
    }
   }
  
  
  }

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

  
 }
}