ASP.NET 2.0 SMTP Configuration

Sunday, April 16, 2006 12:43:28 PM
Rate this Content 1 Votes

I've been working on the MembershipProvider for mojoPortal and trying out the new PasswordRecovery control that hooks into the Membership provider and handles emailing the password depending on whether that is allowed by the settings.

In order to use this control you have to have an smtp server configured in your web.config file. Unfortunately its very hard to find information on setting this up correctly and what information I have been able to find with google has been incorrect because apparently things changed in how this is supposed to be done since the beta versions of  2.0 .NET. Even the book I got (Apress Pro ASP.NET 2.0 in C# 2005) has it wrong probably because they were working on the book while 2.0 .NET was still in beta.

What the book says and what I mostly found with google was like this:

This is apparently how it worked in Beta 1 of 2.0 .NET
But in the release version this throws an error:
Unrecognized configuration section system.web/smtpMail

There was nothing much I could find on MSDN but finally after much googling I found this :


Which was apparently the way to do it in Beta 2 of 2.0 .NET but I was getting this error:
Unrecognized attribute 'from'. Note that attribute names are case-sensitive

But finally after some trial and error I figured out that you have to put the from attribute on the smtp element like this:


Hope this helps anyone running into the same problem and unable to find good documentation.

You can see for youself that the documentation on MSDN for system.net config section doesn't even mention the mailSettings but this configuration works.

Copyright 2003-2010 Joe Audette
Share This Using Popular Bookmarking Services

re: ASP.NET 2.0 SMTP Configuration

Monday, April 17, 2006 4:29:01 AM Carl Olsen

Hi Joe,

I worked on the same section of the book (Password Recovery) this weekend and came up with something similar to what you have.

<configuration>
 <system.net>
  <mailSettings>
   <smtp from=noreply@yourdomain.com>
    <network
      host="localhost"
      port="25"
      defaultCredentials="true" />
   </smtp>
  </mailSettings>
 </system.net>
</configuration>

re: ASP.NET 2.0 SMTP Configuration

Monday, April 17, 2006 6:48:56 AM Joe Audette
Hi Carl,

Thats interesting!

Where did you find info on the defaultCredentials attribute?

I haven't seen that one before. I wonder if there are any others i don't know about yet.

Cheers,

Joe

re: ASP.NET 2.0 SMTP Configuration

Tuesday, April 18, 2006 6:59:59 PM Tom
Hi, Joe
Everything is documented in .Net Framework 2.0 SDK documentation. You can search for 'mailSettings'.

 HTH, Tom

re: ASP.NET 2.0 SMTP Configuration

Wednesday, April 19, 2006 4:19:45 AM Joe Audette
Thanks Tom! You are right.

I just found it here http://msdn2.microsoft.com/en-us/library/ms164240(VS.80).aspx

However in order to search for mailSettings you have to already know what to search for. Initially I was searching for smtpMail.

I've also discovered since my initial post that you can use the Web Administration Tool to configure smtp for a web site. Of course all it does is make the same settings in the web.config

re: ASP.NET 2.0 SMTP Configuration

Friday, April 21, 2006 7:08:58 AM Michael Appelmans
I too have been searching hi and lo for details on this. I note that in the link above although the attributes are presented there is no explanation of them. For instance how does one define "defaultCredentials"? Does anyone have an idea?

Thanks.

re: ASP.NET 2.0 SMTP Configuration

Thursday, May 04, 2006 2:09:00 PM Jaguar
this is very helpful, im just about to write my own module for this purpose. glad i found it and avoid possible headaches.
thanks :)

re: ASP.NET 2.0 SMTP Configuration

Wednesday, May 24, 2006 4:48:29 AM Amichai Sichel

I've found your article just on time.

Thanks a-lot!

re: ASP.NET 2.0 SMTP Configuration

Wednesday, May 31, 2006 7:06:36 AM Brad

I was having problems with passwordrecovery not finding the smtphost but not anymore!

 

Thanks for the solution.

re: ASP.NET 2.0 SMTP Configuration

Thursday, July 13, 2006 10:19:05 PM Benedict

Another hapless soul thanking you for saving me untold hours banging my head against this.  Thanks!

re: ASP.NET 2.0 SMTP Configuration

Saturday, October 21, 2006 11:18:49 AM unknown
I am also using built-in PassowrdRecovery and finally made it work so the e-mail message was sent to user. However in the message (for which I have specified a text body) I do not see the passowrd being attached. It is just the text I specified.  I am storing password in the Clear format ao according to the book I am using (from Apress) I should be seeing the message body (my text file) + some information with user password.  So what else I am missing?

re: ASP.NET 2.0 SMTP Configuration

Saturday, October 21, 2006 11:20:02 AM rcupryjak
I am also using built-in PassowrdRecovery and finally made it work so the e-mail message was sent to user. However in the message (for which I have specified a text body) I do not see the passowrd being attached. It is just the text I specified.  I am storing password in the Clear format ao according to the book I am using (from Apress) I should be seeing the message body (my text file) + some information with user password.  So what else I am missing?

re: ASP.NET 2.0 SMTP Configuration

Wednesday, January 03, 2007 12:50:45 PM harveysburger
Let's assume the application is relying on multiple SMTP servers, let's say one is used for external communication and the other for internal communication, can we use this mailSettings thing? I have been googling this for over an hour and can't seem to find anything on this. It appears I will have to write my own thing...

I tried things like...
  <mailSettings>
   <smtp from="postmaster@smtp.net">
    <firstnetwork host="smtp1.test.com" password="pwdtest" userName="usertest" />
    <secondnetwork host="smtp1.test.com" password="pwdtest" userName="usertest" />
   </smtp>
  </mailSettings>

but System.Net.Configuration.SmtpSection throws exceptions saying that my config section is invalid... which is normal i guess,


am I missing something??

re: ASP.NET 2.0 SMTP Configuration

Tuesday, March 20, 2007 6:13:22 PM unknown
Comments are closed on this post.
Donate Money to support the mojoPortal Project. View Joe Audette's profile on LinkedIn View Joe Audette's profile on The Guild of Accessible Web Designers site