)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Imap (Ia.Cl.Models)

Public general use code classes and xml files that we've compiled and used over the years:

IMAP Server Support Class

    1: using System;
    2: using System.Collections.Generic;
    3: using MimeKit;
    4: using MailKit;
    5: using MailKit.Search;
    6: using MailKit.Security;
    7: using MailKit.Net.Imap;
    8: using System.Diagnostics;
    9: using System.Linq;
   10: using System.Net.WebSockets;
   11:  
   12: namespace Ia.Cl.Models
   13: {
   14:     ////////////////////////////////////////////////////////////////////////////
   15:  
   16:     /// <summary publish="true">
   17:     /// IMAP Server Support Class
   18:     /// </summary>
   19:     /// 
   20:     /// <remarks> 
   21:     /// Copyright © 2001-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   22:     ///
   23:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   24:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   25:     ///
   26:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   27:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   28:     /// 
   29:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   30:     /// 
   31:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   32:     /// </remarks> 
   33:     public class Imap
   34:     {
   35:         // https://github.com/jstedfast/MailKit/blob/master/Documentation/Examples/ImapExamples.cs
   36:  
   37:         private int port;
   38:         private bool imapServerEnableSsl;
   39:         private string imapServerHost, imapServerUserName, imapServerUser, imapServerPassword;
   40:         private SecureSocketOptions secureSocketOptions = new SecureSocketOptions();
   41:  
   42:         ////////////////////////////////////////////////////////////////////////////
   43:  
   44:         /// <summary>
   45:         ///
   46:         /// </summary>
   47:         public class Message
   48:         {
   49:             /// <summary/>
   50:             public Message(/*IMailFolder mailFolder,*/ MimeMessage mimeMessage, UniqueId uniqueId, int index)
   51:             {
   52:                 //this.MailFolder = mailFolder;
   53:                 this.MimeMessage = mimeMessage;
   54:                 this.Index = index;
   55:                 this.UniqueId = uniqueId;
   56:             }
   57:  
   58:             /// <summary/>
   59:             //public IMailFolder MailFolder { get; set; }
   60:  
   61:             /// <summary/>
   62:             public MimeMessage MimeMessage { get; set; }
   63:  
   64:             /// <summary/>
   65:             public UniqueId UniqueId { get; set; }
   66:  
   67:             /// <summary/>
   68:             public int Index { get; set; }
   69:         }
   70:  
   71:         ////////////////////////////////////////////////////////////////////////////
   72:  
   73:         /// <summary>
   74:         ///
   75:         /// </summary>
   76:         public Imap(string _imapServerHost, string _imapServerUser, string _imapServerPassword, bool _imapServerEnableSsl)
   77:         {
   78:             imapServerEnableSsl = _imapServerEnableSsl;
   79:             imapServerHost = _imapServerHost;
   80:             //imapServerUserName = ;
   81:             imapServerUser = _imapServerUser;
   82:             imapServerPassword = _imapServerPassword;
   83:  
   84:             Initialize();
   85:         }
   86:  
   87:         ////////////////////////////////////////////////////////////////////////////
   88:  
   89:         /// <summary>
   90:         ///
   91:         /// </summary>
   92:         public Imap()
   93:         {
   94:             /* 
   95:              * appsettings.json:
   96:   "AppSettings": {
   97:     "ImapServerEnableSsl": "false|true",
   98:     "ImapServerHost": "*********",
   99:     "ImapServerUserName": ""*********",",
  100:     "ImapServerUser": "*********",
  101:     "ImapServerPassword": "*********",
  102:   }
  103:             */
  104:  
  105:             imapServerEnableSsl = bool.TryParse(Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:ImapServerEnableSsl"), out bool b) && b;
  106:             imapServerHost = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:ImapServerHost");
  107:             //imapServerUserName = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:ImapServerUserName");
  108:             imapServerUser = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:ImapServerUser");
  109:             imapServerPassword = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:ImapServerPassword");
  110:  
  111:             Initialize();
  112:         }
  113:  
  114:         ////////////////////////////////////////////////////////////////////////////
  115:  
  116:         /// <summary>
  117:         ///
  118:         /// </summary>
  119:         public void Initialize()
  120:         {
  121:             secureSocketOptions = imapServerEnableSsl ? SecureSocketOptions.Auto : SecureSocketOptions.None;
  122:             port = 143;
  123:  
  124:             if (imapServerHost == "imap.gmail.com")
  125:             {
  126:                 port = 993;
  127:                 secureSocketOptions = SecureSocketOptions.SslOnConnect;
  128:             }
  129:         }
  130:  
  131:         ////////////////////////////////////////////////////////////////////////////
  132:  
  133:         /// <summary>
  134:         ///
  135:         /// </summary>
  136:         public List<Message> MessageList()
  137:         {
  138:             return MessageList(null, SearchQuery.All);
  139:         }
  140:  
  141:         ////////////////////////////////////////////////////////////////////////////
  142:  
  143:         /// <summary>
  144:         ///
  145:         /// </summary>
  146:         public List<Message> MessageList(string folderName)
  147:         {
  148:             return MessageList(folderName, SearchQuery.All);
  149:         }
  150:  
  151:         ////////////////////////////////////////////////////////////////////////////
  152:  
  153:         /// <summary>
  154:         ///
  155:         /// </summary>
  156:         public List<Message> MessageListBySenderEmail(string folderName, string senderEmail)
  157:         {
  158:             List<Message> list;
  159:  
  160:             if (!string.IsNullOrEmpty(folderName) && !string.IsNullOrEmpty(senderEmail))
  161:             {
  162:                 list = MessageList(folderName, SearchQuery.FromContains(senderEmail.ToLower()));
  163:             }
  164:             else list = new List<Message>();
  165:  
  166:             return list;
  167:         }
  168:  
  169:         ////////////////////////////////////////////////////////////////////////////
  170:  
  171:         /// <summary>
  172:         ///
  173:         /// </summary>
  174:         public List<Message> MessageListBySubjectSubtext(string folderName, string subjectSubtext)
  175:         {
  176:             List<Message> list;
  177:  
  178:             if (!string.IsNullOrEmpty(folderName) && !string.IsNullOrEmpty(subjectSubtext))
  179:             {
  180:                 list = MessageList(folderName, SearchQuery.SubjectContains(subjectSubtext.ToLower()));
  181:             }
  182:             else list = new List<Message>();
  183:  
  184:             return list;
  185:         }
  186:  
  187:         ////////////////////////////////////////////////////////////////////////////
  188:  
  189:         /// <summary>
  190:         ///
  191:         /// </summary>
  192:         public List<Message> MessageListByBodySubtext(string folderName, string bodySubtext)
  193:         {
  194:             List<Message> list;
  195:  
  196:             if (!string.IsNullOrEmpty(folderName) && !string.IsNullOrEmpty(bodySubtext))
  197:             {
  198:                 list = MessageList(folderName, SearchQuery.BodyContains(bodySubtext.ToLower()));
  199:             }
  200:             else list = new List<Message>();
  201:  
  202:             return list;
  203:         }
  204:  
  205:         ////////////////////////////////////////////////////////////////////////////
  206:  
  207:         /// <summary>
  208:         ///
  209:         /// </summary>
  210:         private List<Message> MessageList(string folderName, SearchQuery searchQuery)
  211:         {
  212:             int index = 0;
  213:             Message message;
  214:             IMailFolder folder = null;
  215:             List<Message> messageList;
  216:  
  217:             using (var imapClient = new ImapClient())
  218:             {
  219:                 imapClient.Connect(imapServerHost, port, secureSocketOptions);
  220:  
  221:                 Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  222:  
  223:                 imapClient.Authenticate(imapServerUser, imapServerPassword);
  224:  
  225:                 Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  226:  
  227:                 messageList = new List<Message>();
  228:  
  229:                 if (!string.IsNullOrEmpty(folderName))
  230:                 {
  231:                     if (folderName.ToLower() == "inbox")
  232:                     {
  233:                         folder = imapClient.Inbox;
  234:  
  235:                         folder.Open(FolderAccess.ReadOnly);
  236:  
  237:                         var uniqueIdList = folder.Search(searchQuery);
  238:  
  239:                         foreach (var uniqueId in uniqueIdList)
  240:                         {
  241:                             message = new Message(/*folder,*/ folder.GetMessage(uniqueId), uniqueId, index++);
  242:  
  243:                             messageList.Add(message);
  244:                         }
  245:                     }
  246:                     else
  247:                     {
  248:                         var folderList = FolderList(imapClient);
  249:  
  250:                         if (folderList.Count > 0)
  251:                         {
  252:                             folder = folderList.Where(u => string.Equals(u.Name, folderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  253:  
  254:                             folder.Open(FolderAccess.ReadOnly);
  255:  
  256:                             var uniqueIdList = folder.Search(searchQuery);
  257:  
  258:                             foreach (var uniqueId in uniqueIdList)
  259:                             {
  260:                                 message = new Message(/*folder,*/ folder.GetMessage(uniqueId), uniqueId, index++);
  261:  
  262:                                 messageList.Add(message);
  263:                             }
  264:                         }
  265:                     }
  266:                 }
  267:                 else
  268:                 {
  269:                     var folderList = FolderList(imapClient);
  270:  
  271:                     if (folderList.Count > 0)
  272:                     {
  273:                         foreach (var _folder in folderList)
  274:                         {
  275:                             _folder.Open(FolderAccess.ReadOnly);
  276:  
  277:                             var uniqueIdList = _folder.Search(searchQuery);
  278:  
  279:                             foreach (var uniqueId in uniqueIdList)
  280:                             {
  281:                                 message = new Message(/*_folder,*/ _folder.GetMessage(uniqueId), uniqueId, index++);
  282:  
  283:                                 messageList.Add(message);
  284:                             }
  285:                         }
  286:                     }
  287:                 }
  288:  
  289:                 imapClient.Disconnect(true);
  290:             }
  291:  
  292:             return messageList;
  293:         }
  294:  
  295:         ////////////////////////////////////////////////////////////////////////////
  296:  
  297:         /// <summary>
  298:         ///
  299:         /// </summary>
  300:         public void MoveMessage(Message message, string sourceFolderName, string destinationFolderName)
  301:         {
  302:             IMailFolder sourceFolder, destinationFolder;
  303:  
  304:             if (message != null && !string.IsNullOrEmpty(sourceFolderName) && !string.IsNullOrEmpty(destinationFolderName))
  305:             {
  306:                 using (var imapClient = new ImapClient())
  307:                 {
  308:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  309:  
  310:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  311:  
  312:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  313:  
  314:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  315:  
  316:                     var folderList = FolderList(imapClient);
  317:  
  318:                     if (folderList.Count > 0)
  319:                     {
  320:                         if (sourceFolderName.ToLower() == "inbox")
  321:                         {
  322:                             sourceFolder = folderList.Where(u => string.Equals(u.Name, sourceFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  323:  
  324:                             if (sourceFolder == null)
  325:                             {
  326:                                 sourceFolder = folderList.Where(u => string.Equals(u.Name, "sent", StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault().ParentFolder;
  327:                             }
  328:                         }
  329:                         else sourceFolder = folderList.Where(u => string.Equals(u.Name, sourceFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  330:  
  331:                         destinationFolder = folderList.Where(u => string.Equals(u.Name, destinationFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  332:  
  333:                         if (sourceFolder != null && destinationFolder != null)
  334:                         {
  335:                             sourceFolder.Open(FolderAccess.ReadWrite);
  336:  
  337:                             sourceFolder.MoveTo(message.UniqueId, destinationFolder);
  338:                         }
  339:                     }
  340:  
  341:                     imapClient.Disconnect(true);
  342:                 }
  343:             }
  344:         }
  345:  
  346:         ////////////////////////////////////////////////////////////////////////////
  347:  
  348:         /// <summary>
  349:         ///
  350:         /// </summary>
  351:         public int MoveMessagesToFolderBySenderEmail(string sourceFolderName, string destinationFolderName, string email)
  352:         {
  353:             var numberOfMessagesMoved = 0;
  354:  
  355:             if (!string.IsNullOrEmpty(sourceFolderName) && !string.IsNullOrEmpty(destinationFolderName) && !string.IsNullOrEmpty(email))
  356:             {
  357:                 var messageList = MessageListBySenderEmail(sourceFolderName, email);
  358:  
  359:                 if (messageList.Count > 0)
  360:                 {
  361:                     using (var imapClient = new ImapClient())
  362:                     {
  363:                         imapClient.Connect(imapServerHost, port, secureSocketOptions);
  364:  
  365:                         Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  366:  
  367:                         imapClient.Authenticate(imapServerUser, imapServerPassword);
  368:  
  369:                         Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  370:  
  371:                         var folderList = FolderList(imapClient);
  372:  
  373:                         if (folderList.Count > 0)
  374:                         {
  375:                             var sourceFolder = folderList.Where(u => string.Equals(u.Name, sourceFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  376:                             var destinationFolder = folderList.Where(u => string.Equals(u.Name, destinationFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  377:  
  378:                             if (sourceFolder != null && destinationFolder != null)
  379:                             {
  380:                                 foreach (var message in messageList)
  381:                                 {
  382:                                     MoveMessage(message, sourceFolderName, destinationFolderName);
  383:  
  384:                                     numberOfMessagesMoved++;
  385:                                 }
  386:                             }
  387:                         }
  388:  
  389:                         imapClient.Disconnect(true);
  390:                     }
  391:                 }
  392:             }
  393:  
  394:             return numberOfMessagesMoved;
  395:         }
  396:  
  397:         ////////////////////////////////////////////////////////////////////////////
  398:  
  399:         /// <summary>
  400:         ///
  401:         /// </summary>
  402:         public void DeleteMessage(Message message, string folderName)
  403:         {
  404:             if (message != null && !string.IsNullOrEmpty(folderName))
  405:             {
  406:                 using (var imapClient = new ImapClient())
  407:                 {
  408:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  409:  
  410:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  411:  
  412:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  413:  
  414:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  415:  
  416:                     if (folderName.ToLower() == "inbox")
  417:                     {
  418:                         var folder = imapClient.Inbox;
  419:  
  420:                         if (folder != null)
  421:                         {
  422:                             folder.Open(FolderAccess.ReadWrite);
  423:  
  424:                             folder.AddFlags(message.UniqueId, MessageFlags.Deleted, true);
  425:  
  426:                             folder.Expunge();
  427:                         }
  428:                     }
  429:                     else
  430:                     {
  431:                         var folderList = FolderList(imapClient);
  432:  
  433:                         if (folderList.Count > 0)
  434:                         {
  435:                             var folder = folderList.Where(u => string.Equals(u.Name, folderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  436:  
  437:                             if (folder != null)
  438:                             {
  439:                                 folder.Open(FolderAccess.ReadWrite);
  440:  
  441:                                 folder.AddFlags(message.UniqueId, MessageFlags.Deleted, true);
  442:  
  443:                                 folder.Expunge();
  444:                             }
  445:                         }
  446:                     }
  447:  
  448:                     imapClient.Disconnect(true);
  449:                 }
  450:             }
  451:         }
  452:  
  453:         ////////////////////////////////////////////////////////////////////////////
  454:  
  455:         /// <summary>
  456:         ///
  457:         /// </summary>
  458:         public List<IMailFolder> FolderList()
  459:         {
  460:             var folderList = new List<IMailFolder>();
  461:  
  462:             using (var imapClient = new ImapClient())
  463:             {
  464:                 imapClient.Connect(imapServerHost, port, secureSocketOptions);
  465:  
  466:                 Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  467:  
  468:                 imapClient.Authenticate(imapServerUser, imapServerPassword);
  469:  
  470:                 Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  471:  
  472:                 folderList = FolderList(imapClient);
  473:  
  474:                 imapClient.Disconnect(true);
  475:             }
  476:  
  477:             return folderList;
  478:         }
  479:  
  480:         ////////////////////////////////////////////////////////////////////////////
  481:  
  482:         /// <summary>
  483:         ///
  484:         /// </summary>
  485:         public void CreateFolder(string parentFolderName, string newFolderName)
  486:         {
  487:             if (!string.IsNullOrEmpty(parentFolderName) && !string.IsNullOrEmpty(newFolderName))
  488:             {
  489:                 using (var imapClient = new ImapClient())
  490:                 {
  491:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  492:  
  493:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  494:  
  495:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  496:  
  497:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  498:  
  499:                     if (parentFolderName.ToLower() == "inbox")
  500:                     {
  501:                         var folder = imapClient.Inbox;
  502:  
  503:                         folder.Open(FolderAccess.ReadWrite);
  504:  
  505:                         folder.Create(newFolderName, true);
  506:                     }
  507:                     else
  508:                     {
  509:                         var folderList = FolderList(imapClient);
  510:  
  511:                         if (folderList.Count > 0)
  512:                         {
  513:                             var folder = folderList.Where(u => string.Equals(u.Name, parentFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  514:  
  515:                             if (folder != null)
  516:                             {
  517:                                 folder.Open(FolderAccess.ReadWrite);
  518:  
  519:                                 folder.Create(newFolderName, true);
  520:                             }
  521:                         }
  522:                     }
  523:  
  524:                     imapClient.Disconnect(true);
  525:                 }
  526:             }
  527:         }
  528:  
  529:         ////////////////////////////////////////////////////////////////////////////
  530:  
  531:         /// <summary>
  532:         ///
  533:         /// </summary>
  534:         public void DeleteFolder(string parentFolderName, string folderName)
  535:         {
  536:             if (!string.IsNullOrEmpty(parentFolderName) && !string.IsNullOrEmpty(folderName))
  537:             {
  538:                 using (var imapClient = new ImapClient())
  539:                 {
  540:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  541:  
  542:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  543:  
  544:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  545:  
  546:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  547:  
  548:                     if (parentFolderName.ToLower() == "inbox")
  549:                     {
  550:                         var folder = imapClient.Inbox;
  551:  
  552:                         folder.Open(FolderAccess.ReadWrite);
  553:  
  554:                         var mailFolder = folder.GetSubfolder(folderName);
  555:  
  556:                         if (mailFolder != null) mailFolder.Delete();
  557:                     }
  558:                     else
  559:                     {
  560:                         var folderList = FolderList(imapClient);
  561:  
  562:                         if (folderList.Count > 0)
  563:                         {
  564:                             var folder = folderList.Where(u => string.Equals(u.Name, parentFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  565:  
  566:                             if (folder != null)
  567:                             {
  568:                                 folder.Open(FolderAccess.ReadWrite);
  569:  
  570:                                 var mailFolder = folder.GetSubfolder(folderName);
  571:  
  572:                                 if (mailFolder != null) mailFolder.Delete();
  573:                             }
  574:                         }
  575:                     }
  576:  
  577:                     imapClient.Disconnect(true);
  578:                 }
  579:             }
  580:         }
  581:  
  582:         ////////////////////////////////////////////////////////////////////////////
  583:  
  584:         /// <summary>
  585:         ///
  586:         /// </summary>
  587:         private List<IMailFolder> FolderList(ImapClient imapClient)
  588:         {
  589:             List<IMailFolder> folder0List = new List<IMailFolder>();
  590:             List<IMailFolder> folder1List = new List<IMailFolder>();
  591:             List<IMailFolder> folderList = new List<IMailFolder>();
  592:             List<FolderNamespace> folderNamespaceList = new List<FolderNamespace>();
  593:  
  594:             foreach (var ns in imapClient.PersonalNamespaces) folderNamespaceList.Add(ns);
  595:  
  596:             foreach (var ns in imapClient.SharedNamespaces) folderNamespaceList.Add(ns);
  597:  
  598:             foreach (var ns in imapClient.OtherNamespaces) folderNamespaceList.Add(ns);
  599:  
  600:             // inbox is probabrly the folder that represents the first personal namespace
  601:             //var personal = imapClient.GetFolder(imapClient.PersonalNamespaces[0]);
  602:  
  603:             foreach (var fn in folderNamespaceList)
  604:             {
  605:                 folder0List.Add(imapClient.GetFolder(fn));
  606:             }
  607:  
  608:             foreach (var f in folder0List)
  609:             {
  610:                 folder1List.AddRange(f.GetSubfolders());
  611:             }
  612:  
  613:             foreach (var f in folder1List)
  614:             {
  615:                 folderList.Add(f);
  616:  
  617:                 folderList.AddRange(f.GetSubfolders());
  618:             }
  619:  
  620:             return folderList;
  621:         }
  622:  
  623:         #region Capabilities
  624:         ////////////////////////////////////////////////////////////////////////////
  625:  
  626:         /// <summary>
  627:         ///
  628:         /// </summary>
  629:         public void Capabilities()
  630:         {
  631:             using (var imapClient = new ImapClient())
  632:             {
  633:                 imapClient.Connect(imapServerHost, port, secureSocketOptions);
  634:  
  635:                 var mechanisms = string.Join(", ", imapClient.AuthenticationMechanisms);
  636:                 Debug.WriteLine("The IMAP server supports the following SASL authentication mechanisms: {0}", mechanisms);
  637:  
  638:                 imapClient.Authenticate(imapServerUser, imapServerPassword);
  639:  
  640:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Id))
  641:                 {
  642:                     var clientImplementation = new ImapImplementation { Name = "MailKit", Version = "1.0" };
  643:                     var serverImplementation = imapClient.Identify(clientImplementation);
  644:  
  645:                     Debug.WriteLine("Server implementation details:");
  646:  
  647:                     foreach (var property in serverImplementation.Properties)
  648:                         Debug.WriteLine("  {0} = {1}", property.Key, property.Value);
  649:                 }
  650:  
  651:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Acl))
  652:                 {
  653:                     Debug.WriteLine("The IMAP server supports Access Control Lists.");
  654:  
  655:                     Debug.WriteLine("The IMAP server supports the following access rights: {0}", imapClient.Rights);
  656:  
  657:                     Debug.WriteLine("The Inbox has the following access controls:");
  658:  
  659:                     var acl = imapClient.Inbox.GetAccessControlList();
  660:  
  661:                     foreach (var ac in acl) Debug.WriteLine("  {0} = {1}", ac.Name, ac.Rights);
  662:  
  663:                     var myRights = imapClient.Inbox.GetMyAccessRights();
  664:  
  665:                     Debug.WriteLine("Your current rights for the Inbox folder are: {0}", myRights);
  666:                 }
  667:  
  668:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Quota))
  669:                 {
  670:                     Debug.WriteLine("The IMAP server supports quotas.");
  671:  
  672:                     Debug.WriteLine("The current quota for the Inbox is:");
  673:  
  674:                     var quota = imapClient.Inbox.GetQuota();
  675:  
  676:                     if (quota.StorageLimit.HasValue)
  677:                         Debug.WriteLine("  Limited by storage space. Using {0} out of {1} bytes.", quota.CurrentStorageSize.Value, quota.StorageLimit.Value);
  678:  
  679:                     if (quota.MessageLimit.HasValue)
  680:                         Debug.WriteLine("  Limited by the number of messages. Using {0} out of {1} bytes.", quota.CurrentMessageCount.Value, quota.MessageLimit.Value);
  681:  
  682:                     Debug.WriteLine("The quota root is: {0}", quota.QuotaRoot);
  683:                 }
  684:  
  685:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Thread))
  686:                 {
  687:                     if (imapClient.ThreadingAlgorithms.Contains(ThreadingAlgorithm.OrderedSubject))
  688:                         Debug.WriteLine("The IMAP server supports threading by subject.");
  689:  
  690:                     if (imapClient.ThreadingAlgorithms.Contains(ThreadingAlgorithm.References))
  691:                         Debug.WriteLine("The IMAP server supports threading by references.");
  692:                 }
  693:  
  694:                 imapClient.Disconnect(true);
  695:             }
  696:         }
  697:         #endregion
  698:  
  699:         #region Namespaces
  700:         ////////////////////////////////////////////////////////////////////////////
  701:  
  702:         /// <summary>
  703:         ///
  704:         /// </summary>
  705:         public void ShowNamespaces()
  706:         {
  707:             using (var imapClient = new ImapClient())
  708:             {
  709:                 imapClient.Connect(imapServerHost, port, secureSocketOptions);
  710:  
  711:                 Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  712:  
  713:                 imapClient.Authenticate(imapServerUser, imapServerPassword);
  714:  
  715:                 Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  716:  
  717:                 Debug.WriteLine("Personal namespaces:");
  718:  
  719:                 foreach (var ns in imapClient.PersonalNamespaces)
  720:                     Debug.WriteLine($"* \"{ns.Path}\" \"{ns.DirectorySeparator}\"");
  721:  
  722:                 Debug.WriteLine("");
  723:  
  724:                 Debug.WriteLine("Shared namespaces:");
  725:  
  726:                 foreach (var ns in imapClient.SharedNamespaces)
  727:                     Debug.WriteLine($"* \"{ns.Path}\" \"{ns.DirectorySeparator}\"");
  728:  
  729:                 Debug.WriteLine("");
  730:  
  731:                 Debug.WriteLine("Other namespaces:");
  732:  
  733:                 foreach (var ns in imapClient.OtherNamespaces)
  734:                     Debug.WriteLine($"* \"{ns.Path}\" \"{ns.DirectorySeparator}\"");
  735:  
  736:                 Debug.WriteLine("");
  737:  
  738:                 // get the folder that represents the first personal namespace
  739:                 var personal = imapClient.GetFolder(imapClient.PersonalNamespaces[0]);
  740:  
  741:                 // list the folders under the first personal namespace
  742:                 var subfolders = personal.GetSubfolders();
  743:  
  744:                 Debug.WriteLine("The list of folders that are direct children of the first personal namespace:");
  745:  
  746:                 foreach (var folder in subfolders)
  747:                     Debug.WriteLine($"* {folder.Name}");
  748:  
  749:                 imapClient.Disconnect(true);
  750:             }
  751:         }
  752:         #endregion
  753:  
  754:         ////////////////////////////////////////////////////////////////////////////
  755:         ////////////////////////////////////////////////////////////////////////////
  756:     }
  757: }