0

由于 SendGrid 使用 API 密钥而不是用户名和密码更新了 SMTP 服务,因此我在更新代码时遇到了问题。

我承认 sendgrid 库需要更新,但在我实施从网站更改代码后,发送电子邮件时出现错误。<等待transportWeb.DeliverAsync(myMessage);>

我不确定 mailaccount 和 mailpassword 是否需要在 webconfig 中使用名称“apikey”和“apikeycode”进行更新

库 .NETFRAMEWORK 4.6、SENDGRID v9.22、SENDGRID.SMTPAPI V1.39

任何帮助将不胜感激。

旧代码:

 public class EmailService : IIdentityMessageService
{
    public async Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        //return Task.FromResult(0);
        await configSendGridasync(message);
    }

    private async Task configSendGridasync(IdentityMessage Message)
    {
        var myMessage = new SendGridMessage();
        myMessage.AddTo(Message.Destination);
        myMessage.From = new MailAddress(
                            "test@domain.com", "Customer Services");
        myMessage.Subject = Message.Subject;
        myMessage.Text = Message.Body;
        myMessage.Html = Message.Body;

        var credentials = new NetworkCredential(
                   ConfigurationManager.AppSettings["mailAccount"],
                   ConfigurationManager.AppSettings["mailPassword"]
                   );
        // Create a Web transport for sending email.
        var transportWeb = new Web(credentials);

        // Send the email.
        if (transportWeb != null)
        {
            await transportWeb.DeliverAsync(myMessage);
        }
        else
        {
            Trace.TraceError("Failed to create web transport");
            await Task.FromResult(0);
        }
    }
}

网络配置:

<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="mailAccount" value="USER" />
<add key="mailPassword" value="ABCD" />

更新代码

public async Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        //return Task.FromResult(0);
        await configSendGridasync(message);
    }

    private async Task configSendGridasync(IdentityMessage Message)
    {
        var apiKey = Environment.GetEnvironmentVariable("SENDGRID_KEY");
        var client = new SendGridClient(apiKey);
        var myMessage = new SendGridMessage();
        myMessage.AddTo(Message.Destination);
        myMessage.From = new EmailAddress(
                            "Test@Domain.com", "Customer Services");
        myMessage.Subject = Message.Subject;
        myMessage.PlainTextContent = Message.Body;
        myMessage.HtmlContent = Message.Body;

        //var credentials = new NetworkCredential(
        //           ConfigurationManager.AppSettings["mailAccount"],
        //           ConfigurationManager.AppSettings["mailPassword"]
        //           );
        // Create a Web transport for sending email.
        //var transportWeb = new Web(credentials);
        var transportWeb = await client.SendEmailAsync(myMessage).ConfigureAwait(false);
        // Send the email.
        if (transportWeb != null)
        {
            
            await transportWeb.DeliverAsync(myMessage);
        }
        else
        {
            Trace.TraceError("Failed to create web transport");
            await Task.FromResult(0);
        }
    }
}
4

0 回答 0