C# App.config中的多个邮件设置
我目前有一个App.config,如下所示:C# App.config中的多个邮件设置,c#,C#,我目前有一个App.config,如下所示: <system.net> <mailSettings> <smtp from="xxx@xxnn.co.uk"> <network host="nn.nn.nn.nn" port="25" userName="myname" password="mypass"/>
<system.net>
<mailSettings>
<smtp from="xxx@xxnn.co.uk">
<network host="nn.nn.nn.nn"
port="25"
userName="myname"
password="mypass"/>
</smtp>
</mailSettings>
</system.net>
但是如何配置3个或4个不同的
,并在运行时引入正确的配置
要从中发送的邮箱将根据下面的行[“分类账”]
而有所不同
foreach (DataRow row in accounts.Tables[0].Rows)
{
string cust = row["Account"].ToString();
string site = row["Ledger"].ToString();
string mail = row["Email"].ToString();`
您可以在消息对象中指定发件人地址:
msg.From = new MailAddress("fred@somewhere.com");
因此,现在您只需将站点
值映射到发件人电子邮件地址。例如,如果将它们保存在app.config
的appSettings
中:
<appSettings>
<add key="Site1" value="someone@somewhere.com" />
<add key="Site2" value="someone@somewhere-else.com" />
</appSettings>
例如,您的完整代码可能如下所示
SmtpClient client = new SmtpClient();
foreach (DataRow row in accounts.Tables[0].Rows)
{
string cust = row["Account"].ToString();
string site = row["Ledger"].ToString();
string mail = row["Email"].ToString();
//Get your email address, say, from the app.config file
string fromAddress = GetFromAddressBySite(site);
MailMessage msg = new MailMessage
{
From = new MailAddress(fromAddress),
Subject = "Hello!",
Body = "..."
};
msg.To.Add(new MailAddress(mail));
client.Send(msg);
}
注意:您还可以将服务器主机、端口等存储在设置文件中并使用它们:
var client = new SmtpClient("your.mailserver.com", 25);
您不必从
mailSettings
获取设置。您可以使用自己的配置系统,例如JSON文件:
{
"PublicEmailAddress" : { "From" : "external@mycompany.com" },
"InternalEmailAddress": { "From" : "internal@mycompany.com"}
}
您可以在启动时编写代码来读取配置并将配置存储在内存中,然后根据
行[“分类账”]
选择合适的配置。经过进一步研究,我提出了以下解决方案:
在App.Config中
<configSections>
<sectionGroup name="Ledgers">
<section name="1stCompany" type="System.Configuration.NameValueSectionHandler" />
<section name="2ndCompany" type="System.Configuration.NameValueSectionHandler" />
<section name="3rdCompany" type="System.Configuration.NameValueSectionHandler" />
<section name="4thCompany" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<Ledgers>
<1stCompany>
<add key="host" value="nn.nn.nn.nn"/>
<add key="user" value="xxx1@xxx1.co.uk"/>
<add key="pass" value="password"/>
<add key="from" value="xxx1@xxx1.co.uk"/>
</1stCompany>
<2ndCompany>
<add key="host" value="nn.nn.nn.nn"/>
<add key="user" value="xxx2@xxx2.co.uk"/>
<add key="pass" value="password"/>
<add key="from" value="xxx2@xxx2.co.uk"/>
</2ndCompany>
<3rdCompany>
<add key="host" value="nn.nn.nn.nn"/>
<add key="user" value="xxx3@xxx3.co.uk"/>
................................. etc ...........
................................. etc .................
</Ledgers>
</configuration>
类方法似乎相当费劲。
我确信我可以直接转到所需的部分,因为我知道我需要哪个部分,因为它的标题与参数“site”匹配。
但是它正在工作,所以现在还可以我宁愿使用配置文件,因此邮件服务器详细信息的任何未来更改都只需要编辑文本文件,而不需要重新编译源代码。那么,
行[“分类账”]
中有什么内容呢?这是您的配置。DataRow中的数据不包含任何mailserver信息。行[“分类账”]只是一个字符串,其中包含发送电子邮件的公司的名称,但映射到配置的代码完全由您决定。为什么不将这些邮件设置保存在数据库中呢?这是一个想法,尽管不是我的数据库,但我可以创建一个可以在“分类账”上加入的表。
{
"PublicEmailAddress" : { "From" : "external@mycompany.com" },
"InternalEmailAddress": { "From" : "internal@mycompany.com"}
}
<configSections>
<sectionGroup name="Ledgers">
<section name="1stCompany" type="System.Configuration.NameValueSectionHandler" />
<section name="2ndCompany" type="System.Configuration.NameValueSectionHandler" />
<section name="3rdCompany" type="System.Configuration.NameValueSectionHandler" />
<section name="4thCompany" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<Ledgers>
<1stCompany>
<add key="host" value="nn.nn.nn.nn"/>
<add key="user" value="xxx1@xxx1.co.uk"/>
<add key="pass" value="password"/>
<add key="from" value="xxx1@xxx1.co.uk"/>
</1stCompany>
<2ndCompany>
<add key="host" value="nn.nn.nn.nn"/>
<add key="user" value="xxx2@xxx2.co.uk"/>
<add key="pass" value="password"/>
<add key="from" value="xxx2@xxx2.co.uk"/>
</2ndCompany>
<3rdCompany>
<add key="host" value="nn.nn.nn.nn"/>
<add key="user" value="xxx3@xxx3.co.uk"/>
................................. etc ...........
................................. etc .................
</Ledgers>
</configuration>
private SmtpClient findClient(string site, ref string from)
{
// Get the application configuration file.
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the collection of the section groups.
ConfigurationSectionGroupCollection myCollect = config.SectionGroups;
string host = "";
string user = "";
string pass = "";
SmtpClient client = new SmtpClient();
foreach (ConfigurationSectionGroup myGroups in myCollect)
{
if (myGroups.Name != "Ledgers") continue;
foreach (ConfigurationSection mySection in myGroups.Sections)
{
string ledger = mySection.SectionInformation.Name.ToString();
Console.WriteLine("Site is " + site + "ledger is " + ledger);
if (ledger.Equals(site, StringComparison.Ordinal))
{
var section = ConfigurationManager.GetSection(
mySection.SectionInformation.SectionName)
as NameValueCollection;
host = section["host"];
user = section["user"];
pass = section["pass"];
from = section["from"];
Console.WriteLine("\n\nHost " + host + "\nUsername " +
user + "\nPassword " + pass + "\nFrom " + from);
client.Port = 25;
client.Host = host;
client.Credentials = new System.Net.NetworkCredential(user, pass);
break;
}
}
}
return client;
}