问题描述
我正在尝试使用 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","Test User-Eswar")
},Bccs=new List<EmailAddress>()
{
new EmailAddress("xxx@gmail.com",new EmailAddress("xxx@techmahindra.com","Test User-Eswar")
}
}
};
var response = await client.SendEmailAsync(msg);
var test = response.IsSuccessstatusCode;
}
解决方法
sendgrid 不会在缺少这三个中的任何一个时发送电子邮件,例如 to,cc,bcc
。要么,您必须包含所有三个,才能使用或消除 Email List
中的键。
例如:这就是我在 JS
中的做法,其中 to
、bcc
和 cc
键只有在给定一些数据时才会添加.
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!'
},};