شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

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 Next Generation Network'a (NGN's) business model.

   1:  using System;
   2:  using System.Collections;
   3:  using System.Collections.Generic;
   4:  using System.Linq;
   5:  using System.Text.RegularExpressions;
   6:  using System.Xml;
   7:  using System.Xml.Serialization;
   8:  using System.Text;
   9:  using System.Configuration;
  10:   
  11:  namespace Ia.Ngn.Cl.Model.Business
  12:  {
  13:      ////////////////////////////////////////////////////////////////////////////
  14:   
  15:      /// <summary publish="true">
  16:      /// Mail process support class of Next Generation Network'a (NGN's) business model.
  17:      /// </summary>
  18:      /// 
  19:      /// <remarks> 
  20:      /// Copyright © 2006-2017 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet 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 partial class Mail
  33:      {
  34:          private bool serverEnableSsl;
  35:          private string serverHost, serverUser, serverPassword; //, defaultMailbox;
  36:          private Ia.Cl.Model.Imap imap;
  37:   
  38:          private List<string> mailboxList;
  39:          private List<string> mailRegexToMailboxList;
  40:   
  41:          /// <summary/>
  42:          public bool EnabledSsl { get; set; }
  43:          /// <summary/>
  44:          public bool IsActive { get; set; }
  45:          /// <summary/>
  46:          public bool IsActiveDuringWorkingHoursOnly { get; set; }
  47:          /// <summary/>
  48:          public int MailboxCheckFrequencyInMinutes { get; set; }
  49:          /// <summary/>
  50:          public string Host { get; set; }
  51:          /// <summary/>
  52:          public string UserName { get; set; }
  53:          /// <summary/>
  54:          public string Password { get; set; }
  55:          /// <summary/>
  56:          public List<string> MailboxList { get { return mailboxList; } }
  57:          /// <summary/>
  58:          public List<string> MailRegexToMailboxList { get { return mailRegexToMailboxList; } }
  59:   
  60:          ////////////////////////////////////////////////////////////////////////////
  61:   
  62:          /// <summary>
  63:          ///
  64:          /// </summary>
  65:          public Mail()
  66:          {
  67:              bool b;
  68:   
  69:              mailRegexToMailboxList = new List<string>(100);
  70:              mailboxList = new List<string>(100);
  71:   
  72:              serverHost = ConfigurationManager.AppSettings["imapServerHost"].ToString();
  73:              serverUser = ConfigurationManager.AppSettings["imapServerUser"].ToString();
  74:              serverPassword = ConfigurationManager.AppSettings["imapServerPassword"].ToString();
  75:              serverEnableSsl = bool.TryParse(ConfigurationManager.AppSettings["imapServerEnableSsl"].ToString(), out b) ? b : false;
  76:          }
  77:   
  78:          ////////////////////////////////////////////////////////////////////////////
  79:   
  80:          /// <summary>
  81:          ///
  82:          /// </summary>
  83:          public void Connect()
  84:          {
  85:              imap = new Ia.Cl.Model.Imap(serverHost, serverUser, serverPassword, serverEnableSsl);
  86:          }
  87:   
  88:          ////////////////////////////////////////////////////////////////////////////
  89:   
  90:          /// <summary>
  91:          ///
  92:          /// </summary>
  93:          public bool IsConnected
  94:          {
  95:              get
  96:              {
  97:                  return imap.IsConnected;
  98:              }
  99:          }
 100:   
 101:          ////////////////////////////////////////////////////////////////////////////
 102:   
 103:          /// <summary>
 104:          ///
 105:          /// </summary>
 106:          public void Disconnect()
 107:          {
 108:              imap.Disconnect();
 109:          }
 110:   
 111:          ////////////////////////////////////////////////////////////////////////////
 112:   
 113:          /// <summary>
 114:          ///
 115:          /// </summary>
 116:          public void InitializeMailboxes()
 117:          {
 118:              // Create Processed and Unprocessed folders if they don't already exist
 119:   
 120:              //bool b;
 121:              List<string> mailboxList;
 122:   
 123:              //b = false;
 124:   
 125:              try
 126:              {
 127:                  this.Connect();
 128:   
 129:                  if (this.IsConnected)
 130:                  {
 131:                      mailboxList = imap.MailboxList();
 132:   
 133:                      // below: determain change if any
 134:                      // below: mailboxes to add:
 135:                      if (!mailboxList.Contains("Processed")) imap.CreateMailbox("Processed");
 136:                      if (!mailboxList.Contains("Unprocessed")) imap.CreateMailbox("Unprocessed");
 137:   
 138:                      // below: read all mailboxes again
 139:                      mailboxList = imap.MailboxList();
 140:   
 141:                      //b = true;
 142:                  }
 143:   
 144:                  this.Disconnect();
 145:              }
 146:              catch (Exception)// ex)
 147:              {
 148:                  //b = false;
 149:   
 150:                  //result = ex.ToString();
 151:              }
 152:          }
 153:   
 154:          ////////////////////////////////////////////////////////////////////////////
 155:   
 156:          /// <summary>
 157:          ///
 158:          /// </summary>
 159:          public bool AddMailbox(string name, out string result)
 160:          {
 161:              bool b;
 162:              string s;
 163:   
 164:              if (Ia.Cl.Model.File.IsValidFilename(name))
 165:              {
 166:                  // below: check if the mailbox name exists in upper or lower forms
 167:                  s = (from m in mailboxList where m.ToLower() == name.ToLower() select m).SingleOrDefault();
 168:   
 169:                  if (s == null) // no duplicates
 170:                  {
 171:                      mailboxList.Add(name);
 172:   
 173:                      result = "Success: Mailbox name '" + name + "' assigned to be added. ";
 174:                      b = true;
 175:                  }
 176:                  else
 177:                  {
 178:                      result = "Error: Mailbox name '" + name + "' already assigned or exists. ";
 179:                      b = false;
 180:                  }
 181:              }
 182:              else
 183:              {
 184:                  result = "Error: Mailbox '" + name + "' is not valid. ";
 185:                  b = false;
 186:              }
 187:   
 188:              return b;
 189:          }
 190:   
 191:          ////////////////////////////////////////////////////////////////////////////
 192:   
 193:          /// <summary>
 194:          ///
 195:          /// </summary>
 196:          public bool DeleteMailbox(string name, out string result)
 197:          {
 198:              bool b;
 199:   
 200:              if (mailboxList.Contains(name) || mailboxList.Contains(name.ToLower()))
 201:              {
 202:                  mailboxList.Remove(name);
 203:   
 204:                  result = "Success: Mailbox name '" + name + "' assigned to be deleted. ";
 205:                  b = true;
 206:              }
 207:              else
 208:              {
 209:                  result = "Error: Mailbox name '" + name + "' does not exist. ";
 210:                  b = false;
 211:              }
 212:   
 213:              return b;
 214:          }
 215:   
 216:          ////////////////////////////////////////////////////////////////////////////
 217:   
 218:          /// <summary>
 219:          ///
 220:          /// </summary>
 221:          public void CopyMailboxList(List<string> newMailboxList)
 222:          {
 223:              // below: this will copy mailbox list into object
 224:   
 225:              mailboxList.Clear();
 226:   
 227:              foreach (string s in newMailboxList) mailboxList.Add(s);
 228:          }
 229:   
 230:          ////////////////////////////////////////////////////////////////////////////
 231:   
 232:          /// <summary>
 233:          ///
 234:          /// </summary>
 235:          public bool AddRegex(string mailboxName, string regex, out string result)
 236:          {
 237:              bool b;
 238:   
 239:              if (Ia.Cl.Model.Default.IsRegexPatternValid(regex))
 240:              {
 241:                  if (mailboxList.Contains(mailboxName) || mailboxList.Contains(mailboxName.ToLower()))
 242:                  {
 243:                      if (!mailRegexToMailboxList.Contains(regex + " (" + mailboxName + ")"))
 244:                      {
 245:                          mailRegexToMailboxList.Add(regex + " (" + mailboxName + ")");
 246:   
 247:                          result = "Success: Regex '" + regex + "' for mailbox '" + mailboxName + "' is added. ";
 248:                          b = true;
 249:                      }
 250:                      else
 251:                      {
 252:                          result = "Error: Regex '" + regex + "' for mailbox '" + mailboxName + "' already exists. ";
 253:                          b = false;
 254:                      }
 255:                  }
 256:                  else
 257:                  {
 258:                      result = "Error: Mailbox '" + mailboxName + "' is not assigned or does not exist. ";
 259:                      b = false;
 260:                  }
 261:              }
 262:              else
 263:              {
 264:                  result = "Error: Regex pattern '" + regex + "' is invalid. ";
 265:                  b = false;
 266:              }
 267:   
 268:              return b;
 269:          }
 270:   
 271:          ////////////////////////////////////////////////////////////////////////////
 272:   
 273:          /// <summary>
 274:          ///
 275:          /// </summary>
 276:          public bool DeleteRegex(string regex, out string result)
 277:          {
 278:              bool b;
 279:   
 280:              if (mailRegexToMailboxList.Contains(regex))
 281:              {
 282:                  mailRegexToMailboxList.Remove(regex);
 283:   
 284:                  result = "Success: regex '" + regex + "' was removed. ";
 285:                  b = true;
 286:              }
 287:              else
 288:              {
 289:                  result = "Error: regex does not exist. ";
 290:                  b = false;
 291:              }
 292:   
 293:              return b;
 294:          }
 295:   
 296:          ////////////////////////////////////////////////////////////////////////////
 297:          ////////////////////////////////////////////////////////////////////////////
 298:   
 299:          /// <summary>
 300:          ///
 301:          /// </summary>
 302:          public static Mail DeserializeFromString(string serializedObject)
 303:          {
 304:              Mail m;
 305:   
 306:              m = serializedObject.XmlDeserializeFromString<Mail>();
 307:   
 308:              return m;
 309:          }
 310:   
 311:          ////////////////////////////////////////////////////////////////////////////
 312:   
 313:          /// <summary>
 314:          ///
 315:          /// </summary>
 316:          public string SerializeToString()
 317:          {
 318:              StringBuilder sb;
 319:   
 320:              sb = new StringBuilder(10000);
 321:   
 322:              var serializer = new XmlSerializer(typeof(Mail));
 323:   
 324:              using (var writer = XmlWriter.Create(sb))
 325:              {
 326:                  serializer.Serialize(writer, this);
 327:              }
 328:   
 329:              return sb.ToString();
 330:          }
 331:   
 332:          ////////////////////////////////////////////////////////////////////////////
 333:   
 334:          /// <summary>
 335:          ///
 336:          /// </summary>
 337:          public static bool Sorter(Mail m, out Ia.Cl.Model.Result result)
 338:          {
 339:              bool b;
 340:              int numberOfMessagesMoved;
 341:              string regex, destinationMailbox; //, messageId, email;
 342:              List<string> mailboxList, mailboxesToDeleteList, mailboxesToAddList;
 343:              Hashtable mailRegexToMailboxHashtable, /*messageIdToFromEmailHashtable,*/ messageIdToDestinationMailboxHashtable;
 344:              Match match;
 345:              Ia.Cl.Model.Imap imap2;
 346:   
 347:              b = false;
 348:              numberOfMessagesMoved = 0;
 349:              result = new Ia.Cl.Model.Result();
 350:   
 351:              mailboxesToDeleteList = new List<string>(100);
 352:              mailboxesToAddList = new List<string>(100);
 353:              mailRegexToMailboxHashtable = new Hashtable(100);
 354:              messageIdToDestinationMailboxHashtable = new Hashtable(1000);
 355:   
 356:              try
 357:              {
 358:                  imap2 = new Ia.Cl.Model.Imap(m.Host, m.UserName, m.Password, m.EnabledSsl);
 359:                  //imap2 = new Ia.Cl.Model.Imap("imap.*.com", "*@*.com", "*", false);
 360:   
 361:                  imap2.Connect();
 362:   
 363:                  if (imap2.IsConnected)
 364:                  {
 365:                      // below: read all mailboxes
 366:                      mailboxList = imap2.MailboxList();
 367:   
 368:                      // below: determain change if any
 369:                      // below: mailboxes to add:
 370:                      foreach (string s in m.MailboxList) if (!mailboxList.Contains(s)) mailboxesToAddList.Add(s);
 371:   
 372:                      // below: mailboxes to delete:
 373:                      // below: I will disable deletion of mailboxes
 374:                      // foreach (string s in mailboxArrayList) if (!m.MailboxList.Contains(s)) mailboxesToAddArrayList.Add(s);
 375:   
 376:                      // below: create mailboxes
 377:                      foreach (string s in mailboxesToAddList) imap2.CreateMailbox(s);
 378:   
 379:                      // below: delete mailboxes
 380:                      // foreach (string s in mailboxesToDeleteArrayList) imap.DeleteMailbox(s);
 381:   
 382:                      // below: read all mailboxes again
 383:                      mailboxList = imap2.MailboxList();
 384:   
 385:                      // below: assign change to object
 386:                      m.CopyMailboxList(mailboxList);
 387:   
 388:                      // below: start moving
 389:                      foreach (string s in m.MailRegexToMailboxList)
 390:                      {
 391:                          match = Regex.Match(s, @"(.+?) \((.+?)\)");
 392:   
 393:                          if (match.Success)
 394:                          {
 395:                              regex = match.Groups[1].Value;
 396:                              destinationMailbox = match.Groups[2].Value;
 397:   
 398:                              numberOfMessagesMoved += imap2.MoveMessagesFromEmailToMailbox(regex, destinationMailbox);
 399:                          }
 400:                      }
 401:   
 402:                      imap2.Disconnect();
 403:   
 404:                      result.AddSuccess("Number of messages moved: " + numberOfMessagesMoved + ". ");
 405:   
 406:                      b = true;
 407:                  }
 408:                  else
 409:                  {
 410:   
 411:                  }
 412:              }
 413:              catch (Exception ex)
 414:              {
 415:                  b = false;
 416:   
 417:                  result.AddSuccess("Exception: " + ex.ToString());
 418:              }
 419:   
 420:              return b;
 421:          }
 422:   
 423:          ////////////////////////////////////////////////////////////////////////////
 424:          ////////////////////////////////////////////////////////////////////////////
 425:      }
 426:   
 427:      ////////////////////////////////////////////////////////////////////////////
 428:      ////////////////////////////////////////////////////////////////////////////
 429:  }