无法读取来自web.config文件的邮件设置 [英] Cannot read mail settings form web.config file
本文介绍了无法读取来自web.config文件的邮件设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我已使用以下代码读取配置文件,但仅获得默认端口号,而没有其他
请检查代码并告诉我应该怎么做
Web.config文件:
Hi
I have used the following code to read the config file and i get only the default port number and nothing else
pl check the code and tell me what should be done
Web.config file:
<system.net>
<mailSettings>
<smtp from="abinav@ymail.com">
<network host="smtp.bizmail.yahoo.com" port="25" userName="abinav@ymail.com" password="Mypassword" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
使用以下代码将物理路径转换为虚拟路径
Used the following code to convert the physical path to virtual path
public static Configuration OpenConfigFile(string configPath)
{
var configFile = new FileInfo(configPath);
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var wcfm = new WebConfigurationFileMap(); wcfm.VirtualDirectories.Add("/", vdm);
return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}
使用上述方法读取配置文件
Used the above method to read the config file
string strConfigPath = Server.MapPath("~/LeaveManagementSystem_30_01_2012/web.config");
//strConfigPath = @"~/web.config";// HttpContext.Current.Request.ApplicationPath + "/" + "web.config";
Configuration config = OpenConfigFile(strConfigPath) as Configuration;
MailSettingsSectionGroup mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
int port = mailSettings.Smtp.Network.Port;
string host = mailSettings.Smtp.Network.Host;
string password = mailSettings.Smtp.Network.Password;
string username = mailSettings.Smtp.Network.UserName;
}
但是我只能读取默认端口设置,而不能读取web.config文件中提到的默认端口设置.
but I can read only the default port setting not the one mentioned in the web.config file
推荐答案
仅使用以下代码....
Use the following code only....
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup mailSettings = (System.Net.Configuration.MailSettingsSectionGroup) config.GetSectionGroup("system.net/mailSettings");
if (mailSettings != null)
{
int port = mailSettings.Smtp.Network.Port;
string host = mailSettings.Smtp.Network.Host;
string password = mailSettings.Smtp.Network.Password;
string username = mailSettings.Smtp.Network.UserName;
}
您肯定会得到所需的一切...
You will surely get all that you need...
这篇关于无法读取来自web.config文件的邮件设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文