Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sendgrid multiple recipients c#

Tags:

c#

email

sendgrid

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());
    }


}
like image 341
mainavatar Avatar asked Aug 02 '16 16:08

mainavatar


People also ask

How do I send an email to multiple recipients in SendGrid?

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.

Can you CC in SendGrid?

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).

What is SendGrid C#?

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.

How many emails can be sent at once in SendGrid?

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!


1 Answers

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

like image 102
reza.cse08 Avatar answered Oct 01 '22 00:10

reza.cse08