2012-12-18 23 views
5

我有一個簡單的應用程序發出的通知郵件到我們的一些內部用戶。.NET應用程序配置來存儲電子郵件聯繫信息

我用一個簡單的應用程序配置文件(app.config)中存儲的電子郵件地址和姓名信息,對目標用戶。由於appSettings部分似乎只支持簡單的鍵/值對,它目前看起來是這樣的:

<?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> 

然後我在代碼中單獨添加每個收件人。

目前,這意味着我每次添加或刪除收件人的時間,我還需要重寫代碼來處理新的收件人和重建,並重新部署應用程序

我希望能夠存儲自定義配置條目,這樣也許:

<?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> 

這樣我就可以遍歷其中:

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); 
} 

如何做到這一點?

回答: 在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"]; 
     } 
    } 
} 

與最終的配置部分:

<recipientSection> 
    <recipients> 
    <recipient name="Primary recipient" mailAddr="[email protected]" isCC="false" /> 
    <recipient name="Archive" mailAddr="[email protected]" isCC="true" /> 
    </recipients> 
</recipientSection> 

通過循環訪問recipients集合,可以添加儘可能多的收件人,因爲SmtpClient會讓我發送到:)

Thanks guys guys

+2

你可以創建一個自定義配置部分,截至http://stackoverflow.com/questions/758986/custom-app-config-config-section-handler – stuartd

+0

@StuartDunkeld概述非常漂亮,這正是像這是我正在尋找的,儘管這似乎有點誇張,因爲你使用配置文件的目的不是爲了這個(縫隙)的簡單問題 –

+0

@ MathiasR.Jessen它看起來很誇張。花費在試圖破解配置文件上的時間可能會花在創建自己簡單的xml文件上,這些文件的設計完全是您想要的。 –

回答

3

您應該爲您的收件人編寫cusom配置部分,然後包含此部分。使用自定義部分,您還可以在主配置文件外存儲recepients config,並使用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. 添加您的自定義配置,它可以被列爲單獨的配置文件

+0

另外兩個指導鏈接:http://msdn.microsoft.com/en-us/library/2tw134k3.aspx或這裏用configSource http://joelabrahamsson.com/entry/creating-a -custom-configuration-section-in-net – Regfor

+0

已經看過3-easy-steps文章,沒有多大幫助,我需要一個元素集合(多個收件人),該文章一次只處理1個。 joel abrahamsson最後一個鏈接非常有趣,雖然 –

+0

不,這只是開始理解定製配置節的想法。你有沒有檢查第2步鏈接http://stackoverflow.com/questions/8829414/how-to-have-custom-attribute-in-configurationelementcollection? – Regfor

1

在web.config中創建自定義節。你可以找到一些例子,說明如何做到這一點這裏http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx或者你可以在google一點。

比你可以從節中的實體映射到某種如果POCO,你將爲表示電子郵件接收器。

所以你只好用電子郵件接收器即會方便您的工作集合操作。

而且不要忘記用於發送電子郵件創建服務層。

所以這裏你必須做的步驟:

  1. 創建自定義配置節。
  2. 創建電子郵件接收器
  3. 的波索表示地圖從自定義配置的實體到POCO集合
  4. 創建用於發送電子郵件

服務或簡單的輔助BTW這是一個很好的做法,將域/應用程序特定的邏輯分開以分開文件,因此請參閱此鏈接以及Moving a custom configuration group to a separate file

祝您好運!

+0

不是ASP,但我想這是無關緊要的。但是,我查看了3-easy-steps解決方案,示例中的自定義配置元素是唯一的,即。只有1個BlogSettings元素。我需要存儲一組元素 –

0

您可以將名稱/地址值存儲在每個郵件尋址的部分中。

<configuration> 
    <configSections> 
    <section 
     name="MailAddressing" 
     type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
    </configSections> 

    <MailAddressing> 
    <add key="To" value="&quot;An Example&quot; <[email protected]>;&quot;Second Example&quot; <[email protected]>" /> 
    <add key="CC" value="[email protected]" /> 
    <add key="BCC" value="[email protected]" /> 
    <add key="From" value="[email protected]" /> 
    </MailAddressing> 
</configuration> 

然後通過

NameValueCollection section = 
     (NameValueCollection)ConfigurationManager.GetSection("MailAddressing"); 

訪問addresse(多個)或許用於序列化的最簡單的解決方案是使用字符串轉換器在MailAddress類來處理的設置的值。

// Test data 
var addressList = new[] 
    { 
     new MailAddress("[email protected]", "An Example"), 
     new MailAddress("[email protected]", "Second Example") 
    }; 

// To String for saving in config 
string strValue = addressList.Select(i => i.ToString()) 
      .Aggregate((i, j) => i + ';' + j); 

// From String for reading from config 
MailAddress[] addressList2 = strValue.Split(';').Select(i => 
      new MailAddress(i)).ToArray(); 

現在,您可以對每個通過郵件尋址分組的To/CC/BCC值進行一個配置設置。它可以與單個地址或多個地址一起使用,無論是否有顯示名稱。

+0

是的,我試圖擺弄NameValueCollection類型在你的例子中,但我仍然面臨的關鍵/價值的限制,我有每個條目(名稱,地址,以/ cc)多個屬性 –

+0

我想你會喜歡什麼我添加了:) – ccook

0

儘管我同意自定義配置節是一種有效的方法,但可以在一個appSetting中放置多個地址,包括顯示名稱。例如。:

<add key="To" 
    value='"Recipient Name" &lt;[email protected]>, "Another Recipient Name" &lt;[email protected]>'/> 

... 
string to = ConfigurationManager.AppSettings["To"]; 
MailMessage m = new MailMessage(); 
m.To.Add(to); 
相關問題