2017-09-05 54 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