Details |
![]() |
QuickDocId | 505 |
Written by | Imar Spaanjaars |
Posted | 11/28/2009 20:54 |
Page views | 23796 |
Are you looking to hire an experienced software developer or .NET consultant? Then get in touch with me through my company's web site at devierkoeden.com
Found an interesting article on this site? Got inspired by something you read here? Then consider making a donation with PayPal.
You typically don't have to write a lot of code when you want to send an e-mail from one of the Login controls if your mail server doesn't require SSL or authentication. For example, for the CreateUserWizard to send an e-mail with an account confirmation message, you need to do three things:
Here's the definition for the CreateUserWizard control:
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server"> <MailDefinition BodyFileName="~/App_Data/CreateUserWizard.txt" Subject="Your new account"> </MailDefinition> <WizardSteps> <asp:CreateUserWizardStep runat="server" /> <asp:CompleteWizardStep runat="server" /> </WizardSteps> </asp:CreateUserWizard>
The CreateUserWizard.txt file can contain text welcoming the user and can optionally contain the user's user name and password:
Dear <%UserName%>, Thank you for signing up for an account at our web site. For your reference, please find your login details below: User name: <%UserName%> Password: <%Password%>
The final step is configuring the mail server in the web.config file. If you can use your ISP's mail server or you have a mail server on your local network, you typically don't need more than this:
<system.net> <mailSettings> <smtp deliveryMethod="Network" from="You@YourProvider.com"> <network host="smtp.YourProvider.com"/> </smtp> </mailSettings> </system.net>
Now, whenever a user signs up for an account using the CreateUserWizard control, an e-mail that contains the user's user name and password is sent using the mail server defined in the config file. This all works fine if your mail server doesn't require SSL. When it does, for example because you are using GMail's SMTP server, you need to write a bit more code.
When sending an e-mail message with your own code to a mail server that requires SSL, you need to set the EnableSsl property of the SmtpClient class you're using to true. However, when using the Login controls, you don't create an instance of SmtpClient yourself so you can't easily enable SSL. Internally, the CreateUserWizard (and other controls) create an SmtpClient with this setting to false, resulting in errors when trying to send the e-mail. To work around this, follow these steps:
For the first step, you need to add something like this to your web.config file:
<system.net> <mailSettings> <smtp deliveryMethod="Network" from="YourAccount@gmail.com"> <network port="587" userName="YourAccount@gmail.com" password="YourPassword" host="smtp.gmail.com" /> </smtp> </mailSettings> </system.net>
You need to fill in your GMail's user name and password in the respective attributes.
The next step is to handle the SendingMail event which is fired by the following three Login controls:
You can handle the SendingMail event that these controls fire with the following code:
protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e) { SmtpClient mySmtpClient = new SmtpClient(); mySmtpClient.EnableSsl = true; mySmtpClient.Send(e.Message); e.Cancel = true; }
This code first creates a new SmtpClient. When this client needs to send out an e-mail, it will use the details provided in the web.config file, including the user name, password and configured mail server.
Next, it enables SSL by setting the EnableSsl property to true. It then accesses the message that is being sent by the Login controls using e.Message which is then passed to the client's Send method. Finally, to stop the control from still sending its own message using the configured mail server without enabling SSL, the code calls e.Cancel = true. Since this code is the same for all three controls, you could create a helper method with this code so you only need to write it once.
This solution is not as convenient as an EnableSsl attribute on the network element in web.config would be, but it doesn't require a whole lot of code to make it work. In the code download that comes with this article, you find a sample page for each of the three Login controls that support sending mail.
Wonder where to go next?
You can read existing comments below
or you can post a comment yourself on this article
.
Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.
I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.
When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.
If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.
For more information about the Talk Back feature, check out this news item.
QuickDocId | 505 |
Full URL | http://imar.spaanjaars.com/505/using-the-aspnet-login-controls-with-mail-servers-that-require-ssl |
Short cut | http://imar.spaanjaars.com/505/ |
Written by | Imar Spaanjaars |
Date Posted | 11/28/2009 20:54 |