I’ve been using the SendGrid API for a project and I was trying to be lazy and read multiple email addresses to send to from a single parameter in appsettings.json with the email addresses separated by a semicolon. Strangely this only sends to the first email in the string so I needed to do a little more work and store the emails in an array in appsettings.json and to read this into a list and then loop through it adding each address individually.

appsettings.json

{
	"Email": {
		"ToEmails": [
			"test1@test.com",
			"test2@test.com",
			"test3@test.com"
		]
	}
}

Program.cs

List<string> emailTo =_config.GetSection("Email:ToEmails").Get<List<string>>()

foreach(stringemailinemailTo)
{
	sendGridMessage.AddTo(newEmailAddress(email));
}

If you are aiming to use this in a console application then you’ll need to install the following NuGet packages.

Microsoft.Extensions.Configuration    
Microsoft.Extensions.Configuration.Binder

13 Comments

Jim · 29th March 2019 at 2:38 pm

Any idea how to do this with combined email address/display name?

    Shinigami · 29th March 2019 at 4:12 pm

    Depends on how you’ve got these stored in your appsettings.json file, if it’s like the below then you can load them into a dictionary rather than a list.

    “Emails”: {
    “test@test.com”: “Mr Test”,
    “test2@test.com”: “Ms Test”
    }

    Dictionary<string, string> emails = _config.GetSection(“Emails”).Get<Dictionary<string, string>>();

      Jim · 29th March 2019 at 5:45 pm

      Thanks. I ended up using one string like “test@test.com:Joe Test” and using string.split(‘:’) after I pulled it out of appsettings.json.

        Bryan · 10th May 2019 at 8:41 pm

        You could also create a class with the and change the structure of your json a little too
        public class EmailConfig
        {
        public string Address { get; set; }

        public string To { get; set; }
        }

        “Emails”: [
        {
        “Address”: “test@test.com”,
        “To”: “Mr. Test”
        },
        {
        “Address”: “test2@test.com”,
        “To”: “Mr. Test2”
        }
        ]

        var emails = _config.GetSection(“Emails”).Get<List>();

        This way you stay strongly typed and don’t have to do string manipulation

          TS · 8th May 2020 at 3:52 pm

          I’ve added my data in appsetting in this similar way but I’ve configured this in hostbuilder and now _config.GetSection is not giving me anything

Marek · 20th September 2020 at 8:57 am

Bryan is right, but I believe, that first email object structure should be defined.

public class Email
{
public string Address {get;set;}
public string To [get;set}
}
var emails = _config.GetSection(“Emails”).Get<List>();

Then you get data binded properly.

Marek Adameczek · 20th September 2020 at 9:00 am

I dont know why this line wasn’t saved in edited version. It should be:
var emails = _config.GetSection(“Emails”).Get<List>();

    Marek Adameczek · 20th September 2020 at 9:01 am

    One more attaempt:
    var emails = _config.GetSection(“Emails”).Get<List>();

    Marek Adameczek · 20th September 2020 at 9:05 am

    Something goes wrong with reply mechanism. I paste whole code line edited externally, but saved is something else :(. So, I type it again in input field. Just last chained method:
    …Get<List>();

Marek Adameczek · 20th September 2020 at 9:09 am

Can’t believe. This site msy be using stange parser, which changes input text 🙁
Anyway. Last try. List must be of your Email class: List

Marek Adameczek · 20th September 2020 at 9:16 am

Bryan is right, but I believe, that first email object should be defined.

public class EmailType
{
public string Address {get;set;}
public string To [get;set}
}
var emails = _config.GetSection(“Emails”).Get<List>();

Then you get data binded properly.

BTW. I posted reply muiltiple times, as input field parser must be trying to eliminate some html tags without warning, what causes wrong code saved. Hopefully now right code will be saved

Marek Adameczek · 20th September 2020 at 9:18 am

I give up. What is above, is wrong due to site reply mechanism error.

    Shinigami · 21st September 2020 at 9:16 am

    Sorry about the formatting, WordPress can do weird things to special characters that are posted in the comments. I might see if I can find a plugin that allows proper formatting of code within them. My class is as it is because this was originally taken from a project that sent a notification email to multiple addresses as well as CCing in other addresses, thisd Email class contained all the settings for the notification email. I see that this could be confusing out of context though so I’ll adjust the example to make it clearer.

Leave a Reply

Your email address will not be published. Required fields are marked *