I have a simple application that sends out status emails to some of our internal users.
I use a simple application configuration file (App.config) to store email address and name information, about the intended users. Since the appSettings section only seem to support simple key/value pairs, it currently looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="toName" value="Recipient Name" />
<add key="toAddr" value="some@email.com" />
<add key="toName2" value="Another Recipient Name" />
<add key="toAddr2" value="another@email.com" />
<add key="ccName" value="An Archive"/>
<add key="ccAddr" value="copies@email.com"/>
<add key="ccName2" value="Another Archive"/>
<add key="ccAddr2" value="morecopies@email.com"/>
</appSettings>
</configuration>
And then I add each recipient individually in the code.
Currently, this means that every time I add or remove recipients, I also need to rewrite the code to handle the new recipients and rebuild and re-deploy the application
I would like to be able to store custom configuration entries, like this maybe:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<recipients>
<recipient recType="to" recAddr="some@email.com" recName="Recipient Name" />
<recipient recType="to" recAddr="another@email.com" recName="Another Recipient Name" />
<recipient recType="cc" recAddr="copies@email.com" recName="An Archive"/>
<recipient recType="cc" recAddr="morecopies@email.com" recName="Another Archive"/>
</recipients>
</configuration>
So I can loop through them:
MailMessage message = new MailMessage();
foreach(recipient rec in recipients)
{
MailAddress mailAddress = new MailAddress(recipient["recAddr"],recipient["recName"]);
if(recipient["recType"] == "cc")
message.CC.Add(mailAddress);
else
message.To.Add(mailAddress);
}
How to accomplish this?