Saturday, February 19, 2011

SMTP mail setting in web.config file?

Example to define email setting in web.config file:

<system.net>
        <mailSettings>
            <smtp deliveryMethod="Network" from="testuser@example.com">
                <network defaultCredentials="true" host="localhost" port="25" userName="testUser" password="testPassword"/>
            </smtp>
        </mailSettings>
    </system.net>


SMTP mail setting using code:

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

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 />");

Enjoy.....

No comments:

Post a Comment