主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:257次


*
w3 JMail v4.4 Professional 的获取请自行 google!
或参阅 http://community.csdn.net/Expert/TopicView.asp?id=3739405
w3 JMail v4.4 Professional 是一个 COM 的组件,我用 C# 把其 POP3 COM 类封装成
一个用于收取邮件的 .Net 组件:
实现了
 //同步事件
  public event MessagesEventHandler MessageReceived; //一封邮件已收到本地
  public event MessagesEventHandler MessageReceive; //一封邮件正到达
  //可用于收取邮件的每个附件处理
  public event AttachmentsEventHandler AttachmentReceive; //一封邮件的附件正到达

 //发生异常事件
  public event MessagesEventHandler MessageException;
  public event AttachmentsEventHandler AttachmentException;

因此在调用该组件的主调程序中可根据分析邮件的主题或附件等结果作不同处理!

将如下所有代码:
1.复制到 Microsoft Visual Studio .Net 2003 的新建的 "控制台应用程序" 项目的 *.cs 文件中
2.然后添加引用 JMail 4.x Library!
jmail 的注册方法,运行命令行: regsvr32 e:\jmail\jmail.dll
3.F5 运行

或者
将如下所有代码:
1.复制到任意一个 *.cs 文件中保存!(如: e:\temp\NJMail.cs)
2.使用 tlbimp.exe(类型库导入程序)实用工具生成一个 .Net 程序集
执行命令行: (位于 Ms VS.Net 安装目录下的: E:\MsVS.Net\SDK\v1.1\Bin\TlbImp.exe)
tlbimp.exe e:\jmail\jmail.dll /out:e:\temp\jmail.net.dll /namespace:jmail
生成的 jmail.net.dll 与 *.cs 文件放在同一目录下!
3.执行 csc 命令行编译 *.cs
编译成 exe : (这里是为了演示测试效果所以编译成 exe)
csc.exe NJMail.cs /r:jmail.net.dll
编译成 dll ,即可由别的 .Net 程序添加引用:
csc.exe /t:library NJMail.cs /r:jmail.net.dll

(当然也可将 namespace Microshaoft.NJMail 下的代码单独编译成 dll)
*/

namespace Microshaoft.NJMail
{
 //using jmail;
 //using System;

 public class POP3
 {
  public delegate void MessagesEventHandler(MessagesState oMessagesState);
  public delegate void AttachmentsEventHandler(AttachmentsState oAttachmentsState);

  //异步事件
  public event MessagesEventHandler MessagesReceiveAsync;
  public event AttachmentsEventHandler AttachmentsReceiveAsync;

  //同步事件
  public event MessagesEventHandler MessageReceived;
  public event MessagesEventHandler MessageReceive;
  public event AttachmentsEventHandler AttachmentReceive;

  //发生异常事件
  public event MessagesEventHandler MessageException;
  public event AttachmentsEventHandler AttachmentException;

  private string _UserName;
  private string _Password;
  private string _Server;
  private int _Port = 110;

  private static object _LockObject = new object();

  public POP3(string UserName,string Password,string Server,int Port)
  {
   this._UserName = UserName;
   this._Password = Password;
   this._Server = Server;
   this._Port = Port;
  }

  public POP3(string UserName,string Password,string Server)
  {
   this._UserName = UserName;
   this._Password = Password;
   this._Server = Server;
  }

  //[MTAThread]
  public void Execute()
  {
   this.Execute(false);
  }

  public void Execute(bool IsAsync)
  {
   jmail.POP3Class pop3 = new jmail.POP3Class();
   try
   {
    pop3.Timeout = 0;
    pop3.Connect(this._UserName,this._Password,this._Server,this._Port);
    jmail.MessagesClass jms = (jmail.MessagesClass) pop3.Messages;
    int I = jms.Count;
    MessagesState omss = null;
    for (int i = 0; i < I - 1; i++)
    {
     try
     {
      jmail.MessageClass jmc = (jmail.MessageClass) jms[i+1];
      if (omss == null)
      {
       omss = new MessagesState(i+1,jmc,jms,pop3);
       if (this.MessageReceive != null)
       {
        this.MessageReceive(omss);
       }
      }
      if (!omss.CancelCurrent)
      {
       if (IsAsync)
       {
        //System.IAsyncResult iar =
        (new MessagesEventHandler(this.MessageExecuteAsync)).BeginInvoke(omss,new System.AsyncCallback(this.OnMessageCallBack),omss);
       }
       else
       {
        int J = jmc.Attachments.Count;
        AttachmentsState oass = null;
        for (int j = 0; j < J; j++)
        {
         try
         {
          if (oass == null)
          {
           oass = new AttachmentsState(j,omss);
           if (this.AttachmentReceive != null)
           {
            this.AttachmentReceive(oass);
           }
          }
          if (!oass.CancelCurrent)
          {
           string s = oass.FileName;
           omss.SetFilePath(System.IO.Path.GetDirectoryName(s) + @"\");
           oass.SaveToFile(s);
          }
         }
         catch (System.Exception e)
         {
          if (this.AttachmentException != null)
          {
           oass.Exception = e;
           this.AttachmentException(oass);
           if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.CancelAll)
           {
            break;
           }
           else
           if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Retry)
           {
            j--;
           }
           else
           if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Ignore)
           {
            //continue;
           }
           else
           if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Throw)
           {
            throw e;
           }
          }
          else
          {
           throw e;
          }
         }
        }
        if (this.MessageReceived != null)
        {
         this.MessageReceived(omss);
        }
       }
      }
     }
     catch (System.Exception e)
     {
      if (this.MessageException != null)
      {
       omss.Exception = e;
       this.MessageException(omss);
       if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.CancelAll)
       {
        break;
       }
       else
       if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Retry)
       {
        i--;
       }
       else
       if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Ignore)
       {
        //continue;
       }
       else
       if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Throw)
       {
        throw e;
       }
      }
      else
      {
       throw e;
      }
     }
    }
   }
   catch (System.Exception e)
   {
    throw e;
   }
   finally
   {
    pop3.Disconnect();
    pop3 = null;
   }
   //System.Console.WriteLine("main end");
  }

  //[MTAThread]
  private void MessageExecuteAsync(MessagesState oMessagesState)
  {
   int J = oMessagesState.jMessage.Attachments.Count;
   for (int j = 0; j < J; j++)
   {
    AttachmentsState oass = new AttachmentsState(j,oMessagesState);
    //System.IAsyncResult iar =
    (new AttachmentsEventHandler(this.AttachmentExecuteAsync)).BeginInvoke(oass,new System.AsyncCallback(this.OnAttachemnetCallBack),oass);
   }
  }

  //[MTAThread]
  private void AttachmentExecuteAsync(AttachmentsState oAttachmentsState)
  {
   //
  }

  //[MTAThread]
  private void OnMessageCallBack(System.IAsyncResult iar)
  {
   MessagesState omss = (MessagesState) iar.AsyncState;
   if (this.MessagesReceiveAsync != null)
   {
    if (omss.jMessage.Attachments.Count == 0)
    {
     this.MessagesReceiveAsync(omss);
    }
   }
  }

  //[MTAThread]
  private void OnAttachemnetCallBack(System.IAsyncResult iar)
  {
   AttachmentsState oass = (AttachmentsState) iar.AsyncState;
   if (this.AttachmentsReceiveAsync != null)
   {
    this.AttachmentsReceiveAsync(oass);
   }
   if (!oass.CancelCurrent)
   {
    try
    {
     oass.SaveToFile(oass.FileName);
    }
    catch (System.Exception e)
    {
     oass.Exception = e;
     if (AttachmentException != null)
     {
      AttachmentException(oass);
      if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.CancelAll)
      {

      }
      else
      if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Retry)
      {
       this.OnAttachemnetCallBack((System.IAsyncResult) oass);
      }
      else
      if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Ignore)
      {

      }
      else
      if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Throw)
      {
       throw e;
      }
     }
    }
   }
   if (this.MessagesReceiveAsync != null)
   {
    if (oass.AttachmentsCount == 0)
    {
     this.MessagesReceiveAsync(oass.MessagesState);
    }
   }
  }
 }

 public class MessagesState //Messages 状态
 {
  private static object _LockObject = new object();
  private int _MessageID;
  private jmail.MessageClass _jMessage;
  private jmail.MessagesClass _jMessages;
  private jmail.POP3Class _jPOP3;
  private string _FilePath;
  private bool _CancelCurrent;

  private System.Exception _Exception;
  private ExceptionActions _ExceptionAction;

 

  public ExceptionActions ExceptionAction
  {
   get
   {
    return _ExceptionAction;
   }
   set
   {
    this._ExceptionAction = value;
   }
  }

  public System.Exception Exception
  {
   get
   {
    return _Exception;
   }
   set
   {
    this._Exception = value;
   }
  }

  public string FilePath
  {
   get
   {
    return this._FilePath;
   }
  }

  internal void SetFilePath(string FilePath)
  {
   this._FilePath = FilePath;
  }

  public bool CancelCurrent
  {
   get
   {
    return this._CancelCurrent;
   }
   set
   {
    this._CancelCurrent = value;
   }
  }

  public int MessagesCount //尚未处理的邮件数
  {
   get
   {
    //lock(MessagesState._LockObject)
    {
     return this._jMessages.Count - this._MessageID - 1;
    }
   }
  }

  public jmail.MessagesClass jMessages
  {
   get
   {
    return this._jMessages;
   }
  }

  public jmail.MessageClass jMessage
  {
   get
   {
    return this._jMessage;
   }
  }

  public int MessageID
  {
   get
   {
    return this._MessageID;
   }
  }

  internal MessagesState(int MessageID,jmail.MessageClass jMessage,jmail.MessagesClass jMessages,jmail.POP3Class jPOP3)
  {
   this._MessageID = MessageID;
   this._jMessage = jMessage;
   this._jMessages = jMessages;
   this._jPOP3 = jPOP3;
  }

  public void DeleteSingleMessage()
  {
   lock(MessagesState._LockObject)
   {
    this.DeleteSingleMessage(this._MessageID);
   }
  
  }

  public void DeleteSingleMessage(int MessageID)
  {
   lock(MessagesState._LockObject)
   {
    this._jPOP3.DeleteSingleMessage(MessageID);
   }
  }

  public void DeleteMessages()
  {
   lock(MessagesState._LockObject)
   {
    this._jPOP3.DeleteMessages();
   }
  }
 }


 public enum ExceptionActions
 {
  CancelAll,Ignore,Retry,Throw
 }

 public class AttachmentsState //Attachments 状态
 {
  private MessagesState _MessagesState;
  private int _AttachmentID;
  private string _FileName;
  private static object _LockObject = new object();
  private jmail.AttachmentClass _jAttachment;
  private bool _CancelCurrent;
  private System.Exception _Exception;
  private ExceptionActions _ExceptionAction;

  public ExceptionActions ExceptionAction
  {
   get
   {
    return _ExceptionAction;
   }
   set
   {
    this._ExceptionAction = value;
   }
  }

  public System.Exception Exception
  {
   get
   {
    return _Exception;
   }
   set
   {
    this._Exception = value;
   }
  }

  public bool CancelCurrent
  {
   get
   {
    return this._CancelCurrent;
   }
   set
   {
    this._CancelCurrent = value;
   }
  }

  public jmail.AttachmentClass jAttachment
  {
   get
   {
    return this._jAttachment;
   }
  }

  public int AttachmentsCount //尚未处理的邮件附件数
  {
   get
   {
    //lock(AttachmentsState._LockObject)
    {
     return this._MessagesState.jMessage.Attachments.Count - this._AttachmentID - 1;
    }
   }
  }

  public string FileName
  {
   get
   {
    return this._FileName;
   }
   set
   {
    this._FileName = value;
   }
  }

  public MessagesState MessagesState
  {
   get
   {
    return this._MessagesState;
   }
  }

  public int AttachmentID
  {
   get
   {
    return this._AttachmentID;
   }
  }

  public void SaveToFile(string FileName)
  {
   //if (!this.CancelCurrent)
   {
    this._jAttachment.SaveToFile(FileName);
   }
  }

  internal AttachmentsState(int AttachmentID,MessagesState oMessagesState)
  {
   this._MessagesState = oMessagesState;
   this._AttachmentID = AttachmentID;
   this._jAttachment = (jmail.AttachmentClass) oMessagesState.jMessage.Attachments[AttachmentID];
   this._FileName = System.String.Format("[{0}].{1}.[{2}].{3}",oMessagesState.MessageID,oMessagesState.jMessage.Subject,AttachmentID,this._jAttachment.Name);
  }
 }
}

//================================================================================================
//控制台测试程序:
namespace ConsoleApplicationTest
{
 using Microshaoft.NJMail;
 using POP = Microshaoft.NJMail.POP3;
 class AppTest
 {
  POP Pop1;
  bool IsBusy = false;

  static void Main()
  {

   //调用 Windows 测试程序
   System.Console.WriteLine("WindowsApplicationTest Begin:");
   WindowsApplicationTest.Form1.Main0();
   System.Console.WriteLine("WindowsApplicationTest End.");

   System.Console.WriteLine("ConsoleApplicationTest Begin:");
   AppTest a = new AppTest();
   a.Pop1 = new POP("UserName","Password","mail.xxx.com");
   //订阅异步事件
   a.Pop1.MessagesReceiveAsync += new Microshaoft.NJMail.POP3.MessagesEventHandler(a.Pop1_MessagesReceiveAsync);
   a.Pop1.AttachmentsReceiveAsync += new Microshaoft.NJMail.POP3.AttachmentsEventHandler(a.Pop1_AttachmentsReceiveAsync);
   //订阅同步事件
   a.Pop1.MessageReceived += new Microshaoft.NJMail.POP3.MessagesEventHandler(a.Pop1_MessageReceived);
   a.Pop1.AttachmentReceive += new Microshaoft.NJMail.POP3.AttachmentsEventHandler(a.Pop1_AttachmentReceive);
   a.Pop1.MessageReceive += new Microshaoft.NJMail.POP3.MessagesEventHandler(a.Pop1_MessageReceive);
   //订阅 Exception 事件
   a.Pop1.AttachmentException += new Microshaoft.NJMail.POP3.AttachmentsEventHandler(a.Pop1_AttachmentException);
   a.Run();
   System.Timers.Timer t = new System.Timers.Timer();
   t.Interval = 1000 * 30; //每 30 秒收取一次邮件
   t.Enabled = true;
   t.Elapsed += new System.Timers.ElapsedEventHandler(a.t_Elapsed);
   while (System.Console.ReadLine().ToLower() != "exit")
   {
    System.Console.WriteLine("press \"exit\" to exit this programe!");
   }
  }

  private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   this.Run();
  }

  private void Run()
  {
   if (!IsBusy)
   {
    System.Console.WriteLine("Busy");
    this.IsBusy = true;
    //this.Pop1.Execute(true); //异步执行
    this.Pop1.Execute(); //同步执行
    this.IsBusy = false;
    System.Console.WriteLine("Idle");
    System.Console.WriteLine("press \"exit\" to exit this programe!");
   }
  }

  private void Pop1_MessagesReceiveAsync(MessagesState oMessagesState)
  {
   System.Console.WriteLine("Message: [{0}].{1} of {2} Messages have been recieved! {3}",oMessagesState.MessageID,oMessagesState.jMessage.Subject,oMessagesState.jMessages.Count - 1,oMessagesState.MessagesCount);
   if (oMessagesState.MessagesCount == 0)
   {
    System.Console.WriteLine("all messages have been recieved!");
   }
  }

  private void Pop1_AttachmentsReceiveAsync(AttachmentsState oAttachmentsState)
  {
  
   oAttachmentsState.SaveToFile(@"E:\jmailAttachments\[" + System.Guid.NewGuid().ToString() + "]." +  oAttachmentsState.FileName);
   oAttachmentsState.FileName = @"E:\jmailAttachments\[" + System.Guid.NewGuid().ToString() + "]." +  oAttachmentsState.FileName;

   if (oAttachmentsState.AttachmentsCount == 0)
   {
    System.Console.WriteLine("[{0}].{1} have been recieved!",oAttachmentsState.MessagesState.MessageID,oAttachmentsState.MessagesState.jMessage.Subject);
    //异步执行删除有错误
    //oAttachmentsState.MessagesState.DeleteSingleMessage(oAttachmentsState.MessagesState.MessageID);
   }
   //oAttachmentsState.CancelCurrent = true;
  }

  private void Pop1_MessageReceived(MessagesState oMessagesState)
  {
   System.Console.WriteLine("Message: [{0}].{1} of {2} Messages have been recieved! {3}",oMessagesState.MessageID,oMessagesState.jMessage.Subject,oMessagesState.jMessages.Count - 1,oMessagesState.MessagesCount);
   //可以每收完一封邮件即删除
   //oMessagesState.DeleteSingleMessage(oMessagesState.MessageID);
  }

  private void Pop1_AttachmentReceive(AttachmentsState oAttachmentsState)
  {
   //oAttachmentsState.CancelCurrent = true;
   oAttachmentsState.SaveToFile(@"E:\jmailAttachments\[" + System.Guid.NewGuid().ToString() + "]." +  oAttachmentsState.FileName);
   oAttachmentsState.FileName = @"E:\jmailAttachments\[" + System.Guid.NewGuid().ToString() + "]." +  oAttachmentsState.FileName;
   //oAttachmentsState.CancelCurrent = true;
  }

  private void Pop1_MessageReceive(MessagesState oMessagesState)
  {
   //oMessagesState.CancelCurrent = false;
  }

  private void Pop1_AttachmentException(Microshaoft.NJMail.AttachmentsState oAttachmentsState)
  {
   System.Console.WriteLine("Execute Exception: {0}",oAttachmentsState.Exception.Message);
   oAttachmentsState.FileName ="e:\\temp\\copy of " + oAttachmentsState.jAttachment.Name;
   oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Retry; //上一句文件改名后重新处理
   oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Ignore; //处理下一个附件
   //oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Throw;
  }
 }
}

//================================================================================================
// Windows 测试程序:
namespace WindowsApplicationTest
{
 using System;
 using System.Windows.Forms;
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Timer timer1;
  private System.ComponentModel.IContainer components;

  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.components = new System.ComponentModel.Container();
   this.button1 = new System.Windows.Forms.Button();
   this.timer1 = new System.Windows.Forms.Timer(this.components);
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(24, 24);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(80, 40);
   this.button1.TabIndex = 0;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // timer1
   //
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  public static void Main0() //Main() //若要单独的 Windows 程序可再换回 Main()
  {
   Application.Run(new Form1());
  }
  private Microshaoft.NJMail.POP3 x;
  private void button1_Click(object sender, System.EventArgs e)
  {
   button1.Enabled = false;
   x = new Microshaoft.NJMail.POP3("UserName","Password","mail.xxx.com");
   x.MessageReceive += new Microshaoft.NJMail.POP3.MessagesEventHandler(x_MessageReceive);
   x.MessageReceived += new Microshaoft.NJMail.POP3.MessagesEventHandler(x_MessageReceived);
   x.AttachmentReceive += new Microshaoft.NJMail.POP3.AttachmentsEventHandler(x_AttachmentReceive);
   x.AttachmentException += new Microshaoft.NJMail.POP3.AttachmentsEventHandler(x_AttachmentException);
   timer1.Interval = 1000 * 120; //每 120 秒收取一次邮件
   timer1.Enabled = false;
   x.Execute();
   button1.Enabled = true;
  }

  private void x_MessageReceive(Microshaoft.NJMail.MessagesState oMessagesState)
  {
   //System.Windows.Forms.MessageBox.Show(oMessagesState.MessageID.ToString());
   System.Console.WriteLine("{0},《{1}》",oMessagesState.MessageID.ToString(),oMessagesState.jMessage.Subject);
 
   //System.Console.WriteLine(oMessagesState.jMessage.Body);
   //System.Console.WriteLine(oMessagesState.jMessage.Text);

  }

  private void timer1_Tick(object sender, System.EventArgs e)
  {
   x.Execute();
  }

  private void x_MessageReceived(Microshaoft.NJMail.MessagesState oMessagesState)
  {
   if (oMessagesState.MessagesCount == 0)
   {
    //System.Windows.Forms.MessageBox.Show("game over");
   }
   else
   {
    System.Console.WriteLine(oMessagesState.MessageID.ToString());
   }
  }

  private void x_AttachmentReceive(Microshaoft.NJMail.AttachmentsState oAttachmentsState)
  {
   oAttachmentsState.FileName = "e:\\temp\\" + oAttachmentsState.FileName;
   oAttachmentsState.SaveToFile(oAttachmentsState.FileName);
   if (oAttachmentsState.Exception != null)
   {
    throw oAttachmentsState.Exception ;
   }

   oAttachmentsState.CancelCurrent = true; //不Save处理当前附件
   if (oAttachmentsState.AttachmentsCount == 0)
   {
    //System.Windows.Forms.MessageBox.Show(oAttachmentsState.MessagesState.jMessage.Attachments.Count.ToString());
   }
   else
   {
    System.Console.WriteLine(oAttachmentsState.AttachmentID.ToString());
   }
  }

  private void x_AttachmentException(Microshaoft.NJMail.AttachmentsState oAttachmentsState)
  {
   System.Console.WriteLine("Execute Exception: {0}",oAttachmentsState.Exception.Message);
   oAttachmentsState.FileName ="e:\\temp\\copy of " + oAttachmentsState.jAttachment.Name;
   //oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Retry; //上一句文件改名后重新处理
   oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Ignore; //处理下一个附件
   //oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Throw;
  }
 }
}


 

关键字词: