在 C# 中以编程方式从 app.config 访问 system.net 设置
我正在尝试以编程方式访问 Windows 应用程序 app.config 文件。 特别是,我正在尝试访问“system.net/mailSettings” 以下代码
Configuration config = ConfigurationManager.OpenExeConfiguration(configFileName);
MailSettingsSectionGroup settings =
(MailSettingsSectionGroup)config.GetSectionGroup(@"system.net/mailSettings");
Console.WriteLine(settings.Smtp.DeliveryMethod.ToString());
Console.WriteLine("host: " + settings.Smtp.Network.Host + "");
Console.WriteLine("port: " + settings.Smtp.Network.Port + "");
Console.WriteLine("Username: " + settings.Smtp.Network.UserName + "");
Console.WriteLine("Password: " + settings.Smtp.Network.Password + "");
Console.WriteLine("from: " + settings.Smtp.From + "");
未能给出主机,来自。 它只获取端口号。 其余均为空;
I am trying to programmatically access a Windows application app.config file. In particular, I am trying to access the "system.net/mailSettings"
The following code
Configuration config = ConfigurationManager.OpenExeConfiguration(configFileName);
MailSettingsSectionGroup settings =
(MailSettingsSectionGroup)config.GetSectionGroup(@"system.net/mailSettings");
Console.WriteLine(settings.Smtp.DeliveryMethod.ToString());
Console.WriteLine("host: " + settings.Smtp.Network.Host + "");
Console.WriteLine("port: " + settings.Smtp.Network.Port + "");
Console.WriteLine("Username: " + settings.Smtp.Network.UserName + "");
Console.WriteLine("Password: " + settings.Smtp.Network.Password + "");
Console.WriteLine("from: " + settings.Smtp.From + "");
fails to give the host, from. it only gets the port number. The rest are null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我来说似乎没问题:
这是我的 app.config 文件:
但是,正如 Nathan 所说,您可以使用应用程序或计算机配置文件来指定所有 SmtpClient 对象。 有关详细信息,请参阅MDSN 上的元素 。
This seems to work ok for me:
Here's my app.config file:
However, as stated by Nathan, you can use the application or machine configuration files to specify default host, port, and credentials values for all SmtpClient objects. For more information, see <mailSettings> Element on MDSN.
不确定这是否有帮助,但如果您尝试创建 SmtpClient,如果您使用默认构造函数,它将自动使用配置文件中的值。
Not sure if this helps, but if you are trying to make a SmtpClient, that will automatically use the values in your config file if you use the default constructor.
我使用以下命令来访问邮件设置:
I used the following to access the mailSettings: