如何解决更新 SendGrid SMTP API C# ASP.NET
由于 SendGrid 使用 API 密钥而不是用户名和密码更新了 SMTP 服务,因此我在更新代码时遇到了问题。
我承认 sendgrid 库需要更新,但是在我实现从网站更改代码后,我收到了带有下划线的发送电子邮件的错误。
我不确定是否需要在 webconfig 中使用名称“apikey”和“apikeycode”更新 mailaccount 和 mailpassword
图书馆 .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);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。