public static async Task <SmtpClient> CreateConnectedSmtpClientAsync([NotNull] this RelayRule forwardRule, CancellationToken token) { if (forwardRule == null) { throw new ArgumentNullException(nameof(forwardRule)); } var client = new SmtpClient(); await client.ConnectAsync( forwardRule.SmtpServer, forwardRule.SmtpPort, forwardRule.SmtpUseSSL?SecureSocketOptions.Auto : SecureSocketOptions.None, token); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); if (!string.IsNullOrWhiteSpace(forwardRule.SmtpPassword) && !string.IsNullOrWhiteSpace(forwardRule.SmtpUsername)) { // Note: only needed if the SMTP server requires authentication await client.AuthenticateAsync(forwardRule.SmtpUsername, forwardRule.SmtpPassword, token); } return(client); }
public static void PopulateServerFromUri([NotNull] this RelayRule rule, string smtpServer) { if (rule == null) { throw new ArgumentNullException(nameof(rule)); } var uri = new Uri("smtp://" + smtpServer); rule.SmtpServer = uri.DnsSafeHost; rule.SmtpPort = uri.IsDefaultPort ? 25 : uri.Port; }