[转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

时间:2022-12-01 17:22:26

本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386

1.  在WinForm下读取 App.config中的邮件配置语句如下:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//OpenExeConfiguration2个方法的参数我也没搞清楚到底该怎么用。
MailSettingsSectionGroup mailSettings = NetSectionGroup.GetSectionGroup(config).MailSettings; App.config代码如下: <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<mailSettings>
<smtp from="admin@wmdev.mobi">
<network host="smtp.wmdev.mobi"
userName="admin"
password="password"/>
</smtp>
</mailSettings>
</system.net>
</configuration> 2. 在WebForm下读取Web.config中的邮件配置代码如下: Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
MailSettingsSectionGroup mailSettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");//此处的"system.net/mailSettings“如果改成其他节点则读取其他的相应的配置信息 Web.config代码如下: 与App.config一样 <?xml version="1.0" ?>
<configuration>
<system.net>
<mailSettings>
<smtp from="admin@wmdev.mobi">
<network host="smtp.wmdev.mobi"
userName="admin"
password="password"/>
</smtp>
</mailSettings>
</system.net>
</configuration>