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

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

Mail process support class of Fixed Telecommunications Network (FTN) business model.

    1: using MailKit;
    2: using System;
    3: using System.Collections;
    4: using System.Collections.Generic;
    5: using System.Configuration;
    6: using System.Linq;
    7: using System.Text;
    8: using System.Text.RegularExpressions;
    9: using System.Xml;
   10: using System.Xml.Serialization;
   11:  
   12: namespace Ia.Ftn.Cl.Models.Business
   13: {
   14:     ////////////////////////////////////////////////////////////////////////////
   15:  
   16:     /// <summary publish="true">
   17:     /// Mail process support class of Fixed Telecommunications Network (FTN) business model.
   18:     /// </summary>
   19:     /// 
   20:     /// <remarks> 
   21:     /// Copyright © 2006-2021 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 Mail
   34:     {
   35:         private readonly List<string> folderList, mailRegexToFolderList;
   36:  
   37:         /// <summary/>
   38:         public bool EnabledSsl { get; set; }
   39:  
   40:         /// <summary/>
   41:         public bool IsActive { get; set; }
   42:  
   43:         /// <summary/>
   44:         public bool IsActiveDuringWorkingHoursOnly { get; set; }
   45:  
   46:         /// <summary/>
   47:         public int MailboxCheckFrequencyInMinutes { get; set; }
   48:  
   49:         /// <summary/>
   50:         public string Host { get; set; }
   51:  
   52:         /// <summary/>
   53:         public string UserName { get; set; }
   54:  
   55:         /// <summary/>
   56:         public string Password { get; set; }
   57:  
   58:         /// <summary/>
   59:         public List<string> FolderList { get { return folderList; } }
   60:  
   61:         /// <summary/>
   62:         public List<string> MailRegexToMailboxList { get { return mailRegexToFolderList; } }
   63:  
   64:         ////////////////////////////////////////////////////////////////////////////
   65:  
   66:         /// <summary>
   67:         ///
   68:         /// </summary>
   69:         public Mail()
   70:         {
   71:             mailRegexToFolderList = new List<string>(100);
   72:             folderList = new List<string>(100);
   73:         }
   74:  
   75:         ////////////////////////////////////////////////////////////////////////////
   76:  
   77:         /// <summary>
   78:         ///
   79:         /// </summary>
   80:         public bool AddFolder(string name, out string result)
   81:         {
   82:             bool b;
   83:             string s;
   84:  
   85:             if (Ia.Cl.Models.File.IsValidFilename(name))
   86:             {
   87:                 // below: check if the folder name exists in upper or lower forms
   88:                 s = (from m in folderList where m.ToLower() == name.ToLower() select m).SingleOrDefault();
   89:  
   90:                 if (s == null) // no duplicates
   91:                 {
   92:                     folderList.Add(name);
   93:  
   94:                     result = "Success: Folder name '" + name + "' assigned to be added. ";
   95:                     b = true;
   96:                 }
   97:                 else
   98:                 {
   99:                     result = "Error: Folder name '" + name + "' already assigned or exists. ";
  100:                     b = false;
  101:                 }
  102:             }
  103:             else
  104:             {
  105:                 result = "Error: Folder '" + name + "' is not valid. ";
  106:                 b = false;
  107:             }
  108:  
  109:             return b;
  110:         }
  111:  
  112:         ////////////////////////////////////////////////////////////////////////////
  113:  
  114:         /// <summary>
  115:         ///
  116:         /// </summary>
  117:         public bool DeleteFolder(string name, out string result)
  118:         {
  119:             bool b;
  120:  
  121:             if (folderList.Contains(name) || folderList.Contains(name.ToLower()))
  122:             {
  123:                 folderList.Remove(name);
  124:  
  125:                 result = "Success: Mailbox name '" + name + "' assigned to be deleted. ";
  126:                 b = true;
  127:             }
  128:             else
  129:             {
  130:                 result = "Error: Mailbox name '" + name + "' does not exist. ";
  131:                 b = false;
  132:             }
  133:  
  134:             return b;
  135:         }
  136:  
  137:         ////////////////////////////////////////////////////////////////////////////
  138:  
  139:         /// <summary>
  140:         ///
  141:         /// </summary>
  142:         public void CopyFolderList(List<string> newFolderList)
  143:         {
  144:             // below: this will copy folder list into object
  145:  
  146:             folderList.Clear();
  147:  
  148:             foreach (string s in newFolderList) folderList.Add(s);
  149:         }
  150:  
  151:         ////////////////////////////////////////////////////////////////////////////
  152:  
  153:         /// <summary>
  154:         ///
  155:         /// </summary>
  156:         public bool AddRegex(string folderName, string regex, out string result)
  157:         {
  158:             bool b;
  159:  
  160:             if (Ia.Cl.Models.Default.IsRegexPatternValid(regex))
  161:             {
  162:                 if (folderList.Contains(folderName) || folderList.Contains(folderName.ToLower()))
  163:                 {
  164:                     if (!mailRegexToFolderList.Contains(regex + " (" + folderName + ")"))
  165:                     {
  166:                         mailRegexToFolderList.Add(regex + " (" + folderName + ")");
  167:  
  168:                         result = "Success: Regex '" + regex + "' for folder '" + folderName + "' is added. ";
  169:                         b = true;
  170:                     }
  171:                     else
  172:                     {
  173:                         result = "Error: Regex '" + regex + "' for folder '" + folderName + "' already exists. ";
  174:                         b = false;
  175:                     }
  176:                 }
  177:                 else
  178:                 {
  179:                     result = "Error: Folder '" + folderName + "' is not assigned or does not exist. ";
  180:                     b = false;
  181:                 }
  182:             }
  183:             else
  184:             {
  185:                 result = "Error: Regex pattern '" + regex + "' is invalid. ";
  186:                 b = false;
  187:             }
  188:  
  189:             return b;
  190:         }
  191:  
  192:         ////////////////////////////////////////////////////////////////////////////
  193:  
  194:         /// <summary>
  195:         ///
  196:         /// </summary>
  197:         public bool DeleteRegex(string regex, out string result)
  198:         {
  199:             bool b;
  200:  
  201:             if (mailRegexToFolderList.Contains(regex))
  202:             {
  203:                 mailRegexToFolderList.Remove(regex);
  204:  
  205:                 result = "Success: regex '" + regex + "' was removed. ";
  206:                 b = true;
  207:             }
  208:             else
  209:             {
  210:                 result = "Error: regex does not exist. ";
  211:                 b = false;
  212:             }
  213:  
  214:             return b;
  215:         }
  216:  
  217:         ////////////////////////////////////////////////////////////////////////////
  218:         ////////////////////////////////////////////////////////////////////////////
  219:  
  220:         /// <summary>
  221:         ///
  222:         /// </summary>
  223:         public static Mail DeserializeFromString(string serializedObject)
  224:         {
  225:             Mail m;
  226:  
  227:             m = serializedObject.XmlDeserializeFromString<Mail>();
  228:  
  229:             return m;
  230:         }
  231:  
  232:         ////////////////////////////////////////////////////////////////////////////
  233:  
  234:         /// <summary>
  235:         ///
  236:         /// </summary>
  237:         public string SerializeToString()
  238:         {
  239:             StringBuilder sb;
  240:  
  241:             sb = new StringBuilder(10000);
  242:  
  243:             var serializer = new XmlSerializer(typeof(Mail));
  244:  
  245:             using (var writer = XmlWriter.Create(sb))
  246:             {
  247:                 serializer.Serialize(writer, this);
  248:             }
  249:  
  250:             return sb.ToString();
  251:         }
  252:  
  253:         ////////////////////////////////////////////////////////////////////////////
  254:  
  255:         /// <summary>
  256:         ///
  257:         /// </summary>
  258:         public static bool Sorter(Mail mail, out Ia.Cl.Models.Result result)
  259:         {
  260:             bool b;
  261:             int numberOfMessagesMoved;
  262:             string regex, destinationFolder; //, messageId, email;
  263:             List<string> folderList;
  264:             List<string> foldersToAddList;
  265:             Match match;
  266:             Ia.Cl.Models.Imap imap2;
  267:  
  268:             numberOfMessagesMoved = 0;
  269:             result = new Ia.Cl.Models.Result();
  270:  
  271:             foldersToAddList = new List<string>(100);
  272:  
  273:             try
  274:             {
  275:                 imap2 = new Ia.Cl.Models.Imap(mail.Host, mail.UserName, mail.Password, mail.EnabledSsl);
  276:                 //imap2 = new Ia.Cl.Models.Imap("imap.*.com", "*@*.com", "*", false);
  277:  
  278:                 // below: read all folders
  279:                 folderList = imap2.FolderList().Select(u => u.Name).ToList();
  280:  
  281:                 // below: determain change if any
  282:                 // below: mailboxes to add:
  283:                 foreach (string s in mail.FolderList)
  284:                 {
  285:                     if (!folderList.Contains(s)) foldersToAddList.Add(s);
  286:                 }
  287:  
  288:                 // below: mailboxes to delete:
  289:                 // below: I will disable deletion of mailboxes
  290:                 // foreach (string s in mailboxArrayList) if (!m.MailboxList.Contains(s)) mailboxesToAddArrayList.Add(s);
  291:  
  292:                 // below: create mailboxes
  293:                 foreach (var s in foldersToAddList) imap2.CreateFolder("inbox", s);
  294:  
  295:                 // below: delete mailboxes
  296:                 // foreach (string s in mailboxesToDeleteArrayList) imap.DeleteMailbox(s);
  297:  
  298:                 // below: read all mailboxes again
  299:                 folderList = imap2.FolderList().Select(u => u.Name).ToList();
  300:  
  301:                 // below: assign change to object
  302:                 mail.CopyFolderList(folderList);
  303:  
  304:                 // below: start moving
  305:                 foreach (string s in mail.MailRegexToMailboxList)
  306:                 {
  307:                     match = Regex.Match(s, @"(.+?) \((.+?)\)");
  308:  
  309:                     if (match.Success)
  310:                     {
  311:                         regex = match.Groups[1].Value;
  312:                         destinationFolder = match.Groups[2].Value;
  313:  
  314:                         //numberOfMessagesMoved += imap2.MoveMessagesFromEmailToMailbox(regex, destinationMailbox);
  315:                     }
  316:                 }
  317:  
  318:                 result.AddSuccess("Number of messages moved: " + numberOfMessagesMoved + ". ");
  319:  
  320:                 b = true;
  321:             }
  322:             catch (Exception ex)
  323:             {
  324:                 b = false;
  325:  
  326:                 result.AddSuccess("Exception: " + ex.ToString());
  327:             }
  328:  
  329:             return b;
  330:         }
  331:  
  332:         ////////////////////////////////////////////////////////////////////////////
  333:         ////////////////////////////////////////////////////////////////////////////
  334:     }
  335:  
  336:     ////////////////////////////////////////////////////////////////////////////
  337:     ////////////////////////////////////////////////////////////////////////////
  338: }