diff --git a/RMuseum/appsettings.json b/RMuseum/appsettings.json index 904e1e7c..44cbecb3 100644 --- a/RMuseum/appsettings.json +++ b/RMuseum/appsettings.json @@ -21,8 +21,9 @@ }, "SmptConfig": { "Server": "smtp.gmail.com", - "Port": "465", + "Port": "587", "UseSsl": "true", + "UseTls": "true", "Username": "test@gmail.com", "Password": "password", "From": "test@gmail.com" diff --git a/RSecurityBackend/Models/Mail/SmptConfig.cs b/RSecurityBackend/Models/Mail/SmptConfig.cs index f86efa51..57e9fec7 100644 --- a/RSecurityBackend/Models/Mail/SmptConfig.cs +++ b/RSecurityBackend/Models/Mail/SmptConfig.cs @@ -30,5 +30,10 @@ /// use ssl /// public bool useSsl { get; set; } + + /// + /// use tls + /// + public bool useTls { get; set; } } } diff --git a/RSecurityBackend/RSecurityBackend.xml b/RSecurityBackend/RSecurityBackend.xml index 139983d2..d203504d 100644 --- a/RSecurityBackend/RSecurityBackend.xml +++ b/RSecurityBackend/RSecurityBackend.xml @@ -1939,6 +1939,11 @@ use ssl + + + use tls + + Notifaction Status diff --git a/RSecurityBackend/Services/Implementation/MailKitEmailSender.cs b/RSecurityBackend/Services/Implementation/MailKitEmailSender.cs index 07929f5a..1b0625ef 100644 --- a/RSecurityBackend/Services/Implementation/MailKitEmailSender.cs +++ b/RSecurityBackend/Services/Implementation/MailKitEmailSender.cs @@ -1,4 +1,5 @@ using MailKit.Net.Smtp; +using MailKit.Security; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Configuration; using MimeKit; @@ -53,7 +54,8 @@ namespace RSecurityBackend.Services.Implementation useSsl = bool.Parse($"{Configuration.GetSection("SmptConfig")["UseSsl"]}"), smtpUsername = $"{ Configuration.GetSection("SmptConfig")["Username"] }", smtpPassword = $"{Configuration.GetSection("SmptConfig")["Password"]}", - from = $"{ Configuration.GetSection("SmptConfig")["From"] }" + from = $"{ Configuration.GetSection("SmptConfig")["From"] }", + useTls = bool.Parse($"{Configuration.GetSection("SmptConfig")["useTls"]}"), }; } @@ -69,9 +71,9 @@ namespace RSecurityBackend.Services.Implementation /// /// /// - public Task SendEmailAsync(string email, string subject, string message) + public async Task SendEmailAsync(string email, string subject, string message) { - return Execute(Options, subject, message, email); + await Execute(Options, subject, message, email); } /// @@ -82,7 +84,7 @@ namespace RSecurityBackend.Services.Implementation /// /// /// - public Task Execute(SmptConfig options, string subject, string message, string email) + public async Task Execute(SmptConfig options, string subject, string message, string email) { var mimeMessage = new MimeMessage(); mimeMessage.From.Add(new MailboxAddress(options.from, options.from)); @@ -95,21 +97,19 @@ namespace RSecurityBackend.Services.Implementation }; - SmtpClient client = new SmtpClient(); + using (SmtpClient client = new SmtpClient()) + { + // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) + client.ServerCertificateValidationCallback = (s, c, h, e) => true; - // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) - client.ServerCertificateValidationCallback = (s, c, h, e) => true; + await client.ConnectAsync(options.server, options.port, options.useTls ? SecureSocketOptions.StartTls : options.useSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None ); - client.Connect(options.server, options.port, options.useSsl); + // Note: only needed if the SMTP server requires authentication + await client.AuthenticateAsync(options.smtpUsername, options.smtpPassword); - // Note: only needed if the SMTP server requires authentication - client.Authenticate(options.smtpUsername, options.smtpPassword); - - client.Send(mimeMessage); - client.Disconnect(true); - client.Dispose(); - - return Task.FromResult(0); + await client.SendAsync(mimeMessage); + await client.DisconnectAsync(true); + } } } }