How do I get the SMTP email address from web.config?

G

Guest

I've tried:
MailSettingsSectionGroup mailConfig =
WebConfigurationManager.GetSection("system.net/mailSettings") as
MailSettingsSectionGroup;

and:
Configuration configurationFile =
WebConfigurationManager.OpenWebConfiguration("/Web.Config");
MailSettingsSectionGroup mailConfig;
if (configurationFile != null)
mailConfig = configurationFile.GetSectionGroup("system.net/mailSettings")
as MailSettingsSectionGroup;

The first method returns null and the second returns an object but it does
not have the host or from properties set.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
G

Guest

Hi,
First write below code in web.config </system.web> and before
</configuration>:
<system.net >
<mailSettings >
<smtp from="(e-mail address removed)">
<network host="mail.myhost.net" userName="xxxxx" password="xxxx"/>
</smtp>
</mailSettings>
</system.net >

Then try this code:
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;

lblHost.Text = host;
lblUserName.Text = username;
lblPassword.Text = password;
}
Also include following namespaces:
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top