【发布时间】:2016-12-08 03:14:56
【问题描述】:
我正在使用此代码从 Web 应用程序发送电子邮件。只有一个收件人没有问题。我研究过使用来自https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html 的相同技术向多个收件人发送电子邮件。我尝试使用逗号分隔的字符串作为 destinatario(请参阅代码中的 args),即 you@example.com, she@example.com, he@example.com 但 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("someone@example.com");
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());
}
}
【问题讨论】:
-
不要使用
Task.Wait()。它会造成死锁。您需要在任何地方使用async。 (也不要使用async void) -
也不要使用
dynamic。