IdentityServer4.Admin icon indicating copy to clipboard operation
IdentityServer4.Admin copied to clipboard

We need SMTP upgraded support

Open egbertn opened this issue 4 years ago • 0 comments

Is your feature request related to a problem? Please describe. No

Describe the solution you'd like Upgrade SMTP component support, use Mailkit.

Describe alternatives you've considered // Copyright (c) Jan Škoruba. All Rights Reserved. // Licensed under the Apache License, Version 2.0.

using System; using MailKit.Net.Smtp; using MailKit.Security; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Logging; using Skoruba.Duende.IdentityServer.Shared.Configuration.Configuration.Email; using MimeKit; using System.Net;

namespace Skoruba.Duende.IdentityServer.Shared.Configuration.Email { public class SmtpEmailSender : IEmailSender { private readonly ILogger<SmtpEmailSender> _logger; private readonly SmtpConfiguration _configuration; private readonly SmtpClient _client;

    public SmtpEmailSender(ILogger<SmtpEmailSender> logger, SmtpConfiguration configuration)
    {
        _logger = logger;
        _configuration = configuration;
        _client = new SmtpClient
        {
            CheckCertificateRevocation = false,
        };

    }

    public async Task SendEmailAsync(string email, string subject, string htmlMessage)
    {
        await _client.ConnectAsync(_configuration.Host, _configuration.Port, SecureSocketOptions.StartTls);
        //WARNING: Less secure app access must be TURNED ON
        //https://myaccount.google.com/u/3/lesssecureapps

        await _client.AuthenticateAsync(new NetworkCredential(_configuration.Login, _configuration.Password));

        _logger.LogInformation($"Sending email: {email}, subject: {subject}, message: {htmlMessage}");
        try
        {
            var mail = new MimeMessage
            {
                Subject = subject,
                Body = new TextPart(MimeKit.Text.TextFormat.Html)
                {
                    Text = htmlMessage
                }
            };
            var from = string.IsNullOrEmpty(_configuration.From) ? _configuration.Login : _configuration.From;
            mail.From.Add(new MailboxAddress(from, from));
            mail.To.Add(MailboxAddress.Parse(email));

            await _client.SendAsync(mail);
            await _client.DisconnectAsync(true);
            _logger.LogInformation($"Email: {email}, subject: {subject}, message: {htmlMessage} successfully sent");
        }
        catch (Exception ex)
        {
            _logger.LogError($"Exception {ex} during sending email: {email}, subject: {subject}");
            throw;
        }
    }
}

}

Additional context Add any other context or screenshots about the feature request here.

egbertn avatar Jun 01 '21 17:06 egbertn