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; }
    }       

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM