类型 'MailAddress' 在未引用的程序集中定义

The type 'MailAddress' is defined in an assembly that is not referenced

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'.

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;

SendGridMessageclass的From属性是Sendgrid.Helpers.MailEmailAddressclass的一种命名空间。但是你在这里分配了不同的类型

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

我认为您使用的 .net 核心 nuget 包不正确。与 .NET Core 兼容的带有 V3 api 的 Sendgrid 的正确更新 nuget 包可在下面 link (v 9.9.0) 获得。

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

检查下面的示例代码以使用 MailHelper class

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

查看下面的 link 以获取更多示例

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