使用带有C#" Web"的SendGrid包。如示例所示,未定义

时间:2017-10-20 13:44:14

标签: sendgrid

我使用的代码就像许多例子一样 但是" Web"是一个未定义的类型。 即使是ReSharper也无法告诉我在哪里可以找到它。 我是否需要使用其他参考文献或者使用“网络”。在v 9.9.0中重命名了吗?

var myMessage = new SendGridMessage();
            myMessage.AddTo(message.Destination);
            myMessage.From = new EmailAddress("somebody@xyz.com", "Fff  
Lll");
            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);

1 个答案:

答案 0 :(得分:1)

您正在关注现已过时的V2 api文档。您可以使用V3 api SDK。示例代码如下所示

            var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress("test@example.com", "Example User");
            var subject = "Sending with SendGrid is Fun";
            var to = new EmailAddress("test@example.com", "Example User");
            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 = await client.SendEmailAsync(msg);

您需要引用以下命名空间

using SendGrid;
using SendGrid.Helpers.Mail;

您也可以在没有邮件助手类的情况下发送邮件。请按照以下链接获取更多用法和演示

https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md

Nuget链接:

https://www.nuget.org/packages/Sendgrid/