fixed smtp email sender requiring username/password, not requiring TLS on Ssl=true

This commit is contained in:
Mia Rose Winter 2024-02-07 20:51:15 +01:00
parent 2acc2916e4
commit 5e49ae3c16
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -1,4 +1,5 @@
using MailKit.Net.Smtp; using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -43,8 +44,11 @@ public class SmtpEmailSender(IOptions<SmtpConfiguration> config, ILogger<SmtpEma
message.Body = builder.ToMessageBody(); message.Body = builder.ToMessageBody();
using var client = new SmtpClient(); using var client = new SmtpClient();
await client.ConnectAsync(Configuration.Host, Configuration.Port); await client.ConnectAsync(Configuration.Host, Configuration.Port,
Configuration.Ssl ? SecureSocketOptions.StartTls : SecureSocketOptions.None);
if (!string.IsNullOrWhiteSpace(Configuration.Username)) {
await client.AuthenticateAsync(Configuration.Username, Configuration.Password); await client.AuthenticateAsync(Configuration.Username, Configuration.Password);
}
await client.SendAsync(message); await client.SendAsync(message);
await client.DisconnectAsync(true); await client.DisconnectAsync(true);
} catch (Exception ex) { } catch (Exception ex) {