使用Sendgrid API for ASP.NET Core WebAPI无法在密件抄送中发送多封电子邮件

时间:2018-09-26 06:09:56

标签: email asp.net-web-api asp.net-core sendgrid sendgrid-api-v3

我正在使用ASP.NET Core WebAPI在其中使用SendGrid发送电子邮件。之前,我能够使用帮助程序类CreateSingleEmail将单个邮件发送给单个收件人。 现在,我希望将同一封电子邮件发送给密件抄送中的多个收件人,因此,这些收件人无法看到其他收件人。 我已经阅读了许多有关此问题的文档,甚至以前遇到过类似的问题,但仍然无法发送邮件。 我也尝试过使用个性化设置块。我本身没有收到任何错误,但是没有通过此方式发送电子邮件。 这是我的方法-

请注意,此处的toEmail属于列表类型。

       var apiKey = "MySendgridApiKey"
        var client = new 
        SendGridClient(apiKey);
        var msg = new SendGridMessage();
        msg.SetFrom(new EmailAddress(fromEmail));
        msg.AddTo(new EmailAddress(fromEmail), 1);
        msg.SetBypassListManagement(true);
        msg.SetBccSetting(true, fromEmail);
        msg.AddSubstitution("%name1%", "Example User1");
        msg.SetSubject(subject);
        var personalization = new Personalization()
        {
            Bccs = toEmail  
        };
        msg.AddBccs(toEmail);
        msg.AddBccs(toEmail, 0, personalization);
        msg.Serialize();
        var response = client.SendEmailAsync(msg);
        return true;

技术细节: 平台(ASP.NET Core WebAPI) .NET Core版本:2.0.8

1 个答案:

答案 0 :(得分:0)

您已经在使用

msg.AddBccs(toEmail);

这也需要List<SendGridMessage>。因此,只需在此处将所有电子邮件地址作为列表传递即可。