Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - How should sending email be handled?

In an ASP.NET MVC application, how should the sending of emails be handled? I've been thinking about this, and I really like the idea of handling it in an MVC-oriented way (it is an MVC application, after all.)

What I mean is that the email that gets delivered to the user is really just a view that should have a model passed to it. This raises some questions: How should we deliver views to both the requesting browser and an SMTP server at the same time, when an action is executed? Does the framework have any facilities to support this? If not, is there some 3rd party release/guidance for this sort of functionality?

The alternatives to treating email sending in an MVC way are:

  • Email sending code shows up in the controller's action methods.
  • Email sending is a behavior of the Model objects.

How are you handling the sending of emails in your ASP.NET MVC applications? What do you think about an MVC-oriented approach?

like image 658
Ronnie Overby Avatar asked Jul 12 '10 19:07

Ronnie Overby


People also ask

Which namespace using can send email in ASP NET MVC?

For sending mail from ASP.NET MVC we use the "System. Net.Mail" namespace.

How can you send an email message from an asp net web page?

First: Edit Your AppStart Page SmtpPort: The port the server will use to send SMTP transactions (emails). EnableSsl: True, if the server should use SSL (Secure Socket Layer) encryption. UserName: The name of the SMTP email account used to send the email. Password: The password of the SMTP email account.

What do you have to do to escape email addresses in razor?

In Razor, `@` symbol is used to transition from HTML to C#. To escape an '@' symbol in razor markup, use two '@' symbols.


2 Answers

Usually when you'd need a new result type, you would program a new MyResult object that would inherit from ActionResult. This way your action method would be free to choose the result type that was indicated, and the View Engine would simply call the ExecuteResult() method on the new result type, which would hold the logic on how to render the data.

However, since what you're doing sounds like you just want to get HTML from a View and email it (it's the same result type as for the web browser), the answers to this question should cover it in depth. Note that the answers with the code for MVC 2.0 are the best way to go, since they added the "render a view to a string" functionality into the second release due to popular demand.

Your question is a good one, as I would highly recommend an MVC approach to any output from your program. Sending a view to email is conceptually no different than sending a view to any other output type (PDF, mobile browser, SMS), and by building pluggable view outputs, you have an easy way to support expansion of the usage of your app.

like image 145
womp Avatar answered Sep 28 '22 04:09

womp


Did you take a look at MvcMailer? See the NuGet package here and the project documentation

Hope it helps!

like image 40
Sohan Avatar answered Sep 28 '22 04:09

Sohan