2017-09-05 51 views
0

Error: The type 'MailAddress' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net.Mail, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.类型“MailAddress”中未被引用

enter image description here

var myMessage = new SendGrid.SendGridMessage(); 

     myMessage.AddTo(model.To); 
     myMessage.From = new System.Net.Mail.MailAddress(model.From, model.Subject); 
     myMessage.Subject = model.Subject; 
     myMessage.Text = model.TextBody; 
     myMessage.Html = model.HtmlBody; 
+2

并且您是否添加了该参考? – David

+0

另外,netcoreapp,或完整的框架? – poke

+0

是的,我有一个参考System.Net.Mail – WanjyDan

回答

0

SendGridMessage类的From属性是类型EmailAddress类,这是在Sendgrid.Helpers.Mail命名空间的组件被定义。但是,你在这里分配不同类型的

myMessage.From = new System.Net.Mail.MailAddress(model.From, model.Subject); 

我想你使用的是不正确的NuGet包.NET的核心。在下面的链接(v 9.9.0)中提供了适用于与.NET Core兼容的V3 api的Sendgrid的正确更新nuget软件包。

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

检查下面的示例代码使用MailHelper类发送电子邮件与V3 API

  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); 

查看以下链接,更多的例子

https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md#send-a-single-email-to-a-single-recipient

你可以找到这里有类似的问题

https://github.com/sendgrid/sendgrid-csharp/issues/507