Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Application Configuration to store email contact information

Tags:

c#

.net

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="[email protected]" />
    <add key="toName2" value="Another Recipient Name" />
    <add key="toAddr2" value="[email protected]" />
    <add key="ccName" value="An Archive"/>
    <add key="ccAddr" value="[email protected]"/>
    <add key="ccName2" value="Another Archive"/>
    <add key="ccAddr2" value="[email protected]"/>
  </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="[email protected]" recName="Recipient Name" />
    <recipient recType="to" recAddr="[email protected]" recName="Another Recipient Name" />
    <recipient recType="cc" recAddr="[email protected]" recName="An Archive"/>
    <recipient recType="cc" recAddr="[email protected]" 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?

like image 935
Mathias R. Jessen Avatar asked Oct 05 '22 15:10

Mathias R. Jessen


1 Answers

You should write a custom config section for your recipients and then include this section. With the custom section you will be also able to store the recipients config info outside the main config file and include it using the configSource attribute.

For a start you can look here: http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx

In short, you should:

  1. Implement your custom config section by inheriting from ConfigurationElement (for one element) and ConfigurationElementCollection (for collections, you need collection in your case and each recipient will be element of connection). Sample implementation is in answer here: how to have custom attribute in ConfigurationElementCollection?

  2. Define config section in main config

  3. Add your custom configuration and it could be included as separate config file

like image 162
Regfor Avatar answered Oct 13 '22 12:10

Regfor