mailkit库收发邮件

mailkit库用于收发邮件.这个库可以替代C#自带的发邮件库

环境  W10 / VS2017CMMT / MailKit version="2.0.3" "net46"  / MimeKit version="2.0.3" "net46"

mailkit使用nuget下载或者到GIT  https://github.com/jstedfast/MailKit

邮件收发是与邮件服务器交互,原理参考 https://www.cnblogs.com/ysocean/p/7652934.html

帮助类参考部份mailkit源码.可简单实现发邮件与收邮件.在处理大附件,草稿保存,时未找到解决办法

发送邮件

MyMailKit mymail = new MyMailKit();
// 设置发件人
mymail.CfgSendEmail(1);
// 添加1个或多个收件人
mymail.AddTo("xxx@qq.com", "收件人XXX");
mymail.AddTo("yyy@163.com");
// 添加1个或多个附件
MemoryStream ms = new MemoryStream();
using (FileStream fs = new FileStream("123.doc", FileMode.Open))
{
    fs.CopyTo(ms);
}
mymail.AddAttachment(ms, "123.doc", ms.Length);
// 发送邮件
mymail.SendEmail();

接收邮件

// 查看一个文件夹的邮件
MyMailKit mymail = new MyMailKit();
// 收邮件配置
mymail.CfgIMAP(1);
// 获取文件夹的所有邮件(3个月内的)
var list = mymail.GetEmailByFolder(folder);
if (list == null)
{
   // 没找到邮件出错信息提示
   mymail.ErrMsg;
   return;
}
foreach (var item in list)
{
   // 收件人
   StringBuilder tolist = new StringBuilder();
   foreach (var to in item.ToList)
   { 
      tolist.Append($"{to.Name}<{to.Address}>,");
   }
   //
   item.UniqueId; // 邮件标识
   item.IsRead;// 是否已读
   item.Count;// 附件数
   item.IsAnswered;// 是否回复
   item.Name;// 发件人
   item.Address;// 发件人邮箱
   item.Subject;// 标题
   item.Date.ToString("yyyy-MM-dd");// 日期
}
// 显示邮件详细
var emdetail = mymail.GetEmailByUniqueId(uint.Parse(id), folder);
// 设置删除邮件
List uids = new List();
uids.Add(id);
mymail.SetFlag(uids, 8, folder);
  1  public class MyMailKit
  2     {
  3         #region 属性 账户配置信息等
  4         /// <summary>
  5         ///  发件人邮箱地址
  6         /// </summary>
  7         private string fromEmail = null;
  8         /// <summary>
  9         ///  发件人别名
 10         /// </summary>
 11         private string fromAlias = null;
 12         /// <summary>
 13         /// 发件人邮箱密码(或授权码)
 14         /// </summary>
 15         private string fromPwd = null;
 16         /// <summary>
 17         /// SMTP服务器地址
 18         /// </summary>
 19         private string serverSMTP = null;
 20         private int portSMTP = 0;
 21         /// <summary>
 22         /// IMAP服务器地址
 23         /// </summary>
 24         private string serverIMAP = null;
 25         private int portIMAP = 0;
 26         /// <summary>
 27         /// POP服务器地址
 28         /// </summary>
 29         private string serverPOP = null;
 30         private int portPOP = 0;
 31         /// <summary>
 32         /// 邮件账户(收邮件时登录账户)
 33         /// </summary>
 34         private string account = null;
 35         /// <summary>
 36         /// 邮件账户密码(收邮件时登录密码)
 37         /// </summary>
 38         private string pwd = null;
 39         #endregion
 40 
 41         #region 属性 邮件主体内容 内容块容器
 42 
 43         /// <summary>
 44         /// 邮件对象
 45         /// </summary>
 46         private MimeMessage message = null;
 47         /// <summary>
 48         /// 邮件内容块的容器 放置邮件正文,附件等内容块
 49         /// </summary>
 50         private Multipart mimeparts = null;
 51         /// <summary>
 52         /// 收件人列表
 53         /// </summary>
 54         private List<MailboxAddress> toList = null;
 55         /// <summary>
 56         /// 附件列表
 57         /// </summary>
 58         private List<MimePart> attaList = null;
 59         #endregion
 60 
 61         /// <summary>
 62         /// 操作异常信息
 63         /// </summary>
 64         public string ErrMsg { get; private set; }
 65 
 66         #region 制作与发送邮件
 67 
 68         /// <summary>
 69         /// 添加一个收件人
 70         /// 在制作邮件方法之前调用
 71         /// </summary>
 72         /// <param name="address">收件人地址</param>
 73         /// <param name="name"></param>
 74         public void AddTo(string address, string name = null)
 75         {
 76             if (this.toList == null)
 77                 this.toList = new List<MailboxAddress>();
 78             if (string.IsNullOrWhiteSpace(name))
 79                 name = address.Substring(0, address.IndexOf('@'));
 80             this.toList.Add(new MailboxAddress(name, address));
 81         }
 82 
 83         /// <summary>
 84         /// 添加一个附件
 85         /// 在制作邮件方法之前调用
 86         /// </summary>
 87         /// <param name="atta">附件流</param>
 88         /// <param name="name">附件名字</param>
 89         /// <param name="size">附件大小(K)</param>
 90         public void AddAttachment(Stream atta, string name, long size = 0)
 91         {
 92             try
 93             {
 94                 if (this.attaList == null)
 95                     this.attaList = new List<MimePart>();
 96                 // 附件内容块
 97                 MimePart attapart = new MimePart();
 98                 attapart.Content = new MimeContent(atta);
 99 
100                 // 内容描述为附件
101                 attapart.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
102 
103                 // 附件名字设置,如果名字有中文也没关系
104                 attapart.ContentDisposition.FileName = name;
105                 // 大小设置
106                 if (size > 0)
107                     attapart.ContentDisposition.Size = size;
108 
109                 // 采用base64编码传输
110                 attapart.ContentTransferEncoding = ContentEncoding.Base64;
111 
112                 //
113                 this.attaList.Add(attapart);
114             }
115             catch (Exception e)
116             {
117                 ErrMsg = $"添加附件异常:{e.ToString()} [{e.Message}]";
118             }
119         }
120 
121         /// <summary>
122         /// 制作一封邮件
123         /// 调用此方法之前,先调用邮件配置初始化方法和添加收件人,添加附件方法
124         /// </summary>
125         /// <param name="subject">邮件主题(标题)</param>
126         /// <param name="body">邮件正文(内容)</param>
127         /// <param name="ishtml">正文是否为HTML格式,纯文本格式=false</param>
128         public void MakeEmail(string subject, string body, bool ishtml = true)
129         {
130             try
131             {
132                 // 邮件类新实例
133                 message = new MimeMessage();
134 
135                 // 设置邮件主题
136                 message.Subject = subject;
137 
138                 // 设置发件人信息
139                 message.From.Add(new MailboxAddress(fromAlias, fromEmail));
140 
141                 // 设置收件人信息
142                 message.To.AddRange(this.toList);
143 
144                 // 设置邮件正文
145                 var content = new TextPart(ishtml ? "html" : "plain");
146                 content.SetText(Encoding.UTF8, body);
147 
148                 // 建立内容块容器,将内容或附件等添加到其中 MimeEntity是各种类型内容的基类
149                 mimeparts = new Multipart("mixed");
150                 mimeparts.Add(content);
151                 // 附件
152                 if (this.attaList != null)
153                 {
154                     foreach (var atta in this.attaList)
155                     {
156                         mimeparts.Add(atta);
157                     }
158                 }
159 
160                 // 将内容块容器设置到邮件的内容.到此已经填好邮件实体的主要属性
161                 message.Body = mimeparts;
162             }
163             catch (Exception e)
164             {
165                 ErrMsg = $"制作邮件异常:{e.ToString()} [{e.Message}]";
166             }
167         }
168 
169         /// <summary>
170         /// 设置此邮件是对指定邮件的回复(这是一封回复邮件)
171         /// 在调用制作邮件方法之后调用,在发送前调用.需要调用收件配置方法CfgIMAP()
172         /// </summary>
173         /// <param name="uniqueid">被回复邮件唯一标识</param>
174         /// <param name="folderName">被回复邮件文件夹</param>
175         public void SetReplyTo(uint uniqueid, string folderName = null)
176         {
177             try
178             {
179                 // 被回复的邮件
180                 MimeMessage remail;
181                 // 查找这个被回复的邮件,设置回复状态
182                 using (var client = ConnectIMAP())
183                 {
184                     if (folderName == null)
185                         folderName = client.Inbox.Name;
186                     var emailUniqueId = new UniqueId(uniqueid);
187                     var folder = client.GetFolder(folderName);
188                     folder.Open(FolderAccess.ReadWrite);
189 
190                     remail = folder.GetMessage(emailUniqueId);
191                     folder.AddFlags(emailUniqueId, MessageFlags.Answered, true);
192                     folder.Close();
193                     client.Disconnect(true);
194                 }
195                 // construct the In-Reply-To and References headers
196                 if (!string.IsNullOrEmpty(remail.MessageId))
197                 {
198                     // 设置此邮件是对这个MESSAGEID的邮件的回复
199                     message.InReplyTo = remail.MessageId;
200                     // 此邮件的"对其它消息"的引用属性设为这个邮件的引用属性
201                     foreach (var id in remail.References)
202                         message.References.Add(id);
203                     message.References.Add(remail.MessageId);
204                 }
205                 // 回复邮件主题前面加RE:
206                 if (!message.Subject.StartsWith("RE:", StringComparison.OrdinalIgnoreCase))
207                     message.Subject = "RE:" + message.Subject;
208             }
209             catch (Exception e)
210             {
211                 ErrMsg = $"设置为回复邮件异常:{e.ToString()} [{e.Message}]";
212             }
213         }
214 
215         /// <summary>
216         /// 设置
217         /// </summary>
218         /// <param name="uniqueid"></param>
219         /// <param name="folderName"></param>
220         //public void SetForWard(uint uniqueid, string folderName = null)
221         //{
222 
223         //}
224 
225         /// <summary>
226         /// 发送一个邮件
227         /// 调用此方法之前,请先调用建立邮件的方法MakeMessage()
228         /// </summary>
229         public bool SendEmail()
230         {
231             try
232             {
233                 // 建立发件服务客户端
234                 using (var client = new SmtpClient())
235                 {
236                     // SMTP服务器
237                     client.Connect(serverSMTP, portSMTP);
238 
239                     // 登录
240                     client.Authenticate(fromEmail, fromPwd);
241 
242                     // 发邮件
243                     client.Send(message);
244 
245                     // 关闭连接
246                     client.Disconnect(true);
247                     return true;
248                 }
249             }
250             catch (Exception e)
251             {
252                 ErrMsg = $"发送邮件异常:{e.ToString()} [{e.Message}]";
253                 return false;
254             }
255         }
256 
257         #endregion
258 
259         #region 接收与处理邮件
260 
261         /// <summary>
262         /// 连接到IMAP服务器        
263         /// </summary>
264         private ImapClient ConnectIMAP()
265         {
266             try
267             {
268                 ImapClient client = new ImapClient();
269                 client.Connect(serverIMAP, portIMAP);
270                 client.Authenticate(account, pwd);
271 
272                 /**********************************************************************/
273                 // 网易126 163相关邮箱时,要用这两句话,表明客户端身份.在连接后调用.否则无法登录邮箱.
274                 var clientImplementation = new ImapImplementation
275                 {
276                     Name = "MeSince",
277                     Version = "2.0"
278                 };
279                 var serverImplementation = client.Identify(clientImplementation);
280                 /*********************************************************************/
281 
282                 return client;
283             }
284             catch (Exception e)
285             {
286                 ErrMsg = $"连接到IMAP服务器异常:{e.ToString()} [{e.Message}]";
287                 return null;
288             }
289         }
290 
291         /// <summary>
292         /// 获取邮箱的所有文件夹列表
293         /// 调用前先调用配置方法CfgIMAP()
294         /// </summary>
295         public EmailViewM GetFolders()
296         {
297             try
298             {
299                 using (var client = ConnectIMAP())
300                 {
301                     List<IMailFolder> mailFolderList = client.GetFolders(client.PersonalNamespaces[0]).ToList();
302                     var entity = FillEntity(null, null, mailFolderList.ToArray());
303                     client.Disconnect(true);
304                     //
305                     return entity;
306                 }
307             }
308             catch (Exception e)
309             {
310                 ErrMsg = $"获取邮箱的所有文件夹异常:{e.ToString()} [{e.Message}]";
311                 return null;
312             }
313         }
314 
315         /// <summary>
316         /// 根据唯一标识和文件夹名,获取单个邮件
317         /// </summary>
318         /// <param name="folderName"></param>
319         /// <returns></returns>
320         public EmailViewM GetEmailByUid(uint uniqueid, string folderName = null)
321         {
322             try
323             {
324                 using (ImapClient client = ConnectIMAP())
325                 {
326                     if (folderName == null)
327                         folderName = client.Inbox.Name;
328                     IMailFolder folder = client.GetFolder(folderName);
329                     folder.Open(FolderAccess.ReadOnly);
330                     var email = folder.GetMessage(new UniqueId(uniqueid));
331                     var entity = FillEntity(null, email);
332                     //
333                     folder.Close();
334                     client.Disconnect(true);
335                     // 
336                     return entity;
337                 }
338             }
339             catch (Exception e)
340             {
341                 ErrMsg = $"获取单个邮件异常:{e.ToString()} [{e.Message}]";
342                 return null;
343             }
344         }
345 
346         /// <summary>
347         /// 获取一个文件夹的邮件 返回一个列表,包含摘要信息.收件/发件人,有几个附件,时间和标题,是否已读
348         /// 默认只获取3个月内的邮件
349         /// 调用前先调用配置方法CfgIMAP()
350         /// </summary>
351         public List<EmailViewM> GetEmailByFolder(string folderName = null)
352         {
353             try
354             {
355                 using (ImapClient client = ConnectIMAP())
356                 {
357                     IMailFolder folder;
358                     // 默认是收件箱
359                     if (folderName == null||folderName.ToLower()=="inbox")
360                     {
361                         folder=client.GetFolder(client.Inbox.Name);
362                     }
363                     else {
364                         // 其它特定的文件夹
365                         string dirK = folderName.ToLower();
366                         Dictionary<string, SpecialFolder> fdict = new Dictionary<string, SpecialFolder>();
367                         fdict.Add("archive", SpecialFolder.Archive);
368                         fdict.Add("drafts", SpecialFolder.Drafts);
369                         fdict.Add("flagged", SpecialFolder.Flagged);
370                         fdict.Add("sent", SpecialFolder.Sent);
371                         fdict.Add("junk", SpecialFolder.Junk);
372                         fdict.Add("trash", SpecialFolder.Trash);
373                         if (fdict.ContainsKey(dirK))
374                             folder = client.GetFolder(fdict[dirK]);
375                         else
376                         {
377                             // 否则是自定义的文件夹,或者是邮件服务商的特别文件夹
378                             folder = client.GetFolder(folderName);
379                         }
380                     }
381                         
382                     folder.Open(FolderAccess.ReadOnly);
383 
384                     // 获取所有邮件的唯一标识列表
385                     SearchQuery sq = SearchQuery.DeliveredAfter(DateTime.Today.AddMonths(-3));
386                     var emailUids = folder.Search(sq);
387 
388                     // 获取这些邮件的摘要信息(MessageSummaryItems.BodyStructure这个项可以知道是否带附件)
389                     var mails = folder.Fetch(emailUids, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.Full);
390                     List<EmailViewM> entityls = new List<EmailViewM>();
391                     foreach (var emhead in mails)
392                     {
393                         var entity = FillEntity(emhead, null, folder);
394                         entityls.Add(entity);
395                     }
396                     //
397                     folder.Close();
398                     client.Disconnect(true);
399                     //
400                     return entityls;
401                 }
402             }
403             catch (Exception e)
404             {
405                 ErrMsg = $"获取一个文件夹的邮件异常:{e.ToString()} [{e.Message}]";
406                 return null;
407             }
408         }
409 
410         /// <summary>
411         /// 使用唯一ID获取一封完整邮件
412         /// 调用前先调用配置方法CfgIMAP()
413         /// </summary>
414         /// <param name="folder">文件夹名,默认是收件箱</param>
415         /// <param name="uniqueid">邮件唯一编号</param>
416         public EmailViewM GetEmailByUniqueId(uint uniqueid, string folderName = null)
417         {
418             try
419             {
420                 using (ImapClient client = ConnectIMAP())
421                 {
422                     if (folderName == null)
423                         folderName = client.Inbox.Name;
424                     IMailFolder folder = client.GetFolder(folderName);
425                     folder.Open(FolderAccess.ReadWrite);
426                     UniqueId emailUniqueId = new UniqueId(uniqueid);
427 
428                     // 获取这些邮件的摘要信息
429                     List<UniqueId> uids = new List<UniqueId>();
430                     uids.Add(emailUniqueId);
431                     var emaills = folder.Fetch(uids, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope);
432                     var emhead = emaills[0];
433 
434                     // 获取邮件含正文部分,然后设置为已读.
435                     MimeMessage embody = folder.GetMessage(emailUniqueId);
436                     folder.AddFlags(emailUniqueId, MessageFlags.Seen, true);
437 
438                     /*赋值到实体类*/
439                     var entity = FillEntity(emhead, embody, folder);
440                     //
441                     folder.Close();
442                     client.Disconnect(true);
443                     //
444                     return entity;
445                 }
446             }
447             catch (Exception e)
448             {
449                 ErrMsg = $"获取单个完整邮件异常:{e.ToString()} [{e.Message}]";
450                 return null;
451             }
452         }
453 
454         /// <summary>
455         /// 邮件添加标识(已读,已回复,已删除等等).参数值参考EmailViewM实体同名属性
456         /// 调用前先调用配置方法CfgIMAP()
457         /// </summary>
458         /// <param name="uniqueIdls">同一文件夹下的邮件唯一标识列表</param>
459         /// <param name="flag">标识代码 1=已读 2=已回复 8=删除</param>
460         /// <param name="folderType">文件夹名</param>
461         public void SetFlag(List<uint> uniqueIdls, int flag, string folderType = null)
462         {
463             try
464             {
465                 using (ImapClient client = ConnectIMAP())
466                 {
467                     List<UniqueId> uniqueids = uniqueIdls.Select(o => new UniqueId(o)).ToList();
468                     MessageFlags messageFlags = (MessageFlags)flag;
469                     if (folderType == null)
470                         folderType = client.Inbox.Name;
471                     IMailFolder folder = client.GetFolder(folderType);
472                     folder.Open(FolderAccess.ReadWrite);
473                     folder.AddFlags(uniqueids, messageFlags, true);
474                     //
475                     folder.Close();
476                     client.Disconnect(true);
477                 }
478             }
479             catch (Exception e)
480             {
481                 ErrMsg = $"邮件添加标识时异常:{e.ToString()} [{e.Message}]";
482             }
483         }
484 
485         /// <summary>
486         /// 将邮件保存到草稿箱 返回邮件的唯一标识
487         /// 调用前先调用配置方法CfgIMAP(),调用制做邮件方法
488         /// </summary>
489         public int SaveDrafts(int uniqueId = -1)
490         {
491             try
492             {
493                 using (ImapClient client = ConnectIMAP())
494                 {
495                     // 打开草稿箱,添加邮件
496                     IMailFolder folder = client.GetFolder(SpecialFolder.Drafts);
497                     folder.Open(FolderAccess.ReadWrite);
498 
499                     // 如果保存的是已经有的草稿邮件,则删除它再保存新的草稿.(没找到保存已有草稿的办法)
500                     if (uniqueId > -1)
501                     {
502                         List<UniqueId> uidls = new List<UniqueId>();
503                         uidls.Add(new UniqueId((uint)uniqueId));
504                         folder.SetFlags(uidls, MessageFlags.Seen | MessageFlags.Deleted, true);
505                         folder.Expunge(uidls);
506                     }
507 
508                     UniqueId? uid = folder.Append(this.message, MessageFlags.Seen | MessageFlags.Draft);
509                     //
510                     folder.Close();
511                     client.Disconnect(true);
512                     return uid.HasValue ? (int)uid.Value.Id : -1;
513                 }
514             }
515             catch (Exception e)
516             {
517                 ErrMsg = $"邮件保存草稿时异常:{e.ToString()} [{e.Message}]";
518                 return -1;
519             }
520         }
521         #endregion
522 
523         /// <summary>
524         /// 将邮件相关信息填充到实体对象
525         /// </summary>
526         /// <param name="emhead">邮件基本信息</param>
527         /// <param name="embody">邮件详细信息</param>
528         /// <param name="folders">邮箱文件夹</param>
529         /// <returns></returns>
530         private EmailViewM FillEntity(IMessageSummary emhead = null, MimeMessage embody = null, params IMailFolder[] folders)
531         {
532             try
533             {
534                 // 邮件基本信息 主题(标题),发件人名,地址,日期,状态等
535                 EmailViewM entity = new EmailViewM();
536                 if (emhead != null)
537                 {
538                     entity.UniqueId = emhead.UniqueId.Id;
539                     if (emhead.Envelope.From.Count > 0)
540                     {
541                         entity.Name = emhead.Envelope.From.Mailboxes.ElementAt(0).Name;
542                         entity.Address = emhead.Envelope.From.Mailboxes.ElementAt(0).Address;
543                     }
544                     entity.Date = emhead.Envelope.Date.Value.DateTime;
545                     entity.Subject = emhead.Envelope.Subject;
546                     if (folders.Length > 0)
547                     {
548                         entity.FolderType = folders[0].Name;
549                     }
550                     // 收件人可能有多个
551                     entity.ToList = new List<EmailViewM>();
552                     foreach (var to in emhead.Envelope.To.Mailboxes)
553                     {
554                         entity.ToList.Add(new EmailViewM { Name = to.Name, Address = to.Address });
555                     }
556                     // 邮件状态,已读未读等等
557                     if (emhead.Flags.HasValue)
558                     {
559                         entity.IsRead = emhead.Flags.Value.HasFlag(MessageFlags.Seen);
560                         entity.IsAnswered = emhead.Flags.Value.HasFlag(MessageFlags.Answered);
561                     }
562                     // 附件个数(只传emhead时)
563                     entity.Count = emhead.Attachments.Count();
564                 }
565 
566                 // 正文 附件
567                 if (embody != null)
568                 {
569                     // 正文
570                     entity.BodyText = embody.TextBody;
571                     entity.BodyHTML = embody.HtmlBody;
572 
573                     // 附件
574                     // 附件个数(传embody时,包含有附件完整信息)
575                     entity.Count = embody.Attachments.Count();
576                     // 附件信息
577                     if (entity.Count > 0)
578                     {
579                         entity.AttaList = new List<EmailViewM>();
580                         // 这里要转成mimepart类型
581                         foreach (MimePart attachment in embody.Attachments)
582                         {
583                             var atta = new EmailViewM();
584                             atta.Name = attachment.ContentDisposition.FileName;
585                             atta.AttaStream = new MemoryStream();
586                             attachment.Content.DecodeTo(atta.AttaStream);
587                             atta.Size = Math.Round((double)atta.AttaStream.Length / 1024, 1).ToString();
588                             entity.AttaList.Add(atta);
589                         }
590                     }
591                 }
592                 // 邮箱文件夹
593                 if (folders.Length > 0)
594                 {
595                     entity.FolderList = new List<EmailViewM>();
596                     foreach (var item in folders)
597                     {
598                         entity.FolderList.Add(new EmailViewM()
599                         {
600                             Name = item.Name,
601                             FolderType = item.Attributes.ToString(),
602                             Count = item.Count
603                         });
604                     }
605                 }
606                 return entity;
607             }
608             catch (Exception e)
609             {
610                 ErrMsg = $"邮件填充到实体时异常:{e.ToString()} [{e.Message}]";
611                 return null;
612             }
613         }
614 
615         #region 配置账号密码方法
616 
617         /// <summary>
618         /// 初始化一个发件人的配置,发件箱,发件箱密码,SMTP服务器
619         /// </summary>
620         /// <param name="emailCode"></param>
621         public void CfgSendEmail(int emailCode)
622         {
623             switch (emailCode)
624             {
625                 default:
626                     fromAlias = "发件人名称";
627                     fromEmail = "发件人地址";
628                     fromPwd = "授权码或密码";
629                     serverSMTP = "smtp服务器地址";
630                     portSMTP = 25;
631                     break;
632             }
633         }
634 
635         /// <summary>
636         /// 初始化一个接收邮件的配置 登录名和密码,IMAP服务器,
637         /// </summary>
638         /// <param name="accountCode"></param>
639         public void CfgIMAP(int accountCode)
640         {
641             switch (accountCode)
642             {
643                    default:
644                     account = "邮件地址";
645                     pwd = "邮件密码或者授权码";//"";
646                     serverIMAP = "IMAP服务器地址";
647                     portIMAP = 143;
648                     //serverPOP = "POP3服务器地址";
649                     //portPOP = 110;
650                     break;
651             }
652         }
653 
654         #endregion
655     }        
public class EmailViewM
    {
        /// <summary>
        /// 1.从服务器上获取的邮件的UniqueId
        /// </summary>
        public uint UniqueId { get; set; }
        /// <summary>
        /// 1.发件人名字,这个名字可能为null.因为发件人可以不设名字
        /// 2.收件人名(只在ToList里的对象有值)
        /// 3.附件名(只在AttaList里的对象有值)
        /// 4.文件夹名字(只在FolderList里的对象有值)
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 1.发件人地址
        /// 2.收件人地址(只在ToList里的对象有值)
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 发件人邮箱授权码
        /// </summary>
        public string AuthCode { get; set; }
        /// <summary>
        /// 收件人列表
        /// </summary>
        public List<EmailViewM> ToList { get; set; }
        /// <summary>
        /// 邮件主题(标题)
        /// </summary>
        public string Subject { get; set; }
        /// <summary>
        /// 邮件时间
        /// </summary>
        public DateTime Date { get; set; }
        /// <summary>
        /// 1.附件个数
        /// 2.文件夹内邮件个数(只在FolderList里的对象有值)
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 附件标识ID在保存附件在本地时设置(只在AttaList里的对象有值)
        /// 当附件从邮件服务器下载到本地后,需要向客户端提供下载时,用这个ID找到该附件.
        /// </summary>
        public string AttaGuid { get; set; }
        /// <summary>
        /// 附件大小(只在AttaList里的对象有值)
        /// </summary>
        public string Size { get; set; }
        /// <summary>
        /// 附件流(只在AttaList里的对象有值)
        /// </summary>
        public Stream AttaStream { get; set; }
        /// <summary>
        /// 附件列表
        /// </summary>
        public List<EmailViewM> AttaList { get; set; }
        /// <summary>
        /// 是否已经读
        /// </summary>
        public bool IsRead { get; set; }
        /// <summary>
        /// 是否已经回复
        /// </summary>
        public bool IsAnswered { get; set; }
        /// <summary>
        /// 邮件正文的纯文本形式
        /// </summary>
        public string BodyText { get; set; }
        /// <summary>
        /// 邮件正文的HTML形式.
        /// </summary>
        public string BodyHTML { get; set; }

        /// <summary>
        /// 邮箱的文件夹列表
        /// </summary>
        public List<EmailViewM> FolderList { get; set; }
        /// <summary>
        /// 文件夹类型名
        /// 1.表示当前邮件所处文件夹名字
        /// 2.在FolderList里的对象,表示文件夹名字
        ///inbox(收件箱),
        ///archive(档案箱),
        ///drafts(草稿箱),
        ///flagged(标记的),
        ///junk(垃圾箱),
        ///sent(发件箱),
        ///trash(回收箱)
        /// </summary>
        public string FolderType { get; set; }
        /// <summary>
        /// 邮件标识,需要修改邮件标识时,传入此值
        /// 1=Seen(设为已读),
        /// 2=Answered(设为已经回复),
        /// 8=Deleted(设为删除),
        /// </summary>
        public int Flag { get; set; }
    }       

 

posted @ 2018-06-03 22:54  mirrorspace  阅读(3382)  评论(1编辑  收藏  举报