private static string GetFormattedTextError(Exception ex, MailServerType mailServerType, bool timeoutFlag = true) { var headerText = string.Empty; var errorExplain = string.Empty; switch (mailServerType) { case MailServerType.Imap: case MailServerType.ImapOAuth: headerText = MailApiResource.ImapResponse; if (timeoutFlag) { errorExplain = MailApiResource.ImapConnectionTimeoutError; } break; case MailServerType.Pop3: headerText = MailApiResource.Pop3Response; if (timeoutFlag) { errorExplain = MailApiResource.Pop3ConnectionTimeoutError; } break; case MailServerType.Smtp: headerText = MailApiResource.SmtRresponse; if (timeoutFlag) { errorExplain = MailApiResource.SmtpConnectionTimeoutError; } break; } return(GetFormattedTextError(ex, errorExplain, headerText)); }
/// <summary> /// Starts mail controller. /// </summary> /// <param name="serverType"></param> /// <param name="serverEncryption"></param> /// <param name="host"></param> /// <param name="user"></param> /// <param name="password"></param> public void Start(MailServerType serverType, MailServerEncryption serverEncryption, string host, string user, string password) { // Prevents possible issues when Start was called more then once. if (MainLoop != null && !MainLoop.IsCompleted) { Logger.Error("Unexpected engine start detected."); return; // At the moment it logs error message instead of throwing it. // throw new Exception("Unexpected engine start detected."); } // Save all settings to local variable. _host = !string.IsNullOrWhiteSpace(host) ? host : throw new ArgumentException("Invalid host."); _user = !string.IsNullOrWhiteSpace(user) ? user : throw new ArgumentException("Invalid user."); _password = !string.IsNullOrWhiteSpace(password) ? password : throw new ArgumentException("Invalid password."); _serverType = serverType; _serverEncryption = serverEncryption; MainLoop = Task.Factory.StartNew(() => StartMailCheckAsyncLoop(_cancelSource.Token), _cancelSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); MainLoop.ContinueWith(e => { Logger.Error(e.Exception, "StartNewOnFaulted"); }, TaskContinuationOptions.OnlyOnFaulted); MainLoop.ContinueWith(e => { Logger.Trace(e.Exception, "StartNewOnCanceled"); }, TaskContinuationOptions.OnlyOnCanceled); Logger.Trace($"Started"); }
public IMailClient Build(MailServerType serverType) { switch (serverType) { case MailServerType.Imap: return(new ImapClient()); case MailServerType.Pop3: return(new Pop3Client()); default: return(new ImapClient()); } }
private MailHeader TestConnectionAndAwaitAnyFirstMailHeader(MailServerType serverType, MailServerEncryption serverEncryption) { MailHeader mailHeader = null; var mailController = CreateMailController(); try { mailController.Start(MailServerType.Imap, MailServerEncryption.SslTls, _host, _user, _password); mailHeader = mailController.MailHeaderStream.Take(1).Timeout(DateTime.Now.AddSeconds(15)).Wait(); } catch (Exception e) { Debug.WriteLine(e); mailHeader = null; } finally { mailController.Dispose(); } Debug.WriteLine($"[{mailHeader.Date}] {mailHeader.MailFrom} | Subject: {mailHeader.Subject}"); return(mailHeader); }
public static void LoadGolbalConfig() { try { gMaxLogin_Attempt = Cvt.ToInt32(ConfigurationManager.AppSettings["MaxLogin_Attempt"]); gLock_Minutes_After_Max_Login_Attempt = Cvt.ToInt32(ConfigurationManager.AppSettings["Lock_Minutes_After_Max_Login_Attempt"]); gMust_Login = Cvt.ToBoolean(ConfigurationManager.AppSettings["Must_Login"]); gDefault_Language = ConfigurationManager.AppSettings["Default_Language"]; gHome_Page_Title = ConfigurationManager.AppSettings["Home_Page_Title"]; gLogo_Picture = ConfigurationManager.AppSettings["Logo_Picture"]; gLADPUrl = ConfigurationManager.AppSettings["LADPUrl"]; gLADPUser = ConfigurationManager.AppSettings["LADPUser"]; gLADPPwd = ConfigurationManager.AppSettings["LADPPwd"]; string sMailServerType = ConfigurationManager.AppSettings["MailServerType"]; gMailServerAddress = ConfigurationManager.AppSettings["MailServerAddress"]; gMailServerUser = ConfigurationManager.AppSettings["MailServerUser"]; gMailServerPassword = ConfigurationManager.AppSettings["MailServerPassword"]; DataTable tbl = Common.DAL.GetPreferencesSetttings(); foreach (DataRow row in tbl.Rows) { string sKey = Cvt.ToString(row["key"]).Trim(), sValue = Cvt.ToString(row["value"]).Trim(); if (sKey.Length > 0 && sValue.Length > 0) { if (sKey.Equals("Max_Login_Attempt", StringComparison.OrdinalIgnoreCase)) { gMaxLogin_Attempt = Cvt.ToInt32(sValue); } else if (sKey.Equals("Lock_Minute_After_Max_Login_Attempt", StringComparison.OrdinalIgnoreCase)) { gLock_Minutes_After_Max_Login_Attempt = Cvt.ToInt32(sValue); } else if (sKey.Equals("Must_Login", StringComparison.OrdinalIgnoreCase)) { gMust_Login = Cvt.ToBoolean(sValue); } else if (sKey.Equals("Default_Language", StringComparison.OrdinalIgnoreCase)) { gDefault_Language = sValue; } else if (sKey.Equals("Home_Page_Title", StringComparison.OrdinalIgnoreCase)) { gDefault_Language = sValue; } else if (sKey.Equals("Logo_Picture", StringComparison.OrdinalIgnoreCase)) { gLogo_Picture = sValue; } else if (sKey.Equals("LADPUrl", StringComparison.OrdinalIgnoreCase)) { gLADPUrl = sValue; } else if (sKey.Equals("LADPUser", StringComparison.OrdinalIgnoreCase)) { gLADPUser = sValue; } else if (sKey.Equals("LADPPwd", StringComparison.OrdinalIgnoreCase)) { gLADPPwd = sValue; } else if (sKey.Equals("MailServerType", StringComparison.OrdinalIgnoreCase)) { sMailServerType = sValue; } else if (sKey.Equals("MailServerAddress", StringComparison.OrdinalIgnoreCase)) { gMailServerAddress = sValue; } else if (sKey.Equals("MailServerUser", StringComparison.OrdinalIgnoreCase)) { gMailServerUser = sValue; } else if (sKey.Equals("MailServerPassword", StringComparison.OrdinalIgnoreCase)) { gMailServerPassword = sValue; } } } if (!string.IsNullOrEmpty(sMailServerType)) { gMailServerType = (MailServerType)Enum.Parse(typeof(MailServerType), sMailServerType, true); } if (!string.IsNullOrEmpty(gLADPPwd)) { gLADPPwd = Common.DecryptDES(gLADPPwd, rgbIV, rgbKey); } if (!string.IsNullOrEmpty(gMailServerPassword)) { gMailServerPassword = Common.DecryptDES(gMailServerPassword, rgbIV, rgbKey); } } catch (Exception) { } }