0
这是我正在使用的代码。出于某种原因,它给出了一个错误:'未经授权'。任何想法,为什么这可能会发生。 Api键已正确配置。SendGrid V3 api与C#无法发送邮件
String apiKey = Environment.GetEnvironmentVariable("SG.7cSY-INMQnCwIzmonlgZvA.zNtNDycx......", EnvironmentVariableTarget.User);
dynamic sg = new SendGrid.SendGridAPIClient(apiKey, "https://api.sendgrid.com");
Email from = new Email("[email protected]");
String subject = "Hello World from the SendGrid CSharp Library";
Email to = new Email("[email protected]");
Content content = new Content("text/plain", "Textual content");
Mail mail = new Mail(from, subject, to, content);
//Email email = new Email("[email protected]");
//mail.Personalization[0].AddTo(email);
String ret = mail.Get();
string requestBody = ret;
Console.WriteLine(ret);
try
{
dynamic response = sg.client.mail.send.beta.post(requestBody: requestBody);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
}
catch (Exception ex) {
Console.WriteLine("SendGrid Error: {0}",ex.Message);
}
好了,如果我指正我错了:'EnvironmentVariable'和'AppSettings'非常相似。但是,通过'EnvironmentVariable',您可以为不同的用户存储不同的API-Key(不同的配置)。 –
我对AppSettings不熟悉。对于EnvironmentVariable,我的理解是,您可以从当前用户空间或系统范围获取值。在上面的代码中,我假设你在用户空间中设置了环境变量。 –