.NET Application Configuration用于存储电子邮件联系信息

Mat*_*sen 5 .net c#

我有一个简单的应用程序,可以向我们的一些内部用户发送状态电子邮件.

我使用一个简单的应用程序配置文件(App.config)来存储有关预期用户的电子邮件地址和名称信息.由于appSettings部分似乎只支持简单的键/值对,因此它目前看起来像这样:

<?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>
Run Code Online (Sandbox Code Playgroud)

然后我在代码中单独添加每个收件人.

目前,这意味着每次添加或删除收件人时,我还需要重写代码以处理新收件人并重建和重新部署应用程序

我希望能够存储自定义配置条目,如下所示:

<?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>
Run Code Online (Sandbox Code Playgroud)

所以我可以遍历它们:

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);
}
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?

回答: 使用Regfor链接中的示例,我能够构建一个自定义配置部分,其中包含一组自定义ConfigurationElements,如下所示:

public class RecipientElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get
        {
            return (string)base["name"];
        }
    }

    [ConfigurationProperty("mailAddr", IsRequired = true)]
    public string Address
    {
        get
        {
            return (string)base["mailAddr"];
        }
    }

    [ConfigurationProperty("isCC")]
    public bool IsCC
    {
        get
        {
            return (bool)base["isCC"];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用最终配置部分:

<recipientSection>
  <recipients>
    <recipient name="Primary recipient" mailAddr="usermailbox@email.com" isCC="false" />
    <recipient name="Archive" mailAddr="copies@email.com" isCC="true" />
  </recipients>
</recipientSection>
Run Code Online (Sandbox Code Playgroud)

循环通过recipients集合让我添加尽可能多的收件人,因为SmtpClient将让我发送到:)

多谢你们

Reg*_*for 3

您应该为收件人编写自定义配置部分,然后包含此部分。通过自定义部分,您还可以将收件人配置信息存储在主配置文件之外,并使用属性包含它configSource

首先,您可以查看此处: http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx

简而言之,您应该:

  1. 通过继承ConfigurationElement(对于一个元素)和ConfigurationElementCollection(对于集合,您需要在您的情况下进行集合,并且每个接收者将是连接的元素)来实现自定义配置部分。示例实现在这里回答: how to have custom attribute in ConfigurationElementCollection?

  2. 在主配置中定义配置部分

  3. 添加您的自定义配置,它可以作为单独的配置文件包含在内