HOW TO: Accessing Config File Mail Settings Programmatically

Rate It (1)

Last post 04-05-2008 1:57 PM by TATWORTH. 24 replies.

Sort Posts:

  • HOW TO: Accessing Config File Mail Settings Programmatically

    10-22-2006, 6:15 PM

    Originally from: http://tinyurl.com/y4y5lw 

    The .NET Framework 2.0 provides APIs for accessing settings in a configuration file. Here's how you access the mail settings of a config file 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
    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    10-23-2006, 2:31 AM

    Sirz,

    This is excellent codez. Thank you so much.

    Filed under:
  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    11-06-2006, 11:35 AM

    I agree, this is an excellent post. Thanks for the info.

    Cheers, 

    Rick

     

    Specializing in ASP.NET 2.0
  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    12-07-2006, 9:21 AM
    • Loading...
    • porov
    • Joined on 02-01-2006, 8:39 PM
    • Posts 57
    Nice how-to, great job, even tough i use vb
    Free Web Hosting - My asp.net website
  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    12-15-2006, 2:56 AM
    Yes gr8 indeed...
    .NET and Java planets
  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    03-31-2007, 11:20 PM

    Okay, when I do this:

    Configuration

    configurationFile = WebConfigurationManager.OpenWebConfiguration("Web.config");

    The error I got is:

    The relative virtual path 'Web.config' is not allowed here.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: The relative virtual path 'Web.config' is not allowed here.

    Any help is appreciated. 

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    04-10-2007, 10:28 AM
    • Loading...
    • tejssidhu
    • Joined on 04-24-2006, 12:50 PM
    • Posts 10

    Try using

    configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath)

     

    this will get the path to the applications web.config file.

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    04-10-2007, 11:08 AM

    Thanks! I will give that a try. However, it seems that if I do this:

    configurationFile = WebConfigurationManager.OpenWebConfiguration("/web.config")

    then it works. So I'm not sure why but it's working now.

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    05-01-2007, 3:45 PM
    • Loading...
    • slcosta
    • Joined on 01-17-2007, 7:00 PM
    • Posts 3

    As far as I can tell, OpenWebConfiguration() doesn't work when trust level is set to medium, which is what I'm locked into on my Godaddy shared hosting. It gets FileIOPermission errors.

     I'm trying to send mail from and to the address defined in this section of my web.config file:

    <

    system.net requirePermission="false"> <!-- I've tried it with and without this setting -->

    <

    mailSettings requirePermission="false">

    <

    smtp from="who@myhost.com">

    <

    network host="mail.myhost.net" userName="xxxxx" password="xxxx"/>

    </

    smtp>

    </

    mailSettings>

    </

    system.net>

    In addition to OpenWebConfiguration(), I've also tried:

    WebConfigurationManager.GetSection("system.net/mailSettings/smtp")  // (Which returns a null object)

    I've been researching this for several days, and can't find anything that works under medium trust. The only way I've been able to control the address is to hard code it into my program.

    Can anybody shed any light on this problem?  Is there some other way to send email that automatically sets the from or to address to the mailSettings/smtp from   setting?

    / Steve

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    08-28-2007, 8:18 AM
    • Loading...
    • roozbehtk
    • Joined on 05-12-2007, 10:20 PM
    • Posts 5

    Actually when you use the SmtpClient class to send your emails, all you need to do is to add following lines to your web.config file.

    <system.net>

         <mailSettings>
               <smtp deliveryMethod="Network" from="xxxxx@yyyyy.zzz">
                    <network host="smtpout.secureserver.net" userName="mmmm@nnnn.ppp" password="kkkkkkkkkkkk" />
               </smtp>
          </mailSettings>
     </system.net>

    You don't need to explicitly retrieve info from your web.confg file.

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    09-07-2007, 3:44 AM

    I am Agree... Good Post

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    10-01-2007, 5:50 PM

    hi..

    my problem is that "mail sending function" doesn't work on http://www.vwdhosting.net .. i don't know why!! though it works fine "locally"

     

    I  use it to recover password . It works fine "locally" with the following web.config settings: <system.net>

    <mailSettings>

    <smtp from="myname@gmail.com" deliveryMethod="Network">

    <network host="smtp.gmail.com" port="587" userName="myname@gmail.com" password="mypassword"></network>

    </smtp>

    </mailSettings>

    </system.net>

    **************************** 

    recoverPassword.aspx.vb

    *****************************

    Imports System.Net.Mail

    Partial Class recoverPassowrd

    Inherits System.Web.UI.PageProtected Sub PasswordRecovery1_SendingMail(ByVal sender As Object, ByVal e As MailMessageEventArgs)

     

    Dim smtpClient As New Net.Mail.SmtpClient()

    smtpClient.EnableSsl = True

    smtpClient.Send(e.Message)

    e.Cancel =
    True

    End Sub

    End Class

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    12-28-2007, 7:16 AM
    • Loading...
    • sandipgt
    • Joined on 12-20-2007, 6:21 AM
    • Posts 7

     this is realy helpfull code snipt

     

     

    tank you very much 

  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    12-28-2007, 7:52 AM
    • Loading...
    • TATWORTH
    • Joined on 02-04-2003, 1:34 PM
    • England
    • Posts 5,094

     An excellent tip and bi-lingual as well!

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: HOW TO: Accessing Config File Mail Settings Programmatically

    01-18-2008, 3:16 AM

     Very Good Effort

    Muhammad Usman
    Please remember to click "Mark as Answer" on this post if it helped you.
    www.usman-bhatti.blogspot.com