How to programmatically retrieve SMT server details from web.config

I have the following SMTP data stored in web.config

<system.net> <mailSettings> <smtp from="isds@ixtent.com"> <network host="mail.domain.com" port="25" userName="username" password="password" defaultCredentials="true"/> </smtp> </mailSettings> </system.net> 

How to get these values ​​from a C # class.

+7
c # web-config
source share
3 answers
 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; } 
+18
source share

If you need to send an email with the data of this mail server, you do not need to read the settings and apply them. These settings are applied implicitly in the application.

If you read it for any other reason, I was going to write something similar to Darin's answer. But just as I wrote, I found that he answered like that, please refer to his answer if you really need to read. :)

+1
source share

What about:

 string fullpath = @"C:\FullPath\YourFile.config"; string configSection = "system.net/mailSettings"; Configuration config = ConfigurationManager.OpenExeConfiguration(fullpath); MailSettingsSectionGroup settings = config.GetSectionGroup(configSection) as MailSettingsSectionGroup; 
0
source share

All Articles