Welcome to Strong Coders Community | | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Accessing Config File Mail Settings Programmatically

Some people may not know that .NET 2.0 provides APIs for accessing everything in a configuration file. The most common question I see relates to accessing the mail settings in the system.net node programmatically.

C#

using System.Configuration;

using System.Web.Configuration;

using System.Net.Configuration;

 

Configuration configurationFile = WebConfigurationManager .OpenWebConfiguration( "PathToConfigFile" );

 

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;

}

VB.NET

Imports System.Configuration

Imports System.Web.Configuration

Imports System.Net.Configuration

 

Dim configurationFile As Configuration = WebConfigurationManager.OpenWebConfiguration( "PathToConfigFile" )

 

Dim mailSettings As MailSettingsSectionGroup = configurationFile.GetSectionGroup( "system.net/mailSettings" )

 

If Not mailSettings Is Nothing Then

    Dim port As Integer = mailSettings.Smtp.Network.Port

    Dim host As String = mailSettings.Smtp.Network.Host

    Dim password As String = mailSettings.Smtp.Network.Password

    Dim username As String = mailSettings.Smtp.Network.UserName

End If

Share this post: | | | | |
Posted: Saturday, March 25, 2006 9:49 AMby Ryan Olshan
Filed under: , ,

Comments

Larry said:

Question: Is there a way to force it to load mail settings from web.config without having to specify the exact path?  
April 9, 2006 5:36 PM

Ryan Olshan said:

Hi Larry,

If you specify the SMTP server settings in Web.config, ASP.NET will automatically use them when you send an email.

April 9, 2006 9:50 PM
New Comments to this post are disabled