)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Skip Navigation LinksHome » Code Library » Mail

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

Mail process support class of Optical Fiber Network (OFN) business model.

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