private static IHealthChecksBuilder AddImapHealthCheck(this IHealthChecksBuilder builder, IConfiguration configuration) { var imapSettings = ConfigurationHandler.GetImapSettings(configuration); if (string.IsNullOrWhiteSpace(imapSettings.Host)) { logger.Info("IMAP Check: No valid Host found. Skipping check."); return(builder); } return(builder.AddImapHealthCheck(setup => { setup.Host = imapSettings.Host; setup.Port = imapSettings.Port.GetValueOrDefault(); setup.AllowInvalidRemoteCertificates = true; setup.ConnectionType = imapSettings.EnableSsl.Value == true ? ImapConnectionType.SSL_TLS : ImapConnectionType.AUTO; setup.LoginWith(imapSettings.Username, imapSettings.Password); })); }
public ImapSettings(MailSettings smtpSettings) #endif { var log = LogManager.GetLogger(typeof(ImapSettings)); #if NETFRAMEWORK var prefix = "IMAP:"; var server = ConfigurationManager.AppSettings[$"{prefix}host"]; var port = ConfigurationManager.AppSettings[$"{prefix}port"]; var username = ConfigurationManager.AppSettings[$"{prefix}username"]; var password = ConfigurationManager.AppSettings[$"{prefix}password"]; var useSSL = ConfigurationManager.AppSettings[$"{prefix}enableSSL"]; if (string.IsNullOrWhiteSpace(server)) { throw new ArgumentNullException(nameof(smtpSettings), $"Missing setting: [{prefix}host]"); } if (string.IsNullOrWhiteSpace(port)) { throw new ArgumentNullException(nameof(smtpSettings), $"Missing setting: [{prefix}port]"); } Server = server; if (int.TryParse(port, out var p)) { Port = p; } else { throw new ArgumentException("MailSettings", $"Invalid setting: [{prefix}port]"); } Username = username; if (string.IsNullOrWhiteSpace(Username)) { Username = smtpSettings?.Smtp?.Network?.UserName; if (string.IsNullOrWhiteSpace(Username)) { throw new ArgumentNullException(nameof(smtpSettings), $"Missing setting: [{prefix}username]"); } log.Warn($"Missing or incorrect setting: [{prefix}username]. Will try to use the username you set in your SMTP Settings."); } Password = password; if (string.IsNullOrWhiteSpace(Password)) { Password = smtpSettings?.Smtp?.Network?.Password; if (string.IsNullOrWhiteSpace(Password)) { throw new ArgumentNullException(nameof(smtpSettings), $"Missing setting: [{prefix}password]"); } log.Warn($"Missing or incorrect setting: [{prefix}password]. Will try to use the password you set in your SMTP Settings."); } if (bool.TryParse(useSSL, out var s)) { UseSSL = s; } else { log.Warn($"Missing or incorrect setting: [{prefix}enableSSL]. Will go with 'false'."); } #else const string prefix = "IMAP:"; ImapConfiguration = ConfigurationHandler.GetImapSettings(); if (string.IsNullOrWhiteSpace(ImapConfiguration.Host)) { throw new ArgumentNullException(nameof(ImapSettings), $"Missing setting: [{prefix}host]"); } if (ImapConfiguration.Port == null) { throw new ArgumentNullException(nameof(ImapSettings), $"Missing setting: [{prefix}port]"); } if (string.IsNullOrWhiteSpace(ImapConfiguration.Username)) { ImapConfiguration.Username = smtpSettings?.Smtp?.Network?.UserName; if (string.IsNullOrWhiteSpace(ImapConfiguration.Username)) { throw new ArgumentNullException(nameof(ImapSettings), $"Missing setting: [{prefix}username]"); } log.Warn($"Missing or incorrect setting: [{prefix}username]. Will try to use the username you set in your SMTP Settings."); } if (string.IsNullOrWhiteSpace(ImapConfiguration.Password)) { ImapConfiguration.Password = smtpSettings?.Smtp?.Network?.Password; if (string.IsNullOrWhiteSpace(ImapConfiguration.Password)) { throw new ArgumentNullException(nameof(ImapSettings), $"Missing setting: [{prefix}password]"); } log.Warn($"Missing or incorrect setting: [{prefix}password]. Will try to use the password you set in your SMTP Settings."); } if (ImapConfiguration.EnableSsl == null) { log.Warn($"Missing or incorrect setting: [{prefix}enableSSL]. Will go with 'false'."); } #endif }