)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
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-2025 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:                             try
  242:                             {
  243:                                 message = new Message(/*folder,*/ folder.GetMessage(uniqueId), uniqueId, index++);
  244:  
  245:                                 messageList.Add(message);
  246:                             }
  247:                             catch (MailKit.MessageNotFoundException)
  248:                             {
  249:                             }
  250:                         }
  251:                     }
  252:                     else
  253:                     {
  254:                         var folderList = FolderList(imapClient);
  255:  
  256:                         if (folderList.Count > 0)
  257:                         {
  258:                             folder = folderList.Where(u => string.Equals(u.Name, folderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  259:  
  260:                             folder.Open(FolderAccess.ReadOnly);
  261:  
  262:                             var uniqueIdList = folder.Search(searchQuery);
  263:  
  264:                             foreach (var uniqueId in uniqueIdList)
  265:                             {
  266:                                 message = new Message(/*folder,*/ folder.GetMessage(uniqueId), uniqueId, index++);
  267:  
  268:                                 messageList.Add(message);
  269:                             }
  270:                         }
  271:                     }
  272:                 }
  273:                 else
  274:                 {
  275:                     var folderList = FolderList(imapClient);
  276:  
  277:                     if (folderList.Count > 0)
  278:                     {
  279:                         foreach (var _folder in folderList)
  280:                         {
  281:                             _folder.Open(FolderAccess.ReadOnly);
  282:  
  283:                             var uniqueIdList = _folder.Search(searchQuery);
  284:  
  285:                             foreach (var uniqueId in uniqueIdList)
  286:                             {
  287:                                 message = new Message(/*_folder,*/ _folder.GetMessage(uniqueId), uniqueId, index++);
  288:  
  289:                                 messageList.Add(message);
  290:                             }
  291:                         }
  292:                     }
  293:                 }
  294:  
  295:                 imapClient.Disconnect(true);
  296:             }
  297:  
  298:             return messageList;
  299:         }
  300:  
  301:         ////////////////////////////////////////////////////////////////////////////
  302:  
  303:         /// <summary>
  304:         ///
  305:         /// </summary>
  306:         public void MoveMessage(Message message, string sourceFolderName, string destinationFolderName)
  307:         {
  308:             IMailFolder sourceFolder, destinationFolder;
  309:  
  310:             if (message != null && !string.IsNullOrEmpty(sourceFolderName) && !string.IsNullOrEmpty(destinationFolderName))
  311:             {
  312:                 using (var imapClient = new ImapClient())
  313:                 {
  314:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  315:  
  316:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  317:  
  318:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  319:  
  320:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  321:  
  322:                     var folderList = FolderList(imapClient);
  323:  
  324:                     if (folderList.Count > 0)
  325:                     {
  326:                         if (sourceFolderName.ToLower() == "inbox")
  327:                         {
  328:                             sourceFolder = folderList.Where(u => string.Equals(u.Name, sourceFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  329:  
  330:                             if (sourceFolder == null)
  331:                             {
  332:                                 sourceFolder = folderList.Where(u => string.Equals(u.Name, "sent", StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault().ParentFolder;
  333:                             }
  334:                         }
  335:                         else sourceFolder = folderList.Where(u => string.Equals(u.Name, sourceFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  336:  
  337:                         destinationFolder = folderList.Where(u => string.Equals(u.Name, destinationFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  338:  
  339:                         if (sourceFolder != null && destinationFolder != null)
  340:                         {
  341:                             sourceFolder.Open(FolderAccess.ReadWrite);
  342:  
  343:                             sourceFolder.MoveTo(message.UniqueId, destinationFolder);
  344:                         }
  345:                     }
  346:  
  347:                     imapClient.Disconnect(true);
  348:                 }
  349:             }
  350:         }
  351:  
  352:         ////////////////////////////////////////////////////////////////////////////
  353:  
  354:         /// <summary>
  355:         ///
  356:         /// </summary>
  357:         public int MoveMessagesToFolderBySenderEmail(string sourceFolderName, string destinationFolderName, string email)
  358:         {
  359:             var numberOfMessagesMoved = 0;
  360:  
  361:             if (!string.IsNullOrEmpty(sourceFolderName) && !string.IsNullOrEmpty(destinationFolderName) && !string.IsNullOrEmpty(email))
  362:             {
  363:                 var messageList = MessageListBySenderEmail(sourceFolderName, email);
  364:  
  365:                 if (messageList.Count > 0)
  366:                 {
  367:                     using (var imapClient = new ImapClient())
  368:                     {
  369:                         imapClient.Connect(imapServerHost, port, secureSocketOptions);
  370:  
  371:                         Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  372:  
  373:                         imapClient.Authenticate(imapServerUser, imapServerPassword);
  374:  
  375:                         Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  376:  
  377:                         var folderList = FolderList(imapClient);
  378:  
  379:                         if (folderList.Count > 0)
  380:                         {
  381:                             var sourceFolder = folderList.Where(u => string.Equals(u.Name, sourceFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  382:                             var destinationFolder = folderList.Where(u => string.Equals(u.Name, destinationFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  383:  
  384:                             if (sourceFolder != null && destinationFolder != null)
  385:                             {
  386:                                 foreach (var message in messageList)
  387:                                 {
  388:                                     MoveMessage(message, sourceFolderName, destinationFolderName);
  389:  
  390:                                     numberOfMessagesMoved++;
  391:                                 }
  392:                             }
  393:                         }
  394:  
  395:                         imapClient.Disconnect(true);
  396:                     }
  397:                 }
  398:             }
  399:  
  400:             return numberOfMessagesMoved;
  401:         }
  402:  
  403:         ////////////////////////////////////////////////////////////////////////////
  404:  
  405:         /// <summary>
  406:         ///
  407:         /// </summary>
  408:         public void DeleteMessage(Message message, string folderName)
  409:         {
  410:             if (message != null && !string.IsNullOrEmpty(folderName))
  411:             {
  412:                 using (var imapClient = new ImapClient())
  413:                 {
  414:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  415:  
  416:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  417:  
  418:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  419:  
  420:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  421:  
  422:                     if (folderName.ToLower() == "inbox")
  423:                     {
  424:                         var folder = imapClient.Inbox;
  425:  
  426:                         if (folder != null)
  427:                         {
  428:                             folder.Open(FolderAccess.ReadWrite);
  429:  
  430:                             folder.AddFlags(message.UniqueId, MessageFlags.Deleted, true);
  431:  
  432:                             folder.Expunge();
  433:                         }
  434:                     }
  435:                     else
  436:                     {
  437:                         var folderList = FolderList(imapClient);
  438:  
  439:                         if (folderList.Count > 0)
  440:                         {
  441:                             var folder = folderList.Where(u => string.Equals(u.Name, folderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  442:  
  443:                             if (folder != null)
  444:                             {
  445:                                 folder.Open(FolderAccess.ReadWrite);
  446:  
  447:                                 folder.AddFlags(message.UniqueId, MessageFlags.Deleted, true);
  448:  
  449:                                 folder.Expunge();
  450:                             }
  451:                         }
  452:                     }
  453:  
  454:                     imapClient.Disconnect(true);
  455:                 }
  456:             }
  457:         }
  458:  
  459:         ////////////////////////////////////////////////////////////////////////////
  460:  
  461:         /// <summary>
  462:         ///
  463:         /// </summary>
  464:         public List<IMailFolder> FolderList()
  465:         {
  466:             var folderList = new List<IMailFolder>();
  467:  
  468:             using (var imapClient = new ImapClient())
  469:             {
  470:                 imapClient.Connect(imapServerHost, port, secureSocketOptions);
  471:  
  472:                 Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  473:  
  474:                 imapClient.Authenticate(imapServerUser, imapServerPassword);
  475:  
  476:                 Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  477:  
  478:                 folderList = FolderList(imapClient);
  479:  
  480:                 imapClient.Disconnect(true);
  481:             }
  482:  
  483:             return folderList;
  484:         }
  485:  
  486:         ////////////////////////////////////////////////////////////////////////////
  487:  
  488:         /// <summary>
  489:         ///
  490:         /// </summary>
  491:         public void CreateFolder(string parentFolderName, string newFolderName)
  492:         {
  493:             if (!string.IsNullOrEmpty(parentFolderName) && !string.IsNullOrEmpty(newFolderName))
  494:             {
  495:                 using (var imapClient = new ImapClient())
  496:                 {
  497:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  498:  
  499:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  500:  
  501:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  502:  
  503:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  504:  
  505:                     if (parentFolderName.ToLower() == "inbox")
  506:                     {
  507:                         var folder = imapClient.Inbox;
  508:  
  509:                         folder.Open(FolderAccess.ReadWrite);
  510:  
  511:                         folder.Create(newFolderName, true);
  512:                     }
  513:                     else
  514:                     {
  515:                         var folderList = FolderList(imapClient);
  516:  
  517:                         if (folderList.Count > 0)
  518:                         {
  519:                             var folder = folderList.Where(u => string.Equals(u.Name, parentFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  520:  
  521:                             if (folder != null)
  522:                             {
  523:                                 folder.Open(FolderAccess.ReadWrite);
  524:  
  525:                                 folder.Create(newFolderName, true);
  526:                             }
  527:                         }
  528:                     }
  529:  
  530:                     imapClient.Disconnect(true);
  531:                 }
  532:             }
  533:         }
  534:  
  535:         ////////////////////////////////////////////////////////////////////////////
  536:  
  537:         /// <summary>
  538:         ///
  539:         /// </summary>
  540:         public void DeleteFolder(string parentFolderName, string folderName)
  541:         {
  542:             if (!string.IsNullOrEmpty(parentFolderName) && !string.IsNullOrEmpty(folderName))
  543:             {
  544:                 using (var imapClient = new ImapClient())
  545:                 {
  546:                     imapClient.Connect(imapServerHost, port, secureSocketOptions);
  547:  
  548:                     Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  549:  
  550:                     imapClient.Authenticate(imapServerUser, imapServerPassword);
  551:  
  552:                     Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  553:  
  554:                     if (parentFolderName.ToLower() == "inbox")
  555:                     {
  556:                         var folder = imapClient.Inbox;
  557:  
  558:                         folder.Open(FolderAccess.ReadWrite);
  559:  
  560:                         var mailFolder = folder.GetSubfolder(folderName);
  561:  
  562:                         if (mailFolder != null) mailFolder.Delete();
  563:                     }
  564:                     else
  565:                     {
  566:                         var folderList = FolderList(imapClient);
  567:  
  568:                         if (folderList.Count > 0)
  569:                         {
  570:                             var folder = folderList.Where(u => string.Equals(u.Name, parentFolderName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
  571:  
  572:                             if (folder != null)
  573:                             {
  574:                                 folder.Open(FolderAccess.ReadWrite);
  575:  
  576:                                 var mailFolder = folder.GetSubfolder(folderName);
  577:  
  578:                                 if (mailFolder != null) mailFolder.Delete();
  579:                             }
  580:                         }
  581:                     }
  582:  
  583:                     imapClient.Disconnect(true);
  584:                 }
  585:             }
  586:         }
  587:  
  588:         ////////////////////////////////////////////////////////////////////////////
  589:  
  590:         /// <summary>
  591:         ///
  592:         /// </summary>
  593:         private List<IMailFolder> FolderList(ImapClient imapClient)
  594:         {
  595:             List<IMailFolder> folder0List = new List<IMailFolder>();
  596:             List<IMailFolder> folder1List = new List<IMailFolder>();
  597:             List<IMailFolder> folderList = new List<IMailFolder>();
  598:             List<FolderNamespace> folderNamespaceList = new List<FolderNamespace>();
  599:  
  600:             foreach (var ns in imapClient.PersonalNamespaces) folderNamespaceList.Add(ns);
  601:  
  602:             foreach (var ns in imapClient.SharedNamespaces) folderNamespaceList.Add(ns);
  603:  
  604:             foreach (var ns in imapClient.OtherNamespaces) folderNamespaceList.Add(ns);
  605:  
  606:             // inbox is probabrly the folder that represents the first personal namespace
  607:             //var personal = imapClient.GetFolder(imapClient.PersonalNamespaces[0]);
  608:  
  609:             foreach (var fn in folderNamespaceList)
  610:             {
  611:                 folder0List.Add(imapClient.GetFolder(fn));
  612:             }
  613:  
  614:             foreach (var f in folder0List)
  615:             {
  616:                 folder1List.AddRange(f.GetSubfolders());
  617:             }
  618:  
  619:             foreach (var f in folder1List)
  620:             {
  621:                 folderList.Add(f);
  622:  
  623:                 folderList.AddRange(f.GetSubfolders());
  624:             }
  625:  
  626:             return folderList;
  627:         }
  628:  
  629:         #region Capabilities
  630:         ////////////////////////////////////////////////////////////////////////////
  631:  
  632:         /// <summary>
  633:         ///
  634:         /// </summary>
  635:         public void Capabilities()
  636:         {
  637:             using (var imapClient = new ImapClient())
  638:             {
  639:                 imapClient.Connect(imapServerHost, port, secureSocketOptions);
  640:  
  641:                 var mechanisms = string.Join(", ", imapClient.AuthenticationMechanisms);
  642:                 Debug.WriteLine("The IMAP server supports the following SASL authentication mechanisms: {0}", mechanisms);
  643:  
  644:                 imapClient.Authenticate(imapServerUser, imapServerPassword);
  645:  
  646:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Id))
  647:                 {
  648:                     var clientImplementation = new ImapImplementation { Name = "MailKit", Version = "1.0" };
  649:                     var serverImplementation = imapClient.Identify(clientImplementation);
  650:  
  651:                     Debug.WriteLine("Server implementation details:");
  652:  
  653:                     foreach (var property in serverImplementation.Properties)
  654:                         Debug.WriteLine("  {0} = {1}", property.Key, property.Value);
  655:                 }
  656:  
  657:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Acl))
  658:                 {
  659:                     Debug.WriteLine("The IMAP server supports Access Control Lists.");
  660:  
  661:                     Debug.WriteLine("The IMAP server supports the following access rights: {0}", imapClient.Rights);
  662:  
  663:                     Debug.WriteLine("The Inbox has the following access controls:");
  664:  
  665:                     var acl = imapClient.Inbox.GetAccessControlList();
  666:  
  667:                     foreach (var ac in acl) Debug.WriteLine("  {0} = {1}", ac.Name, ac.Rights);
  668:  
  669:                     var myRights = imapClient.Inbox.GetMyAccessRights();
  670:  
  671:                     Debug.WriteLine("Your current rights for the Inbox folder are: {0}", myRights);
  672:                 }
  673:  
  674:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Quota))
  675:                 {
  676:                     Debug.WriteLine("The IMAP server supports quotas.");
  677:  
  678:                     Debug.WriteLine("The current quota for the Inbox is:");
  679:  
  680:                     var quota = imapClient.Inbox.GetQuota();
  681:  
  682:                     if (quota.StorageLimit.HasValue)
  683:                         Debug.WriteLine("  Limited by storage space. Using {0} out of {1} bytes.", quota.CurrentStorageSize.Value, quota.StorageLimit.Value);
  684:  
  685:                     if (quota.MessageLimit.HasValue)
  686:                         Debug.WriteLine("  Limited by the number of messages. Using {0} out of {1} bytes.", quota.CurrentMessageCount.Value, quota.MessageLimit.Value);
  687:  
  688:                     Debug.WriteLine("The quota root is: {0}", quota.QuotaRoot);
  689:                 }
  690:  
  691:                 if (imapClient.Capabilities.HasFlag(ImapCapabilities.Thread))
  692:                 {
  693:                     if (imapClient.ThreadingAlgorithms.Contains(ThreadingAlgorithm.OrderedSubject))
  694:                         Debug.WriteLine("The IMAP server supports threading by subject.");
  695:  
  696:                     if (imapClient.ThreadingAlgorithms.Contains(ThreadingAlgorithm.References))
  697:                         Debug.WriteLine("The IMAP server supports threading by references.");
  698:                 }
  699:  
  700:                 imapClient.Disconnect(true);
  701:             }
  702:         }
  703:         #endregion
  704:  
  705:         #region Namespaces
  706:         ////////////////////////////////////////////////////////////////////////////
  707:  
  708:         /// <summary>
  709:         ///
  710:         /// </summary>
  711:         public void ShowNamespaces()
  712:         {
  713:             using (var imapClient = new ImapClient())
  714:             {
  715:                 imapClient.Connect(imapServerHost, port, secureSocketOptions);
  716:  
  717:                 Debug.WriteLine("Connect(): Connection opened to " + imapServerHost);
  718:  
  719:                 imapClient.Authenticate(imapServerUser, imapServerPassword);
  720:  
  721:                 Debug.WriteLine(string.Format("Connect(): Login to '{0}' by user '{1}' successful", imapServerHost, imapServerUser));
  722:  
  723:                 Debug.WriteLine("Personal namespaces:");
  724:  
  725:                 foreach (var ns in imapClient.PersonalNamespaces)
  726:                     Debug.WriteLine($"* \"{ns.Path}\" \"{ns.DirectorySeparator}\"");
  727:  
  728:                 Debug.WriteLine("");
  729:  
  730:                 Debug.WriteLine("Shared namespaces:");
  731:  
  732:                 foreach (var ns in imapClient.SharedNamespaces)
  733:                     Debug.WriteLine($"* \"{ns.Path}\" \"{ns.DirectorySeparator}\"");
  734:  
  735:                 Debug.WriteLine("");
  736:  
  737:                 Debug.WriteLine("Other namespaces:");
  738:  
  739:                 foreach (var ns in imapClient.OtherNamespaces)
  740:                     Debug.WriteLine($"* \"{ns.Path}\" \"{ns.DirectorySeparator}\"");
  741:  
  742:                 Debug.WriteLine("");
  743:  
  744:                 // get the folder that represents the first personal namespace
  745:                 var personal = imapClient.GetFolder(imapClient.PersonalNamespaces[0]);
  746:  
  747:                 // list the folders under the first personal namespace
  748:                 var subfolders = personal.GetSubfolders();
  749:  
  750:                 Debug.WriteLine("The list of folders that are direct children of the first personal namespace:");
  751:  
  752:                 foreach (var folder in subfolders)
  753:                     Debug.WriteLine($"* {folder.Name}");
  754:  
  755:                 imapClient.Disconnect(true);
  756:             }
  757:         }
  758:         #endregion
  759:  
  760:         ////////////////////////////////////////////////////////////////////////////
  761:         ////////////////////////////////////////////////////////////////////////////
  762:     }
  763: }