Click here to Skip to main content
16,011,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I want to read the network settings from the web config file and i ended up getting the exception as shown below I have atached the code with this pl tell me how to specify the path of the web.config file

C#
public void sendEmail()
    {
        string strConfigPath = Server.MapPath("~/LeaveManagementSystem_30_01_2012");
        Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(strConfigPath);
        MailSettingsSectionGroup mailSettings = configurationFile.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;
        }
    }


Web.config file:

C#
<system.net>
    <mailSettings>
      <smtp from="abinav@kcubeconsulting.com">
        <network host="smtp.bizmail.yahoo.com" port="25" userName="Asd@yahoo.com" password="Password" defaultCredentials="true" />
      </smtp>
    </mailSettings>

  </system.net>


Exception:

'E:\Abinav\Leave_management_system_30_01_2012\LeaveManagementSystem_30_01_2012\LeaveManagementSystem_30_01_2012' is not a valid virtual path.
Posted

1 solution

Hi,

Try the below code,

C#
Configuration configurationFile = WebConfigurationManager
    .OpenWebConfiguration("~/web.config");
MailSettingsSectionGroup mailSettings = configurationFile
    .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;
}


or can also use,

Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);



Hope this helps.
 
Share this answer
 
v2

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