Search code examples
c#sendgrid

SendGrid API for C#


I'm looking at the SendGrid API for C# and the task is to send a single email to multiple recipients.

I'm following this example but I want to keep working with objects -- not JSON. https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md

How do I add multiple recipient emails? Looks like it's under Personalizations but their API is not giving me much help in adding multiple recipients.

Mail mail = new Mail();
mail.From = new Email("me@me.com");
mail.Subject = "Some Subject";

// How do I add multiple emails to To field? 

If I wanted to email a single email address, I can simply do this:

Email from = new Email("me@me.com");
Email to = new Email("joe@joe.com");
string subject = "Some subject";
Content content = new Content("text/plain", "Hello World!");
Mail mail = new Mail(from, subject, to, content);

Solution

  • Here is some code I wrote to test sending to multiple recipients. The code below adds two emails to a single request. I tested it and it worked perfectly.

    You just need to declare a new instance of the Personalization object as a List. Populate your individual Personalization objects with the required details for the recipient or recipients of the email. Add to your Personalization List you have declared and then you will be sorted.

    See below and let me know if this helps. My code is pretty self explanatory. I also enabled the tracking for when the recipients open the email. The below should put you in the right direction and you will be able to send multiple emails by sending a single request to the API. :)

    Any questions let me know. Have fun! :)

        static void Main(string[] args)
        {
            Execute().Wait();           
        }
    
    
        static async Task Execute()
        {
    
            try
            {
                string apiKey = "apikey value";
                dynamic sg = new SendGridAPIClient(apiKey);
    
                //Declare Mail object
                Mail mail = new Mail();               
    
                //Declare List as Personalization object type 
                List<Personalization> personal = new List<Personalization>();
    
                //Declare Personalization object to add to List above
                Personalization emailItem = new Personalization();
    
                emailItem.Subject = "Hi there";
    
                //Declare List as Email type 
                List<Email> emails = new List<Email>();
    
                 //Declare Email object to add to List above
                Email email = new Email();
    
    
                email = new Email("email1@example.com", "Recipient 1");
                emails.Add(email);
    
                email = new Email("email2@example.com", "Recipient 2");
                emails.Add(email);
    
                email = new Email("email3@example.com", "Recipient 3");
                emails.Add(email);
    
                emailItem.Tos = emails;
                personal.Add(emailItem);
    
    
                mail.Personalization = personal;
    
                List<Content> contents = new List<Content>();
    
                Content content = new Content("text/plain", "Test contents");
                contents.Add(content);
    
                Email from = new Email("no-reply@test.com", "Test App");
                string subject = "Testing Sending with SendGrid is Fun";
                mail.Subject = subject;
                mail.From = from;
    
                mail.Contents = contents;
    
              dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
    
                Console.WriteLine(response.StatusCode);
                Console.WriteLine(response.Body.ReadAsStringAsync().Result);
                Console.WriteLine(response.Headers.ToString());
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.Read();
            }
        }