在 C# 中以编程方式从 app.config 访问 system.net 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/625262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access system.net settings from app.config programmatically in C#
提问by
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
我正在尝试以编程方式访问 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 + "");
fails to give the host, from. it only gets the port number. The rest are null;
未能给主机,从。它只获取端口号。其余为空;
回答by Nathan Totten
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.
不确定这是否有帮助,但如果您尝试创建 SmtpClient,如果您使用默认构造函数,它将自动使用配置文件中的值。
回答by Chris Fulstow
This seems to work ok for me:
这对我来说似乎没问题:
MailSettingsSectionGroup mailSettings =
config.GetSectionGroup("system.net/mailSettings")
as MailSettingsSectionGroup;
if (mailSettings != null)
{
string smtpServer = mailSettings.Smtp.Network.Host;
}
Here's my app.config file:
这是我的 app.config 文件:
<configuration>
<system.net>
<mailSettings>
<smtp>
<network host="mail.mydomain.com" />
</smtp>
</mailSettings>
</system.net>
</configuration>
However, as stated by Nathan, you can use the application or machine configuration files to specify default host, port, and credentials values for all SmtpClientobjects. For more information, see <mailSettings> Elementon MDSN.
但是,如 Nathan 所述,您可以使用应用程序或机器配置文件为所有SmtpClient对象指定默认主机、端口和凭据值。有关详细信息,请参阅MDSN 上的<mailSettings> 元素。
回答by Chris Fulstow
I used the following to access the mailSettings:
我使用以下内容访问邮件设置:
var config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
var mailSettings = config.GetSectionGroup("system.net/mailSettings")
as MailSettingsSectionGroup;
回答by Raju
private void button1_Click(object sender, EventArgs e)
{
try
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var mailSettings = config.GetSectionGroup("system.net/mailSettings")
as MailSettingsSectionGroup;
MailMessage msg = new MailMessage();
msg.Subject = "Hi Raju";
msg.To.Add("[email protected]");
msg.From = new MailAddress("[email protected]");
msg.Body = "Hello Raju here everything is fine.";
//MailSettingsSectionGroup msetting = null;
string mMailHost = mailSettings.Smtp.Network.Host;
SmtpClient mailClient = new SmtpClient(mMailHost);
mailClient.Send(msg);
MessageBox.Show("Mail Sent Succesfully...");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}