Click here to Skip to main content
16,011,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
<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
C#
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
C#
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;
       }


but I can read only the default port setting not the one mentioned in the web.config file
Posted

1 solution

Use the following code only....

C#
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...
 
Share this answer
 
Comments
Please mark the answer as correct so that it will be easier for others to find the answer with one go and it will no longer be in the "Unanswered" list.
You will also be rewarded for this action...
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900