2017-10-20 35 views
1

我使用看起来就像这样的例子很多代码 但是,“网络”是一个未定义的类型。 即使ReSharper不能告诉我在哪里可以找到它。 我是否需要使用另一个参考或已在'v9.9.0'中重命名'Web'?使用SendGrid包用C#“网络”作为例子所示,是未定义

var myMessage = new SendGridMessage(); 
      myMessage.AddTo(message.Destination); 
      myMessage.From = new EmailAddress("[email protected]", "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); 
+0

您使用的是旧版本的SDK。在9.9.0中,您必须使用V3 api和'SendGrid.Helpers.Mail,SendGrid.' namespaces – Niladri

回答

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("[email protected]", "Example User"); 
      var subject = "Sending with SendGrid is Fun"; 
      var to = new EmailAddress("[email protected]", "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/

+0

完美!非常感谢你! –

相关问题