【问题标题】:Send mail using SendGrid in mvc在 mvc 中使用 SendGrid 发送邮件
【发布时间】:2019-05-12 20:07:04
【问题描述】:

我正在使用 SendGrid 发送确认邮件以确认电子邮件,但是 ApiKey 始终为空

public Task SendAsync(IdentityMessage message)
{

    var ApiKey = Environment.GetEnvironmentVariable("ApiKeyFromSendGrid");
    var client = new SendGridClient(ApiKey);
    var from = new EmailAddress("XXX@gmail.com", "Any Name");
    var subject = "Sending with SendGrid is Fun";
    var to = new EmailAddress(message.Destination, "Any Name");
    var plainTextContent = "and easy to do anywhere, even with C#";
    var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
    var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
    var response = client.SendEmailAsync(msg);

    return response;

}

错误在哪里? enter image description here

【问题讨论】:

  • 表示该key的环境变量不存在。检查密钥是否正确。调试。
  • 如何检查变量是否存在?
  • 为此使用环境变量似乎很奇怪。为什么不将值存储在 .config 文件中?

标签: c# asp.net-mvc sendgrid


【解决方案1】:

错误是在环境变量的恢复中:

Environment.GetEnvironmentVariable("ApiKeyFromSendGrid");

ApiKeyFromSendGrid 存在吗?

看看这些问题和答案是否能帮助您正确检索环境变量:

How do I get and set Environment variables in C#?

Environment.GetEnvironmentVariable won't find variable value

【讨论】:

  • @OmarSeleim 好的,但图像显示了网站上显示的密钥。你把它设置为环境变量了吗?在您的系统中是否存在?
  • API KEYS 以“SG”开头。请看我上面的回答。
  • 我将用作 var ApiKey = Environment.GetEnvironmentVariable("SG.Tob9Zs...-....");或者像 var ApiKey = ("SG.Tob9Zs...-...."); ??
【解决方案2】:

您为 API 密钥使用了错误的值。您正在使用 API 密钥 ID。如果您没有将原始键值存储在某处,您可能需要创建一个新键。

见SendGrid API Key not working. "The provided authorization grant is invalid, expired or revoked"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2021-10-23
    相关资源
    最近更新 更多