【问题标题】:The type 'MailAddress' is defined in an assembly that is not referenced“MailAddress”类型是在未引用的程序集中定义的
【发布时间】:2017-09-05 13:32:09
【问题描述】:

错误:“MailAddress”类型是在未引用的程序集中定义的。您必须添加对程序集“System.Net.Mail, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”的引用。

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;

【问题讨论】:

  • 你添加了那个引用吗?
  • 还有,netcoreapp,还是完整的框架?
  • 是的,我参考了 System.Net.Mail
  • @WanjyDan 在下面查看我的答案

标签: c# sendgrid asp.net-core-2.0


【解决方案1】:

SendGridMessage 类的From 属性是EmailAddress 类的一种类型,它位于Sendgrid.Helpers.Mail 命名空间中。但是您在这里分配了不同的类型

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

我认为您为 .net core 使用了不正确的 nuget 包。下面的链接 (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("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);

查看以下链接了解更多示例

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

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多