Tuesday, July 21, 2009

How to configure Email settings in web config?

< system.net>
< mailSettings>
< smtp from="feedback@dotnetkanna.com" >
< network host="smtp.dotnetkanna.com" defaultCredentials ="true" userName="feedback@dotnetkanna.com" port="25" password="12345" />
< /smtp>
< /mailSettings >
< /system.net>

To Access, this SMTP Mail Setting Programatically, you need to import below namespaces:

using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;

The .NET Framework provides APIs for accessing settings in a configuration file. Below is how you access the SMTP mail settings of a web.config file in code:

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
Response.Write("host: " + settings.Smtp.Network.Host + "< br />");
Response.Write("port: " + settings.Smtp.Network.Port + "< br />");
Response.Write("Username: " + settings.Smtp.Network.UserName + "< br />");
Response.Write("Password: " + settings.Smtp.Network.Password + "< br />");
Response.Write("from: " + settings.Smtp.From + "< br />");

No comments:

 
Feedback Form