Twitter Feed Popout byInfofru

Send Welcome email to user using Membership API

While using Membership API, It is a very normal task to send user a registration or verification email. We can achieve that using two ways one is using the default email setting of Create User Control and other is by implementing CreateUserWizard.CreatedUser event

Method 1:

Drag and Drop the create user control, right click then properties and find MailDefination and set appropriate values for each property.

membership_sc1

BodyFileName is basically the location of the file which contains the body of the email. It can be text file or html file.

HTML file which I am using is a very simple html file which can be found in the project source specified at the end of this post.

Create User Control uses the SMTP settings in web.config to send the email that is why before checking it you need to make sure that you have specify the correct settings in configuration file. For example

   1: <system.net>
   2:     <mailSettings>
   3:         <smtp deliveryMethod="Network">
   4:             <network defaultCredentials="true" host="localhost" port="25"/>
   5:             <!--userName="" password="" if required specify after port-->
   6:         </smtp>
   7:     </mailSettings>
   8: </system.net>

That's it your control is now ready to send emails.

Method 2:

In this method, we will manually shoot an email when the user get registered successfully. For that we need to implement our logic in CreatedUser event of this control.

Following is the screen shot of different events provided by CreateUserControl

image

As you can see I have generated a method against CreatedUser and here is the code for that

   1: Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.CreatedUser
   2:     Dim ToAddress As String = CreateUserWizard1.Email
   3:     Dim mm As New MailMessage("Support@mysite.com", ToAddress)
   4:     Using objSr As New StreamReader(Server.MapPath("MailTemplate\\welcome_ver.htm"), FileMode.Open)
   5:         With mm
   6:             .Subject = "Welcome to my site"
   7:             .Body = objSr.ReadToEnd()
   8:             .Body = .Body.Replace("{verification_code}", New Random().Next()) 'Any Random number you can put your logic here...
   9:             .IsBodyHtml = True
  10:         End With
  11:         Dim smtp As New SmtpClient
  12:  
  13:         smtp.Send(mm)
  14:     End Using
  15: End Sub

In this method I am also using the SMTP Settings specified in web.config however, you can change that according to your own need.

In line no 10, I have put a token which will replace the string which I have in my source html file with a random number. For those who are facing difficulty in understanding the token stuff, please see the following html

   1: <html>
   2:  
   3: <body>
   4: Hi, 
   5:  
   6: Thank you for the registration and welcome to my site
   7: Your verification code : {verification_code}
   8: </body>
   9: </html>

That is very simple and very easy. I personally recommend the Method 2 as it can give you some extra control over the process.

Here is the VS 2008 code