Johnathan Logan
Johnathan Logan

Reputation: 367

How to send emails with List<string>() using Send Grid mvc azure

I was reading the Send Grid documentation, and below it states that in order to add emails of recipients, this is required:

    // Add multiple addresses to the To field.
List<String> recipients = new List<String>
{
    @"Jeff Smith <[email protected]>",
    @"Anna Lidman <[email protected]>",
    @"Peter Saddow <[email protected]>"
};

myMessage.AddTo(recipients);

Meanwhile in my code I have a list string with stored emails.

 emailing.find_email_send = new List<string>();
 emailing.find_email_send.Add("[email protected]");
 emailing.find_email_send.Add("[email protected]");
 emailing.find_email_send.Add("[email protected]");

How can I add this to recipients ? I tried using forloop:

recipients.Add(emailing.find_email_send[i]);

But doesn't seem to work.

Upvotes: 0

Views: 581

Answers (1)

Dave Greilach
Dave Greilach

Reputation: 895

If the myMessage.AddTo method takes a List as a parameter and you already have your addresses in a List, then you don't need to do anything except:

myMessage.AddTo(emailing.find_email_send);

If you really want to add the addresses from your list to the recipients list it would be:

var recipients = new List<string>();
foreach (var address in emailing.find_email_send)
{
    recipients.Add(address);
}

Upvotes: 2

Related Questions