我正在使用此代码从网络应用程序发送电子邮件。只有一位收件人没有问题。我研究过使用来自的相同技术https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html向多个收件人发送电子邮件。我尝试使用逗号分隔的字符串作为目标(请参阅代码中的参数),即[电子邮件受保护], [电子邮件受保护], [电子邮件受保护]但 SendGrid 仅接受第一个收件人。我也尝试使用数组,但结果相似,SG 仅获取最后一个收件人。传递收件人列表的正确方法是什么?
public class email
{
public void enviar(string destinatario, string asunto, string contenido)
{
Execute(destinatario, asunto, contenido).Wait();
}
static async Task Execute(string destinatario, string asunto, string contenido)
{
string apiKey = "SG...............";
dynamic sg = new SendGridAPIClient(apiKey);
Email from = new Email("[email protected]");
string subject = asunto;
Email to = new Email(destinatario);
Content content = new Content("text/plain", contenido);
Mail mail = new Mail(from, subject, to, content);
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
}
}
您需要添加个性化列出这个。以下代码对我有用。
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("[email protected]", "Sender Name"),
Subject = "Subject",
PlainTextContent = "Text for body",
HtmlContent = "<strong>Hello World!",
Personalizations = new List<Personalization>
{
new Personalization
{
Tos = new List<EmailAddress>
{
new EmailAddress("[email protected]", "abc"),
new EmailAddress("[email protected]", "efg")
}
}
}
};
var response = await client.SendEmailAsync(msg);
欲了解更多详情,请查看以下内容邮件发送
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)