Nuget Goodness

MVC Confirmation Email – Nuget

by Kevin on October 8, 2011

install

Problem

I’ve received several requests to update my original post dealing with adding a confirmation step to the standard ASP.NET MVC registration process so that it can work with ASP.NET MVC3. This seemed like a perfect place to use (and learn) Nuget, so you don’t have to deal with all the steps of creating the methods, modifying the views, etc.

Solution

For now, I’m going to assume that you already have Nuget installed and you have some idea of how to use it. If you don’t, read through the instructions at the Nuget site, or check out blog posts by Scott Hanselman or Phil Haack. If you still need help, post something in the comments and I’ll expand this post to include a Getting Started section.

Step 1a – Create a New Project

In Visual Studio, create a new ASP.NET MVC 3 Internet Application.

createnewproject

Step 1b – Work with an Existing Project

This will work with an existing project, but I’m assuming that you’re using default everything. If you’ve modified the AccountController or AccountModels in an uber custom way, you’ll likely experience pain. If you’re using out-of-the-box functionality, you won’t have any issues. And by “won’t”, I mean you’re completely on your own.

Step 2 – A little preparation

Now that you got your project, rename Controllers/AccountController.cs and Views/Account/LogOn.cshtml by adding “.old” to each file.

rename

This needs to be done because Nuget cannot modify existing files (or at least I don’t know how to do it). These files are included in the Nuget package, and the default behavior of Nuget is to ignore any files that it finds on disk that match files in the package. By renaming them, if you have any custom code in there, you won’t lose it – you’ll just need to port it over to the new files.

Step 3 – Installing the package

A. From the console:

If you’re a fan of the console, Select Tools | Library Package Manager | Package Manager Console. At the console prompt type: install-package MvcConfirmationEmail. Nuget will download the package and modify your project accordingly.

Console

B. From the User Interface:

Right-click on your project root, and select “Manage Nuget Packages…”. When the dialog window pops-up, enter “ConfirmationEmail” (one word) in the search text box in the upper right corner.

gui

After a few seconds, it will find the MvcConfirmationEmail package (shown above). Click the install button, and it will download and install the package into your project.

WARNING!

If you didn’t rename the files as noted in Step 2 – this won’t work, and you will need to uninstall the package (PM>uninstall-package MvcConfirmationEmail), rename the files, and do this again.

Step 4 – Update web.config

When Nuget installed the package, a few additions were made to web.config so that you can configure your email service provider’s settings.

A. Email From Name

When you send an email to someone during the registration process, it has to come from a named account. Open up web.config and find the configuration\appSettings section. There is a key named MvcConfirmationEmailFromAccount. Change the value to the account name that will be sending the email. I’ve been using Gmail as my test account and it works great.

B.  Mail Settings

You need to configure the settings for your email service provider. Head down to the bottom of the config file, and you’ll find the system.net\mailSettings\smtp node. Set the appropriate values for host, userName, password, and port, Gmail requires port 587 for SMTP – if you’re not using Gmail – you will need to check with your service provider to see what this should be.

That’s it! You should now be able to run the application and have the same functionality/workflow available in the original post. If you run into any problems or have any questions, please post down in the comments section and I’ll be happy to walk through your issues.

UPDATE 10/18/2011: I created a sample site on AppHarbor that you can use to test the functionality/workflow. http://confirm.apphb.com.

{ 19 comments… read them below or add one }

jc October 13, 2011 at 7:07 pm

This was exactly what I was looking for:
Everything is working for me except when I click on the confirmation link in the perspective users recieved email, it returns in the browser as “could not connect to mypage.apphb.com:14978″.

As I am hosting on AppHarbor, could this may be the reason? Any pointers would be very much appreciated.
Many thanks for the turtorial Jc

Reply

Kevin October 17, 2011 at 9:46 am

JC -

Hmmm… I haven’t tried this with appharbor yet, but it should work. If you’d like I could send you an email and we could do more troubleshooting off-line.

Regards,

Kevin

Reply

jc October 17, 2011 at 1:38 pm

Hi Kevin,

Yes please, that would be terrific :)

Thank you v. much
Jc

Reply

Kevin October 18, 2011 at 8:09 am

JC –

I just created a test app on appharbor and I got the same error. When the confirmation url is built, it includes a port number… so the confirmation link I received was “http://confirm.apphb.com:14715/account/verify?ID=0e6a77f6-ab9d-4530-b253-76673da76ded”. When I removed the port number I ended up with http://confirm.apphb.com/account/verify?ID=0e6a77f6-ab9d-4530-b253-76673da76ded – and that WORKED. I need to look at the code that’s building the url and make sure it’s not including the port.

For now, you can got into the SendConfirmationEmail method and change the line:
string verifyUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + “/account/verify?ID=” + confirmationGuid;

TO:

string verifyUrl = “http://mypage.apphb.com” + “/account/verify?ID=” + confirmationGuid;

WHERE: “http://mypage.apphb.com” is the name of your appharbor application. Mine would be “http://confirm.apphb.com”.

This isn’t ideal (as it won’t work locally)… and I’ll dig into it more tonight.

Regards,

Kevin

Reply

Kevin October 18, 2011 at 11:37 pm

JC –

Based on this support note on AppHarbor http://support.appharbor.com/kb/getting-started/workaround-for-generating-absolute-urls-without-port-number I just upgraded the package to v1.1. You can either create a new project and run ‘Install-Package MvcConfirmationEmail’ – or run ‘Update-Package MvcConfirmationEmail’ in a v1.0 project. Thank you for pointing this out! I tested that it works locally, as well as when deployed to AppHarbor. Please let me know if you have any questions.

Regards,

Kevin

jc October 19, 2011 at 6:51 pm

Hi Kevin,

You are a star. I simply updated to 1.1 and bobs your uncle.

Brilliant brilliant brilliant. I would have never figured that out.

Brilliant
Thanks again JC

Reply

James Burton November 7, 2011 at 12:46 pm

One approach that I’m trying out that could be useful for you is to implement AccountController as a partial class, then you only need to add the partial keyword and remove excess methods from the existing controller, whilst including the controller methods for this as AccountController.MvcConfirmation.cs.pp (NB: use the substitutions to match the namespace) instead of calling it AccountController.cs.

Reply

ProgTick November 29, 2011 at 12:47 am

Is this nuget still there? It didn’t come up in my search.

Reply

Kevin November 29, 2011 at 12:58 am

ProgTick -

It’s there… if you go to the Nuget Package Manager Console, you can type in “PM> Install-Package MvcConfirmationEmail” (minus the quotes). The search on nuget.org is not very good – but it’s there… http://nuget.org/list/packages/mvcconfirmationemail.

I updated the tags on the package… so you should now search for ConfirmationEmail (no spaces)… and their indexing will find it better. Thanks for finding this issue!

Regards,

Kevin

Reply

Yung March 19, 2012 at 10:10 pm

I tried to register myself on your test app and I just get the following msg,
“Sorry, an error occurred while processing your request.”
I also did not receive a confirmation email. Do you have any idea what’s going on?

thanks,
Yung

Reply

Kevin March 19, 2012 at 11:10 pm

Hi Yung -

Sorry about that! My password had expired… so I created a new gmail account and reset the credentials. I just tested it and it’s working now. I deleted your email address from the server so you should be able to retry it now. I guess I should pay more attention to this :).

Regards,

Kevin

Reply

Vincenz May 21, 2012 at 9:30 am

Hi Kevin,

I’m using your approach with MVC 4 Beta and I got a bad problem. When I register with my app the confirmation email is sent and all is working well. When I register a new user with the same email address theconfirmation mail isn’t sent at all, eventhough it passes the client.Send(message) in debug without any errors. Why am I not able to test the functionality twice with the same email address?

Regards,

Vincenz

Reply

Vincenz May 21, 2012 at 9:43 am

Well, thats embarrassing, it was all my junk email folder. :D

Reply

Kevin May 21, 2012 at 5:37 pm

Hi Vincenz – been there, done that! Glad you’ve got it working. Any issues with MVC4?

Reply

Steve June 24, 2012 at 1:15 pm

This worked out really well. Thanks! Now I’m trying to add the ChangePassword behavior. I’m still learning this, so I’ll bang my head on it a while, but if anyone wants to post their solution, I’d appreciate it.

Reply

Steve June 24, 2012 at 7:07 pm

Where is the phrase “Thank you for registering. Please check your email for a confirmation message with a link that will confirm your account. Once you click the link, your registration will be complete.” buried in the code? Has to be obvious but I can’t find it.

Reply

John V July 25, 2013 at 11:24 am

I’d like to use this in an MVC4 project, but it appears the AccountController is set up much differently by default. I could probably sort out the differences, but I was looking for something that would save me some time.

Any chance you’ve done an MVC4 version and I just haven’t found it yet?

Reply

Olsman January 12, 2014 at 1:49 pm

Hello Kevin,

I tried a couple of different approaches to implement your NUget code into the “default” MVC4 project in Visual Studio 2013. Unfortunatelly I was not able to get the code working, I have to impression that quite a few things have been changed in the “default” template. The is for instance no “LogOn.cshtml” available instead there is a “Login.cshtml”. Also there is no ChangePasswordModel. By default I get 5 error messages, in short on ChangePasswordModel, LogOnModel, RegisterModel, unassigned local variables changePasswordSucceeded and creatStatus. At this moment too many errors for me to handle, so my question to you is, could you have a look at this? Am I doing things wrong or should your code be modified in order to use it in the new version of MVC(4)? I also downloaded your project made in VS2012, which also gives an error compiling it to VS2013.

Reply

Kevin January 12, 2014 at 3:33 pm

Hi Olsman – take a look at this… it looks like this has been updated for MVC4 – I’ll take a deeper look tonight.

Reply

Leave a Comment

{ 1 trackback }

Previous post: