简体   繁体   English

Sendgrid 不发送确认 email asp.net core 3.1

[英]Sendgrid does not send confirmation email asp.net core 3.1

I'm using this tutorial for account confirmation email but I don't receive any email or any error here is the code:我正在使用本教程进行帐户确认 email 但我没有收到任何 email 或任何错误这里是代码:

AuthMessageSenderOptions.cs AuthMessageSenderOptions.cs

public class AuthMessageSenderOptions
{
    public string SendGridUser { get; set; }
    public string SendGridKey { get; set; }
}

appSettings.json appSettings.json

"AuthMessageSenderOptions": {
    "SendGridUser": "MyUserName",
    "SendGridKey": "SG.Xa................ MyKey"
},

EmailSender.cs电子邮件发件人.cs

public class EmailSender : IEmailSender
{
    public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
    {
        Options = optionsAccessor.Value;
    }

    public AuthMessageSenderOptions Options { get; } //set only via Secret Manager

    public Task SendEmailAsync(string email, string subject, string message)
    {
        return Execute(Options.SendGridKey, subject, message, email);
    }

    public Task Execute(string apiKey, string subject, string message, string email)
    {
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage()
        {
            From = new EmailAddress("info@reloadrs.com", Options.SendGridUser),
            Subject = subject,
            PlainTextContent = message,
            HtmlContent = message
        };
        msg.AddTo(new EmailAddress(email));

        // Disable click tracking.
        // See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
        msg.SetClickTracking(false, false);

        return client.SendEmailAsync(msg);
    }
}

Register.cs注册.cs

var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
    _logger.LogInformation("User created a new account with password.");
    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
    var confirmationLink = Url.Page(nameof(ConfirmEmail), "Account",
        new { code, email = user.Email }, Request.Scheme);
    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
        $"Please confirm your account by" +
        $" <a href='{HtmlEncoder.Default.Encode(confirmationLink)}'>clicking here</a>.");
}

I tried also to try it after publish not only locally but still the same在不仅在本地发布而且仍然相同之后,我也尝试过尝试

You can get the result of sending the email and also the error message:您可以获得发送 email 的结果以及错误消息:

var response = await client.SendEmailAsync(msg);
if (response.StatusCode != HttpStatusCode.OK
    && response.StatusCode != HttpStatusCode.Accepted)
{
    var errorMessage = response.Body.ReadAsStringAsync().Result;
    throw new Exception($"Failed to send mail to {to}, status code {response.StatusCode}, {errorMessage}");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 3.1 创建服务发送email确认 - ASP.NET Core 3.1 create a service to send email confirmation 为什么Sendgrid在asp core 2.1的应用程序中不发送任何确认电子邮件? - why Sendgrid does not send any confirmation email in my app in asp core 2.1? 无法在 ASP.NET Core 3.1 MVC 中发送电子邮件以进行确认 - Unable to send email for conformation in ASP.NET Core 3.1 MVC 在 ASP.NET 核心 Web Z72664DC0959F3B0C0470479 中注册后,将 email 确认令牌作为可点击链接发送给用户 - Send email confirmation token as clickable link to user upon registration in ASP.NET Core Web Api Asp.net核心电子邮件确认有时会说InvalidToken - Asp.net Core Email confirmation sometimes says InvalidToken 如何更改 ASP.NET 核心标识 email 确认消息 - How to change the ASP.NET Core Identity email confirmation message ASP.NET Core Identity email React 路由确认 URL - ASP.NET Core Identity email confirmation URL for React routes 我如何在asp.net mvc中发送确认电子邮件 - How can I send a Confirmation Email in asp.net mvc ASP.NET Identity 2.2.1 - 发送包含代码的确认电子邮件,而不是链接 - ASP.NET Identity 2.2.1 - send confirmation email with code, not with link ASP.Net MVC 4 SimpleMembership“重新发送确认电子邮件” - ASP.Net MVC 4 SimpleMembership “Re-Send Confirmation Email”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM