Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be considered the best way to architect sending email from a C# web application?

I am working on a web application that will be going live soon, and I am now trying to figure out the best way for handling sending email from the application. I understand completely HOW to send email from the application using the MailMessage and SmtpClient classes, however my question is from a different angle. My main purpose at my job before this project was support of old applications that had been developed before my time. In these applications, when they needed to send email, they hard coded any of the messages into the actual message with all of the HTML tags embeded directly into the C# code.

The application that I am working on will have a template for the emails to be sent in, as a sort of styling container, and the different messages will be embeded into the main content div's of the template. I would like to avoid hardcoding these templates in this application, so I have been trying to figure out the best way to layout my project. I have thought of using a t4 template, and reading the different t4's into the application and applying a String.Format with the specified parameters to add names/emails to the messages to be sent. However, I am not sure this is the best way to do it.

My other idea was to define a class for each type of message, however this would end up hardcoding messages again, which as I said I don't want to do.

My question is, how have you approached this in the past? What worked, and what didn't and for what reasons? I have looked all over online, but either the only content out there is on HOW to send the message, or I have not used the right Google power words.

like image 901
tostringtheory Avatar asked Dec 19 '11 00:12

tostringtheory


People also ask

What should be the message body of email like?

The body is the actual text of the email. Generally, you'll write this just like a normal letter, with a greeting, one or more paragraphs, and a closing with your name.


1 Answers

I do it this way:

  • Code it the usual way with ViewModel and Razor Template
  • By creating the e-mail, use http://razorengine.codeplex.com/ to load and parse the template
  • Be aware to not use Html and Url helper if you want to send e-mails in a thread, because they rely on HttpContext which you don't have in that case. Build your own helpers if needed.

For example, if you have a ViewModel Car in your application which is displayed somewhere, you could also use this ViewModel as @model in a Razor Template for e-mail.

like image 113
Marc Avatar answered Sep 28 '22 04:09

Marc