Access system.net settings from app.config programmatically in C#

若如初见. 提交于 2019-12-19 15:04:48

问题


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;


回答1:


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.




回答2:


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.




回答3:


I used the following to access the mailSettings:

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

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



回答4:


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("raju@hasten.in");
        msg.From = new MailAddress("hasten.c@hasten.in");
        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());
    }
}


来源:https://stackoverflow.com/questions/625262/access-system-net-settings-from-app-config-programmatically-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!