ReceiveMail.cs
上传用户:hncsjykj
上传日期:2022-08-09
资源大小:461k
文件大小:4k
源码类别:

Email客户端

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenPOP.POP3;
  5. using System.Collections;
  6. using OpenPOP.MIMEParser;
  7. namespace LYBemail
  8. {
  9.     class ReceiveMail
  10.     {
  11.         private Hashtable msgs = new Hashtable();
  12.         private POPClient popClient=new POPClient();
  13.         private int count;
  14.         private string POPServer, port, login, password;
  15.         private OpenPOP.MIMEParser.Message m;
  16.         public ReceiveMail(string POPServer, string port, string login, string password)
  17.         {
  18.             this.POPServer = POPServer;
  19.             this.port = port;
  20.             this.login = login;
  21.             this.password = password;
  22.         }
  23.         public OpenPOP.MIMEParser.Message currentMessage
  24.         {
  25.             get { return m; }
  26.         }
  27.         public bool setMessage(int index)
  28.         {
  29.             m = (OpenPOP.MIMEParser.Message)msgs[index];
  30.             if (m == null)
  31.                 receiveMail(index);
  32.             m = (OpenPOP.MIMEParser.Message)msgs[index];
  33.                 return m.HasRealAttachment;
  34.             
  35.         }
  36.         #region 连接服务器
  37.         //连接服务器,并返回邮件总数
  38.         public int connect()
  39.         {
  40.             //连接POP3服务器
  41.             OpenPOP.POP3.Utility.Log = true;
  42.             popClient.Disconnect();
  43.             popClient.Connect(POPServer, int.Parse(port));
  44.             popClient.Authenticate(login, password);
  45.             //得到邮件总数
  46.             count = popClient.GetMessageCount();
  47.             msgs.Clear();
  48.             return count;
  49.         }
  50.         #endregion
  51.         #region 接收邮件
  52.         public void receiveMail(int beginIndex,int endIndex)
  53.         {
  54.             //收取邮件
  55.             for (int i =beginIndex ; i <=endIndex; i++)
  56.             {
  57.                 receiveMail(i);
  58.             }
  59.         }
  60.         public void receiveMail(int index)
  61.         {
  62.             if (index > count)
  63.                 index = count;
  64.             OpenPOP.MIMEParser.Message m = popClient.GetMessage(index, false);
  65.             try
  66.             {
  67.                 msgs.Add(index, m);
  68.             }
  69.             catch (ArgumentException)
  70.             {
  71.             }
  72.         }
  73.         #endregion
  74.         #region 删除邮件
  75.         public void delMail(int index)
  76.         {
  77.             if (index > count)
  78.                 index = count;
  79.             popClient.DeleteMessage(index);
  80.         }
  81.         public void delMail(int beginIndex, int endIndex)
  82.         {
  83.             for (int i = beginIndex; i <= endIndex; i--)
  84.             {
  85.                 delMail(i);
  86.             }
  87.            // MessageBox.Show("邮件删除完成");
  88.             Console.WriteLine("邮件删除完成");
  89.         }
  90.         public void delMail()
  91.         {
  92.             popClient.DeleteAllMessages();
  93.             //MessageBox.Show("邮箱已清空");
  94.             Console.WriteLine("邮件已清空");
  95.         }
  96.         #endregion
  97.         #region 附件相关
  98.         public ArrayList getAttachmentName()
  99.         {
  100.             ArrayList attachmentName = new ArrayList();
  101.             int count = m.AttachmentCount;
  102.             for (int i = 0; i < count; i++)
  103.             {
  104.                 if (m.GetAttachment(i).NotAttachment)
  105.                     continue;
  106.                 attachmentName.Add(m.GetAttachmentFileName(m.GetAttachment(i)));
  107.             }
  108.             return attachmentName;
  109.         }
  110.         public void getAttachments(string savePath)
  111.         {
  112.             m.SaveAttachments(savePath);
  113.         }
  114.         #endregion
  115.     }
  116. }