I'm using this code to send email from a web application. No problem with just one recipient. I've researched to use the same technic coming from https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html to send email to multiple recipients. I tried whith a comma delimited string as destinatario (see args in the code) i.e. [email protected], [email protected], [email protected] but SendGrid takes the first one recipient only. I also tried using an array but the result is simmilar, SG takes the last recipient only. What is the correct way to pass the list of recipients?
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());
}
}
The most straightforward way to send bulk emails is to have an array of addresses in the to field, and then call sendMultiple with a single message object. Copy this code into index. js and replace the emails in the to array with your email addresses. const sgMail = require('@sendgrid/mail'); sgMail.
SendGrid supports the capability to send the single email to one or more recipients in the TO field plus one or more recipients in the CC or BCC fields (reference here: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html).
With Twilio SendGrid, you can send emails without worrying about scalability. You can use the SendGrid APIs and SDKs to start sending emails within minutes. You could also use the SMTP protocol if you already have an existing codebase that relies on SMTP.
Once you create a sender identity a, you can send 100 emails per day. If you need to send more than that, complete your account setup to upgrade to a paid plan!
You need to add the Personalizations list for that. Following code works for me.
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);
For more details check following Mail Send
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With