Accessing SMTP Mail Settings defined in Web.Config File Programmatically

I needed to read my SMTP email settings defined under system.net section in my web.config file. In order to use eNewsLetter and other SiteAdmin CMS modules that sending email notifications; you can setup your web.config to defind SMTP services settings.

Below is one example of SMTP email setting defined in web.config file:
(Under <configuration> Section)

    <system.net>
        <mailSettings>
            <smtp deliveryMethod="Network" from="testuser@domail.com">
                <network defaultCredentials="true" host="localhost" port="25" userName="kaushal" password="testPassword"/>
            </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 />");

Thats it!

Comments

# Accessing Web.Config File SMTP Mail Settings Programmatically &laquo; KaushaL.NET

Pingback from  Accessing Web.Config File SMTP Mail Settings Programmatically « KaushaL.NET

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Friday, August 14, 2009 5:24 AM by Shane

Great solution which worked for me on my internal invironment, but when I switch to Medium trust level, I get a SecurityException 'Request for the permission of type 'System.Security.Permissions.RegistryPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.'

on the line 'Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

'

Any Idea how to get around this?

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Friday, August 21, 2009 12:37 PM by Vera-Alfons

Damn, that sound's so easy if you think about it.

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Wednesday, September 09, 2009 7:38 PM by Roulette Spielen

This brings me to an idea:...

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Wednesday, September 09, 2009 8:12 PM by Martin-Alfons

This brings me to an idea:...

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Sunday, September 13, 2009 5:11 PM by masood virk

good

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Monday, September 14, 2009 12:10 PM by kickJohn

every dofuser needs <a href=http://www.bawwgt.com>dofus kamas</a>, but most of them can't find a good <a href=http://www.bawwgt.com>cheap dofus kamas</a> provider, so it's a problem is they want to <a href=http://www.bawwgt.com>buy cheap dofus kamas</a>.now bawwgt will be your best choice on <a href=http://www.bawwgt.com>dofus</a>

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Friday, September 18, 2009 9:17 AM by Barack Obama

I stand here today humbled by the task before <a href=http://www.bawwgt.com>dofus kamas</a>, grateful for the trust you have bestowed, mindful of the sacrifices borne by our <a href=http://www.bawwgt.com>cheap dofus kamas</a>. I thank President <a href=http://www.bawwgt.com>dofus</a> for his service to <a href=http://www.bawwgt.com>buy dofus kamas</a>, as well as the generosity and cooperation he has shown throughout this transition.

# re: Accessing SMTP Mail Settings defined in Web.Config File Programmatically

Sunday, September 20, 2009 7:49 AM by Barack Obama

I stand here today humbled by the task before <a href=http://www.bawwgt.com>dofus kamas</a>, grateful for the trust you have bestowed, mindful of the sacrifices borne by our <a href=http://www.bawwgt.com>cheap dofus kamas</a>. I thank President <a href=http://www.bawwgt.com>dofus</a> for his service to <a href=http://www.bawwgt.com>buy dofus kamas</a>, as well as the generosity and cooperation he has shown throughout this transition.