asp.netcore3.1无法使用sendgrid发送邮件。
创始人
2024-09-17 10:01:08
0
  1. 检查SendGrid API Key是否正确配置,确保API Key可以访问SendGrid服务。

  2. 确认SendGrid API Key已在应用程序中配置。

  3. 确认SendGrid客户端已被注入,并检查是否已成功完成注入。

  4. 检查邮件发送地址是否正确。可以将地址更改为另一个有效邮件地址,并尝试重新发送邮件。

以下是一些可能的SendGrid配置示例:

appsettings.json:

"SendGrid": {
    "ApiKey": "xxx",
    "FromName": "xxx",
    "FromEmail": "xxx"
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    // Add SendGrid
    services.AddTransient((x =>
        new SendGridClient(Configuration.GetValue("SendGrid:ApiKey"))));
    services.AddTransient();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    // Add SendGrid
    var sendGridOptions = Configuration.GetSection("SendGrid").Get();
    if (sendGridOptions != null)
    {
        app.UseSendGridEmail(new SendGridEmailOptions
        {
            ApiKey = sendGridOptions.ApiKey,
            FromName = sendGridOptions.FromName,
            FromEmail = sendGridOptions.FromEmail
        });
    }
}

SendEmailService.cs:

public interface ISendEmailService
{
    Task SendEmailAsync(EmailMessage emailMessage);
}

public class SendEmailService : ISendEmailService
{
    private readonly ISendGridClient _sendGridClient;

    public SendEmailService(ISendGridClient sendGridClient)
    {
        _sendGridClient = sendGridClient;
    }

    public async Task SendEmailAsync(EmailMessage emailMessage)
    {
        var from = new EmailAddress(emailMessage.From);
        var subject = emailMessage.Subject;
        var to = new EmailAddress(emailMessage.To);
        var plainTextContent = emailMessage.PlainTextContent;
        var htmlContent = emailMessage.HtmlContent;

        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        var response = await _sendGridClient.SendEmailAsync(msg);
    }
}

相关内容

热门资讯

透视肯定(wepoke确实有挂... 透视肯定(wepoke确实有挂)fishpoker大菠萝外挂(详细辅助透明教程)原本有挂1)wepo...
微扑克ai机器人!wepoke... 微扑克ai机器人!wepoke软件透明挂,(wepOke)从来存在有挂(详细透视wpk教程)是一款可...
透视智能ai(wepokeai... 透视智能ai(wepokeai机器人)微扑克有挂(详细辅助扑克教程)都是是真的有挂,您好,wepok...
wepoke一定有挂!wepo... wepoke一定有挂!wepoke辅助机器人,(AAPoKER)一直有挂(详细透视辅助教程)科技教程...
透视好友房(wepoke真的有... 透视好友房(wepoke真的有挂)德扑ai智能机器人线上代打(详细辅助科技教程)好像是有挂是一款可以...
透视代打(微扑克游戏辅助器)w... 透视代打(微扑克游戏辅助器)wpk ai(详细辅助力荐教程)真是真的是有挂是一款可以让一直输的玩家,...
wepoke辅助技巧!德州之星... wepoke辅助技巧!德州之星辅助透视,(aapokEr)果然真的是有挂(详细辅助实用技巧);《WP...
透视模拟器(WePoKe透明挂... 透视模拟器(WePoKe透明挂)微扑克辅助钻石(详细辅助2025新版总结)总是是有挂1)WePoKe...
微扑克全自动机器人!wepok... 微扑克全自动机器人!wepoke ai,(wEPOKE)本然是有挂(详细透视插件教程)1、点击下载安...
透视透视挂(aapoker辅助... 透视透视挂(aapoker辅助工具存在)aapoker软件有猫腻吗(详细辅助wpk教程)一贯是有挂a...