在 C# 中以编程方式从 app.config 访问 system.net 设置

发布于 2024-07-14 15:40:09 字数 746 浏览 6 评论 0原文

我正在尝试以编程方式访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你曾走过我的故事 2024-07-21 15:40:09

这对我来说似乎没问题:

MailSettingsSectionGroup mailSettings =
    config.GetSectionGroup("system.net/mailSettings")
    as MailSettingsSectionGroup;

if (mailSettings != null)
{
    string smtpServer = mailSettings.Smtp.Network.Host;
}

这是我的 app.config 文件:

<configuration>
  <system.net>
    <mailSettings>
      <smtp>
        <network host="mail.mydomain.com" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

但是,正如 Nathan 所说,您可以使用应用程序或计算机配置文件来指定所有 SmtpClient 对象。 有关详细信息,请参阅 MDSN 上的元素

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:

<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 SmtpClient objects. For more information, see <mailSettings> Element on MDSN.

西瑶 2024-07-21 15:40:09

不确定这是否有帮助,但如果您尝试创建 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.

星星的軌跡 2024-07-21 15:40:09

我使用以下命令来访问邮件设置:

var config = ConfigurationManager.OpenExeConfiguration(
    ConfigurationUserLevel.None);

var mailSettings = config.GetSectionGroup("system.net/mailSettings") 
    as MailSettingsSectionGroup;

I used the following to access the mailSettings:

var config = ConfigurationManager.OpenExeConfiguration(
    ConfigurationUserLevel.None);

var mailSettings = config.GetSectionGroup("system.net/mailSettings") 
    as MailSettingsSectionGroup;
深海不蓝 2024-07-21 15:40:09
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());
    }
}
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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文