C
C#3y ago
Murphy

❔ How to configure email sender to send recovery password(ForgotPassword)

Do you already have code to send emails with sendgrid?
13 Replies
Salvador
Salvador3y ago
Yes, i see videos and the docs But still not working... Do you know @merp_mcderp how to help me?
Murphy
MurphyOP3y ago
Sure, but I need more info. Have you registered an IEmailSender service in your startup code with services.AddScoped<IEmailSender, EmailSender>(); and ensured that the SendEmail(string, string, string) is getting called when you try to request a password?
Salvador
Salvador3y ago
Do you prefere see my screen or prints?
Murphy
MurphyOP3y ago
sure, post whatever code you got. it should help find out what's wrong faster
Salvador
Salvador3y ago
ok public class EmailSender : IEmailSender { private readonly ILogger _logger; public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor, ILogger<EmailSender> logger) { Options = optionsAccessor.Value; _logger = logger; } public AuthMessageSenderOptions Options { get; } //Set with Secret Manager. public async Task SendEmailAsync(string toEmail, string subject, string message) { if (string.IsNullOrEmpty(Options.SendGridKey)) { throw new Exception("Null SendGridKey"); } await Execute(Options.SendGridKey, subject, message, toEmail); } public async Task Execute(string apiKey, string subject, string message, string toEmail) { var client = new SendGridClient(apiKey); var msg = new SendGridMessage() { From = new EmailAddress("joao.rizzo@acelerasaude.com", "Password Recovery"), Subject = subject, PlainTextContent = message, HtmlContent = message }; msg.AddTo(new EmailAddress(toEmail)); msg.SetClickTracking(false, false); var response = await client.SendEmailAsync(msg); _logger.LogInformation(response.IsSuccessStatusCode ? $"Email to {toEmail} queued successfully!" : $"Failure Email to {toEmail}"); } } Email sender class builder.Services.AddTransient<IEmailSender, EmailSender>(); builder.Services.Configure<AuthMessageSenderOptions>(builder.Configuration); public interface IEmailSender { /// <summary> /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> Task SendEmailAsync(string email, string subject, string htmlMessage); }
Salvador
Salvador3y ago
Salvador
Salvador3y ago
Salvador
Salvador3y ago
Yes, i did, i create a account in SendGrid, build a api and generated a key to request
Salvador
Salvador3y ago
Salvador
Salvador3y ago
This is the code when i generated a Scaffold page...
Murphy
MurphyOP3y ago
Nothing's jumping out at me. But I would first start by ensuring that the SendEmailAsync method is getting called and that it is being passed the correct SendGridKey. If it is, then your problem lies with your sendgrid code. I don't know if I can help you with that part but when I was debugging my azure EWS email code, I found it helpful to make a test console project and hard code my all my keys until I was able to send generic emails. That way you can rule out the problem being with your main project (DI, Identity, etc. related). Then I just transplanted that into my actual project (replacing the hardcoded keys with my Options provider of course).
Salvador
Salvador3y ago
Hummm great whatever thanks for helping @merp_mcderp
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?