开发者

Design Question for sending e-mail notifications

开发者 https://www.devze.com 2023-03-13 20:54 出处:网络
I have a very basic question however the solution might be somewhat complex. How are website devs sending out e-mails such as for开发者_JS百科got password links, registration messages, and/or any ot

I have a very basic question however the solution might be somewhat complex.

  1. How are website devs sending out e-mails such as for开发者_JS百科got password links, registration messages, and/or any other notifictions that need to be sent.
  2. Are developers storing the messages in SQL server, a seperate class, XML??

I am using the Onion model and my SMTP Interface is in app.core however my base class for sending mail is located in infrastructure.backends. I dont't want my application services be dependant on backends, not sure if this is right wrong or me being OCD..

I am using Ninject as my IOC/DI.


Use Postal . I think it's the most efficient way of developing email templates. The architecture can be built keeping it in mind. It allows us to change the templates without re-compiling the code.

As per architecture, I haven't really had any issues with keeping it as part of my application services.


If you're sending html emails, I use a slimmed down master/layout page and views. Then I let the controller render that view to a string for use in the emails. It's just like any other mvc view and model.


In c# you System.Net.Mail. I will do most things for you.


Slightly modified from a post I found, this seems to be the right way to roll. At least it is a start.
public interface IViewMailer { string RenderPartialViewToString(string viewName, object model, ControllerContext controllerContext); }

public class ViewMailer : IViewMailer
{
    #region IViewMailer Members

    public string RenderPartialViewToString(string viewName, object model, ControllerContext controllerContext)
    {
        if (string.IsNullOrEmpty(viewName))

            viewName = controllerContext.RouteData.GetRequiredString("action");
        controllerContext.Controller.ViewData.Model = model;
        using (var stringWriter = new StringWriter())
        {
            ViewEngineResult viewEngineResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
            var viewContext = new ViewContext(controllerContext, viewEngineResult.View,
                                              controllerContext.Controller.ViewData,
                                              controllerContext.Controller.TempData, stringWriter);
            viewEngineResult.View.Render(viewContext, stringWriter);
            return stringWriter.GetStringBuilder().ToString();
        }
    }

    #endregion
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号