添加 cc 和 bcc 时无法通过发送网格发送电子邮件

时间:2021-03-26 09:52:29

标签: .net c#-4.0 sendgrid

我正在尝试使用 to list、cc list 和 bcc list 发送电子邮件通知,但我收到错误消息,错误请求和成功代码为 false。

如果我不添加抄送、密送列表,我可以发送电子邮件。你能帮我解决这个问题吗

var apiKey = "SG.xxxxxxxxxxxxxxxxxxxxxxxx"; //Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY"); var client = new SendGridClient(apiKey); var msg = new SendGridMessage();

        msg.From = new EmailAddress("xxxxxx@nidec-motor.com", "TechM");
        msg.Subject = "Test email";
        msg.PlainTextContent = "Sendgrid test email";
        msg.HtmlContent = "<strong>Hello World!";
        msg.Personalizations = new List<Personalization>
            {
            new Personalization
            {
                  Tos = new List<EmailAddress>
                  {
                       new EmailAddress("xxxxxx@gmail.com", "Eswar"),
                       new EmailAddress("xxxx@techmahindra.com", "Test User-Eswar")
                  },
                  Ccs= new List<EmailAddress>()
                  {
                         new EmailAddress("xxxx@TechMahindra.com", "Eswar"),
                       new EmailAddress("xxxx@techmahindra.com", "Test User-Eswar") 
                  },
                  Bccs=new List<EmailAddress>()
                  {
                         new EmailAddress("xxx@gmail.com", "Eswar"),
                       new EmailAddress("xxx@techmahindra.com", "Test User-Eswar")
                  }
            }

        };

        var response = await client.SendEmailAsync(msg);
        var test = response.IsSuccessStatusCode;
    }

1 个答案:

答案 0 :(得分:0)

sendgrid 不会在缺少这三个中的任何一个时发送电子邮件,例如 to, cc, bcc。要么,您必须包含所有三个,才能使用或消除 Email List 中的键。

例如:这就是我在 JS 中的做法,其中 tobcccc 键只有在给定一些数据时才会添加.

const sendEmailObj = {
      from: `goutham`,
      ...(to !== '' && {to: to?.split(',')}),
      ...(ccEmailAddress !== '' && {cc: ccEmailAddress?.split(',')}),
      ...(bccEmailAddress !== '' && {bcc: bccEmailAddress?.split(',')}),
      template: 'temaplate-id',
      subject: `Sendgrid Working`,
      context: {
        message: 'Hello world!'
      },
    };