问题描述
由于 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(
"[email protected]","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(
"[email protected]","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 (将#修改为@)