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

I am working on a web application that will live soon, and now I am trying to find a better way to handle sending email from the application. I fully understand HOW to send email from an application using the MailMessage and SmtpClient classes, however my question is from a different angle. My main goal in my work before this project was to support old applications that were developed before my time. In these applications, when they needed to send emails, they strictly encoded any of the messages into the actual message with all the HTML tags embedded directly in the C # code.

In the application in which I work, a template will be created for sending emails in the form of a kind of container for styling, and different messages will be included in the main content div of the template. I would like to avoid hard-coding these patterns in this application, so I tried to figure out the best layout for my project. I thought about using the t4 template and reading the various t4 in the application and applying String.Format with the specified parameters to add names / messages to sent messages. However, I'm not sure if this is the best way to do this.

My other idea was to define a class for each type of message, however this would again cause hard-coded messages, which, as I said, I do not want to do.

My question is: how did you feel about this in the past? What worked and what didn’t, and for what reasons? I watched it over the Internet, but either the only content there where you can send a message, or I did not use the correct words Power Power.

+6
source share
2 answers

I do like this:

  • Encode it in the usual way using the ViewModel and Razor templates
  • Once you have created the email, use http://razorengine.codeplex.com/ to download and analyze the template.
  • Keep in mind that do not use the helper and the helper url if you want to send emails in a stream because they rely on an HttpContext that you don't have in this case. If necessary, create your own helpers.

For example, if you have a ViewModel Car in your application that displays somewhere, you can also use this ViewModel as @model in the Razor email template.

+2
source

I had to do this several times. I originally used the ASP.Net template engine, based, in my opinion, on the Rick Strahl blog. It worked, but there was always a problem with which I hit my head.

I switched to using the NVelocity engine and found it to be a very simple way to create and maintain email templates. There are a number of other template engines, and I suspect that the next time I could take a serious look at Razor .

Code for combining values ​​into a template:

 private string Merge(ManualTypeEnum manualType, Object mergeValues) { var body = ""; var templateFile = string.Format("{0}MailTemplate.vm", manualType); var velocity = new VelocityEngine(); var props = new ExtendedProperties(); props.AddProperty("file.resource.loader.path", Config.EmailTemplatePath); velocity.Init(props); var template = velocity.GetTemplate(templateFile); var context = new VelocityContext(); context.Put("Change", mergeValues); using (var writer = new StringWriter()) { template.Merge(context, writer); body = writer.ToString(); } return body; } 

The values ​​for the union are passed as an anonymous object and can include various types, including lists, etc., for example.

 var emailBody = Merge(newDocument.ManualType, new { ManualType = newDocument.ManualType.ToString(), Message = change.Message, NewTitle = newDocument.Title ?? "", NewVersion = newDocument.Version ?? "", Contact = From, Changes = change.ToList(), }); 
0
source

Source: https://habr.com/ru/post/904045/


All Articles