public static void TestGoogleSmtpLoginViaOAuth2(string account, string refresh_token) { var authorizatior = new GoogleOAuth2Authorization(); var granted_access = authorizatior.RequestAccessToken(refresh_token); if (granted_access == null) { return; } var smtp = MailClientBuilder.Smtp(); smtp.ConnectSsl("smtp.googlemail.com", 465); smtp.Authenticate(account, granted_access.AccessToken, SaslMechanism.OAuth2); //Do some work... smtp.Disconnect(); }
static public bool TestSmtp(MailServerSettings settings) { var smtp = MailClientBuilder.Smtp(); string s_result = String.Empty; try { IAsyncResult async_res; if (settings.EncryptionType == EncryptionType.None || settings.EncryptionType == EncryptionType.StartTLS) { async_res = smtp.BeginConnect(settings.Url, settings.Port, null); if (!async_res.AsyncWaitHandle.WaitOne(WAIT_TIMEOUT)) { throw new SmtpConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR); } if (settings.AuthenticationType != SaslMechanism.None || settings.EncryptionType == EncryptionType.StartTLS) { smtp.SendEhloHelo(); } if (settings.EncryptionType == EncryptionType.StartTLS) { smtp.StartTLS(settings.Url); } if (settings.AuthenticationType != SaslMechanism.None) { s_result = smtp.Authenticate(settings.AccountName, settings.AccountPass, settings.AuthenticationType); } } else { async_res = smtp.BeginConnectSsl(settings.Url, settings.Port, null); if (!async_res.AsyncWaitHandle.WaitOne(WAIT_TIMEOUT)) { throw new SmtpConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR); } if (settings.AuthenticationType != SaslMechanism.None) { s_result = smtp.Authenticate(settings.AccountName, settings.AccountPass, settings.AuthenticationType); } } if (settings.AuthenticationType != SaslMechanism.None && !s_result.StartsWith("+")) { throw new SmtpConnectionException(s_result); } return(true); } finally { if (smtp.IsConnected) { smtp.Disconnect(); } } }