Wednesday 5 September 2007

CreateUserWizard - SendingMail event

After following the examples in the book, I figured I wanted to add some extra functionality to the Membership and UserProfiling section.

While it allows your users to signup smoothly and simply, using a CreateUserWizard, I wanted to add an option where my site sends the user a link with a GUID embedded in it, in order to verify that the user didn't only fill in a properly formatted E-mail address, but actually does own the mail address that was entered.

I went off on my merry way, using a code snippet from my local installation of MSDN as a basis:



e.Message.Body.Replace("<%PasswordQuestion%>", Createuserwizard1.Question);
e.Message.Body.Replace("<%PasswordAnswer%>", Createuserwizard1.Answer);


So my code became:



protected void SendMail(object sender, MailMessageEventArgs e)
{
// Generate a GUID
string guidResult = System.Guid.NewGuid().ToString();

// Code to paste into a URL, which I cannot paste cause Blogger throws a fit.

e.Message.IsBodyHtml = true;
e.Message.Body.Replace("<%Link%>", url);
}


After running the code, nothing happened. I set a breakpoint and tried again. Nothing. I commented out several options that the book carried as extra steps that I thought might interfere. Still nothing.

I then tried hooking the UserCreated event to the SendMail event that I defined, but ran into trouble when trying to create EmailEventargs to pass as a parameter.

And luckily, then I googled on "CreateUserWizard MailMessageEventArgs", and came across Bryant's blog.

The last line of code now reads:



e.Message.Body = e.Message.Body.Replace("<%Link%>", url);


And lo and behold, problem solved, and I find a neat link included in my mail :)

Bryant, I owe you a beer!

And to those still unconvinced by the Internet, I predict that some day it will be a big hit!

2 comments:

Unknown said...
This comment has been removed by the author.
Anonymous said...

Hi Peter,

I've seen your solution to verify a users email for TheBeerHouse on the Wrox forum at this link:

http://p2p.wrox.com/book-asp-net-2-0-website-programming-problem-design-solution-isbn-978-0-7645-8464-0/66805-confirming-new-registration-via-email-response-2.html

I am clueless when it comes to Sql. Any chance of you explaining the stored proceedures and so forth?

Thanks,

Cal