Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MvcMailer be used in a class library?

I want to use MvcMailer in a class library, which would essentially be my one-stop-shop for composing and sending emails for my solution. I thought that that is what MvcMailer was designed for, but it appears it cannot find any of my .cshtml files--I guess it expects them to in my startup project.

Is there any way for MvcMailer to be 100% separate from my other projects?

Thanks.

like image 998
smdrager Avatar asked Apr 09 '12 21:04

smdrager


1 Answers

This is not possible according to MvcMailer documentation https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide (extract below), maybe you can have a separate web project that its only purpose is to send emails and only accept requests from your application

Email Sending from a Background Process Do you need to send emails from a background process? Yes, you're right. You don't want to block your request/response cycle for that notification email to be sent. Instead, what you want is a background process that does it for you, even if it's sent after a short delay. Here's what you can do:

Save your email related data into a database. Create a REST/SOAP web service that sends out the emails. This will ensure your Mailer has access to the HttpContext, which is essential for the core ASP.NET MVC framework to work properly. For example, to find your views, produce URLs, and perform authentication/authorization. Create a simple App that calls the web service. This could be a windows service app or an executable app running under Windows Scheduled task. A future version of MvcMailer is likely to have support for this. But it is hard because of two reasons:

MailMessage is not Serializable out of the box and has a lot of complex fields and associations. The core ASP.NET framework still needs HttpContext :(

like image 122
jorgehmv Avatar answered Oct 10 '22 04:10

jorgehmv