Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an email batch with SendGrid v3

I have a usecase where I have 1000 emails with the bodies prepared (they are ready to send as is), a single sent from email address, 1000 recipients. I am using SendGrid API v3 in C#. I am trying to figure out how to bulk send them to the SendGrid API. This is my code:

 private async Task SendBatchEmails(DataRowCollection emailDataRows)
        {
            var WriteToDatabaseCollection = new Dictionary<Guid, string>();
            var emailObjectCollection = new List<SendGridMessage>();

            foreach (DataRow emailDataRow in emailDataRows)
            {
                var emailObject = new SendGridMessage();

                var to = (new EmailAddress(emailDataRow["RecipientEmailAddress"] + "", emailDataRow["RecipientName"] + ""));
                var from = new EmailAddress(emailDataRow["SenderEmailAddress"] + "", emailDataRow["SenderName"] + "");
                var subject = emailDataRow["Subject"] + "";
                var text = emailDataRow["MessageBody"] + "";
                var html = $"<strong>{emailDataRow["MessageBody"] + "" }</strong>";

                var msg = MailHelper.CreateSingleEmail(from, to, subject, text, html);
                emailObjectCollection.Add(msg);

            }

            await emailClient.SendBatchEmailsEmailAsync(emailObjectCollection);


            dataContext.UpdateEmailResult(WriteToDatabaseCollection);
        } 


    public async Task SendBatchEmailsEmailAsync(List<SendGridMessage> messages)
    {
        return await client.????(messages);
    }

client is a SendGridClient, and the only option I have is: SendEmailAsync(msg)

How do I send a batch fo sendgrid messages?

like image 791
Ronald Herhuth Avatar asked Sep 21 '25 11:09

Ronald Herhuth


1 Answers

Twilio SendGrid developer evangelist here.

There isn't a batch email send for emails with different bodies. Though you can send the same body to multiple addresses.

To send your 1000 emails you need to loop through your list of messages and call the API once per message.

like image 196
philnash Avatar answered Sep 23 '25 00:09

philnash