Programmatically Populating the "mail.settings" section of "system.net" in web.config

I am fully aware that the system.net section of web.config typically looks something like this:

  <system.net>
    <mailSettings>
      <smtp from="noreply@sample.com">
        <network host="smtp_server_address" userName="myUsername" password="myPassword" />
      </smtp>
    </mailSettings>
  </system.net>

If web.config is so populated then this info is used for sending e-mails generally and also by the Password Recovery control of the Memberships Login facility.

But I have a situation where the one web app will be installed on several different servers and managed by people with different skill levels.  So rather than getting them to mess around in that mailSettings section I've instead found it better to setup 3 application variables in web.config:

    <add key="SMTPserver" value="smtp_server_address" />
    <add key="SMTPusername" value="myUsername" />
    <add key="SMTPpassword" value="myPassword" />

And have no mailSettings area at all.

The problem I'm experiencing is how to get code within the Password Recovery page to programmatically utilize these application variables.  My first inclination was to just build mailSettings on-the-fly following this guidance: http://forums.asp.net/t/1037680.aspx   But when it came to saving it, it wouldn't allow me to do it.  I suppose this is a security feature.

The other approach is to intercept the "SendingMail" event of Password Recovery such as is described here: http://forums.asp.net/p/1227533/2206498.aspx   It's more convoluted but does work.

I'm curious to learn if anyone has come up with a cleaner solution.

Robert


Robert Werner
Vancouver, BC
www.mwtech.blogspot.com
www.pocketpollster.com/beta
0
rmdw
11/14/2008 3:11:30 AM
asp.net.config-deploy 12495 articles. 0 followers. Follow

4 Replies
1618 Views

Similar Articles

[PageSpeed] 45
Get it on Google Play
Get it on Apple App Store

I seem to have found a solution here: http://www.aspnetpro.com/newsletterarticle/2007/02/asp200702jk_l/asp200702jk_l.asp

I don't know what I was doing wrong before but now the following code works perfectly when placed in Global.asax:

  void Application_Start(object sender, EventArgs e)
  {
    UpdateMailSettings();  // Modifies the MailSettings portion of web.config
  }
 

  void UpdateMailSettings()
  {
    System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    System.Net.Configuration.MailSettingsSectionGroup mailSettings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

    mailSettings.Smtp.Network.Host = BusinessObjects.Tools.GetAppSettingsValue("SMTPserver");
    mailSettings.Smtp.Network.UserName = BusinessObjects.Tools.GetAppSettingsValue("SMTPusername");
    mailSettings.Smtp.Network.Password = BusinessObjects.Tools.GetAppSettingsValue("SMTPpassword");
    config.Save();
  }

 

Before, the code failed at config.Save().  I'm aware that the application restarts when web.config is restarted but that's fine.  What it allows me to do is instruct is instruct the assorted web admins to modify the specific app keys and then everything takes care of itself:

  <appSettings>
    <add key="SMTPserver" value="shawmail.vc.shawcable.net"/>
    <add key="SMTPusername" value=""/>
    <add key="SMTPpassword" value=""/>
  </appSettings>

 

Hope this helps someone else!

Robert


Robert Werner
Vancouver, BC
www.mwtech.blogspot.com
www.pocketpollster.com/beta
0
rmdw
11/14/2008 5:51:08 AM

Hi rmdw,

 You need to read this thread completely http://forums.asp.net/t/1250771.aspx?PageIndex=2
There is same scenario like yours. There is no need to use password recovery built in control. You can write your own. which will fetch the USer Name , Security question , answer or reseet the password etc.

Let me know if you have any query.

Satalaj


Watch The true story of internet ||Yet another forum |||
I'm big fan of Open source flash chart for .net very very simple
I n t e r v i e w T i p s
0
satalaj
11/14/2008 12:36:30 PM

Satalaj,

Thank you for responding.  Yes, I'm aware that I could build my own Login facility.  And I did so with ASP.Net 1.0.  But why would I do so now?  I'd love to hear the opinions of other developers on this.

As my previous post indicated, I added some simple code to Global.asax and now I have the Password Recovery control working perfectly!

Robert


Robert Werner
Vancouver, BC
www.mwtech.blogspot.com
www.pocketpollster.com/beta
0
rmdw
11/14/2008 7:12:11 PM

Hi,

As far as I know, your solutions are fine. We can either use PasswordRecovery's SendingMail event to send email or modify web.config in Application_Start event.

 

Thanks.


Thomas Sun
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Thomas
11/17/2008 7:55:06 AM
Reply:

Similar Artilces:

Encrypting "connection strings" and "mail settings" in web.config
I'm trying to encrypt two sections of my web.config.  Encryption works fine when I try encrypting either <connectionStrings> or <system.net/mailSettings/smtp> alone.  But when I try and do both together, I get an error and only the <connectionStrings> section encrypts properly.  Here's the error:Parser Error Message: The configuration file has been changed by another program.Source Error: [No relevant source lines] Here's the code I'm using . . .protected void EncryptConfig(bool bEncrypt) {obje...

"type" attribute of "Section" element in web.config file
Hi, I want to create my custom section in web.config, but it dosn`t work and no exceptions are thrown My code: namespace Bus {   public class WebConfigurationSection: ConfigurationSection   {     public WebConfigurationSection()     {     }     [ConfigurationProperty("FromMail", IsRequired=true)]     public String FromMail     {       get { return (string)this["FromMail"]; }       set { this["FromMail"] = value; }   &nb...

Difference b/w "web.config" & "Machine.config"??
Difference b/w "web.config" & "Machine.config"??Freelance Web Developer & HTML IntegratorASP.NET(1.1/2.0) == PHP The web.config applies to the web application, machine.config applies to the entire machine.  Did you have a specific question or problem with this?JeffPlease: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue....

Precedence of "where" ("of", "is", "will")?
Nobody on #perl6 today could answer this one. Is: Str | Int where { $_ } the same as: (Str | Int) where { $_ } or: Str | (Int where { $_ }) ? Followup questions, Mr. President: What kind of operators are "where", "of", "is", and "will"? Is there a reason that S03 doesn't list them? What are their precedence(s)? -- Chip Salzenberg - a.k.a. - <chip@pobox.com> Open Source is not an excuse to write fun code then leave the actual work to others. Chip Salzenberg writes: &...

.ALLCOL("%COLUMN%", " ", ", ", ", ")
Do you know anyway for me to exclude a subset of columns returned by this function. We have two columns (rec_user and rec_datetime) which are in all of our tables, but when generating triggers I want automatically generate a script which does not include those two columns but does include all other columns in that table. Bruce I should add that I am using PD 9.0.0.580. Bruce "Bruce Lamb" <lamb.bruce@mayo.edu> wrote in message news:6HgI315nCHA.155@forums.sybase.com... > Do you know anyway for me to exclude a subset of columns returned by this > function. ...

VS.NET 2005 is ignoring in web.config strict="true". I get no error when I try this: Dim x As Integer = "string"
Hello,I have this in my VS.NET 2005 web.config <compilation debug="true" strict="true" explicit="true">When I do this in my vb code:Dim x As Integer = "string"I get no error. When I hit compilation I also get no error. What am I doing wrong?Thanks for helping!Regards,J. Bauer It's a bug of some sort See this response by Scott Guthriehttp://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3c1c9898-8076-4f69-a5e2-e99573d03c6e Unfortunately we don’t have a GUI based way to configure this. L It is something we will make sure gets added back in, though, with the next...

"SSL" "Mail" and "Code"
Does anybody know of any discussions taking place within Mozilla regarding these 3 bits in the certificate manager? Perhaps I've missed something in the discussions here. In any case, I think a new mechanism for indicating trust w/in the Mozilla apps are needed. Take for example the "StartCom Certification Authority" root. The Certificate Manager (I'm using Firefox on Windows) says it can identify web sites, email users, and code yet the cert itself says it can only be used for signing other certs (essentially, that is). At a minimum this is confusing but I have...

quotes, quotes, quotes...
I am getting this error and I know what is causing it, but I have no idea how to fix it, any help would be great. The script steps through the /var/log/messages file on a linux server and puts The entries into a mysql database. However when it gets to the 'hlt' line in the messages file it just barfs. The single quotes are freaking it out. I know about quotes but not how to use in this situation. Thanks, Paul Error: May 27 17:53:00 localhost kernel: Checking 'hlt' instruction... OK. <----- doesn't like this in the messages file DBD::mysql::st exec...

Difference between customErrors mode="On" and "RemoteOnly" in web.config
I am debugging some asp.net code written using .NET 1.0./VS 2003. I noticed that in the web.config filethey use the customErrors attribute to redirect to an error page if an error occurs. This is done bysetting the custom error attribute to "RemoteOnl" Here is the code. <customErrors mode="RemoteOnly" defaultRedirect="error/PropAvailError.aspx"/> I was wondering. What is the difference between setting the custom errors mode to "RemoteOnly" as opposed to "On". What is the difference between the two? Hello, here is a link to the doc ...

"Net use" not working in application that runs as "system"
Hello, I am trying to create an application object to distribute a program which during the setup procedure creates two local users on the workstation. I tried to set up the application to run as an unsecure system user and start the setup.exe. This method worked fine for quite a number of apps in the past. However this app needs a drive mapping to a windows 2003 server to start the setup.exe. Apparently under windows 2003 it is no longer possible to use the "net use" command when running a script as system user. I get system error 1312 "A specified logon ses...

#include "config.h" or #include "parrot/config.h"
In perl5, we've had occasional header file name conflicts over the years. One common example is someone putting a file named "config.h" in /usr/local/include. Other conflicts with "string.h" and "memory.h" are also conceivable. I'd suggest cd parrot mkdir include mkdir include/parrot mv *.h include/parrot changing parrot.h to do #include "parrot/config.h" and then changing Makefile to add -I./include to CCFLAGS. -- Andy Dougherty doughera@lafayette.edu Dept. of Physics Lafayette College, Easton PA 18042...

Is that need to upload "global.asax", "web.config" file to web server?
hi all...here is my problem......... Is that need to upload "global.asax", "web.config" file to web server? let say my asp.net main form is "WebForm1.aspx, after i upload WebForm1.aspx to webserver, Is that need to upload "global.asax", "web.config" or other file to the website? Hi, You need to upload your web.config file. If you are making use of your global.asax, then you need to upload the same. Making use, per say, you are storing some application variables etc., in the global.asax.vb or .cs files. Note that you don't nee...

Get "Mail"/"Get "News" / "Write"/"Post" buttons?
If the "Get Mail" button text changed to "Get News", and "Write" changed to "Post" when TB's in the news-reading mode, it would save me and probably others from constantly forgetting to make the change before proceeding. :-) Or is there an extension to do this which I don't know about? Thanks! Bob Bob P wrote: > If the "Get Mail" button text changed to "Get News", and "Write" > changed to "Post" when TB's in the news-reading mode, it would save me > and probably others...

"Net use" not working in application that runs as "system" #2
Hello, I am trying to create an application object to distribute a program which during the setup procedure creates two local users on the workstation. I tried to set up the application to run as an unsecure system user and start the setup.exe. This method worked fine for quite a number of apps in the past. However this app needs a drive mapping to a windows 2003 server to start the setup.exe. Apparently under windows 2003 it is no longer possible to use the "net use" command when running a script as system user. I get system error 1312 "A specified logon ses...

Web resources about - Programmatically Populating the "mail.settings" section of "system.net" in web.config - asp.net.config-deploy

[...] How to Programmatically Administer Off-Facebook Pages with the Open Graph API - Inside Facebook ...
With the Open Graph protocol that Facebook announced at last week’s f8 conference, any URL can now be treated just like a Facebook Page. After ...

Well, This Is Different Time Inc. Now Selling Print Ads Programmatically
Machines are now selling some print ads at Time Inc. The nation's largest magazine publisher owner of People, Time, Sports Illustrated and InStyle ...

Now Marketers Can Buy Sponsored Content Programmatically
Publishers' branded content is going programmatic. Today, Sharethrough and AppNexus are rolling out real-time bidding for Sharethrough's news ...

Wesabe makes Financial Data available programmatically
Wesabe has just done what I've been trying to convince banks to do for years. They're making financial data available via a simple POX (Plain ...

IPG Begins Buying Local Broadcast Programmatically: Can Target, Retarget Viewers In Near Real-Time
In what likely is the most significant development yet to bring programmatic media-buying to local broadcast television, Interpublic's Mediabrands ...

Google Launches Cloud SQL API To Allow Developers To Manage Their Databases Programmatically
... to use Google’s admin interface. Today, however, Google is launching the Cloud SQL API . This new REST API will allow developers to programmatically ...

Are Programmatically Purchased Native Ads Native Enough?
It seems everyone and their mothers are pushing native ad units and tools to support them. Facebook, Twitter, Yahoo and most recently AOL all ...

Most Major U.S. Publishers Now Sell Ads Programmatically
Most major online publishers in the U.S. now sell at least some of their ad space using automated "programmatic" technology, according to new ...

TV Ads Could Soon Be Sold Programmatically, Magna’s Todd Gordon
The key efficiency new-wave digital media sales holds over TV is clear – super-targeting through addressable, automated and programmatic buying ...

How To Use Facebook's New Power Editor For Ads
Here's a guide to the power editor in Facebook's ad manager.

Resources last updated: 12/22/2015 4:47:59 PM