/// <summary> /// Begins an asynchronous request for a remote server connection using. /// </summary> /// <param name="serverEncryption">Server encryption (SslTls, Unencrypted, StartTls)</param> /// <param name="host">The name or IP address of the remote server.</param> /// <returns>An IAsyncResult that references the asynchronous connection.</returns> public IAsyncResult BeginConnect(MailServerEncryption serverEncryption, string host) { switch (serverEncryption) { case MailServerEncryption.SslTls: { return(_imap.BeginConnectSSL(host)); } case MailServerEncryption.Unencrypted: { return(_imap.BeginConnect(host)); } case MailServerEncryption.StartTls: { var asyncResult = _imap.BeginConnect(host); _imap.StartTLS(); return(asyncResult); } default: { return(_imap.BeginConnect(host)); } } }
/// <summary> /// Connects to IMAP server. /// </summary> /// <param name="serverEncryption">Server encryption (SslTls, Unencrypted, StartTls)</param> /// <param name="host">Target host name or IP address.</param> /// <exception cref="T:Limilabs.Client.ServerException">DNS resolving error, connection error.</exception> /// <exception cref="T:Limilabs.Client.IMAP.ImapResponseException">Initial error response.</exception> public void Connect(MailServerEncryption serverEncryption, string host) { switch (serverEncryption) { case MailServerEncryption.SslTls: { _imap.ConnectSSL(host); break; } case MailServerEncryption.Unencrypted: { _imap.Connect(host); break; } case MailServerEncryption.StartTls: { _imap.Connect(host); _imap.StartTLS(); break; } default: { _imap.Connect(host); break; } } }
/// <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"); }
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); }