I'ved added the ability to send email outside of HTTPContext by mocking one up with just the BaseUrl. I didn't run unit tests as they weren't immediately working for me, and I only did it for StringResult (don't know if it's needed anywhere else). I only tested for my specific situation, so it might be able to be optimized.
Now all that you need to do if sending outside of context is specify the BaseUrl of the MailerBase and it if HttpContect.Current is null, it will mock out an HTTPContext object to be able to render the view.
Add ability to send email outside of HTTPContext by mocking one up wi…
…th just the BaseUrl
Thanks for your pull request. I am on the go these days and hopefully get your code before I release a new version early May. Really appreciate your interest.
I'm sure there are plenty of people waiting for this feature, myself included. Looking forward to it (since it's early May already)!
Is this ready to pull down and use? I updated it in my project but it gives an exception that httpContext is null. And when I looked at your commit, I don't see anything that's generating the context if it's null.
Nope, its not ready to pull. But I will keep you posted when its ready.
I just came here to ask what I think is the same thing as this. Are you working on a solution that will allow us to call a controller through code and generate emails?
Since right now If I make my email controller through code and the original request is not coming from a user I get "System.ArgumentNullException was unhandled by user codeMessage=Value cannot be null.Parameter name: httpContext". If I had like a form and submit to my email controller then I have no problems.
The catch is without the httpContext the MVC framework becomes almost useless. For example, HTML helpers, URL helpers, view engines and view finding - everything depends deeply on HttpContext. I would love to make it independent of the context but that said, its gonna be considerable work to guarantee that it will work that way. For now, I would suggest expose a REST/HTTP end point from your web app and hit that from the offline app. This will make a httpContext available and things will be just fine.
@smsohan - could you give an example. For instance I need to pass in a collection of viewmodels. how would I send it to this "Http end point"?
I'm very interested in using MVC mailer + Quartz.net
I have the Quartz scheduler initializing in Application_Start of my asp.net web (which of course is subject to recycling), calling my method to get subscribers from an entity. But: HttpContext.Current is null, so this is where it falls down.
I'm loathe to take this fork as I've found the core to be stable for my interactive email sending. Will jwynveen's fork go in soon, or are there issues with the approach?
Yes, I can see that so hesitated pulling the fork. I'm going to look at going through an asmx.
REST is best
Okay, I have a good solution that allows me to schedule email sending using Quartz and MvcMailer. Currently I have but one scheduled email type, so it's a very simple service.
A SendScheduledEmail controller contains the guts:
I could invoke this service from anywhere of course. As I'm in Azure, with a single web role, I've decided to do it all in this role.
The Quartz scheduler is kicked off in Application_Start. Every minute it calls a method which makes a WebRequest to the SendScheduledEmail controller.
This is a do...while the WebRequest.Response returns true (true being it has found and attempted to send, there may be more).
As I have two web instances, the controller is thread safe.
This is discussion is great
I had exactly the same case as @daniel-sim. We're using Quartz.NET with MvcMailer
We will probably implement @daniel-sim's solution until @smsohan solves this in the next release
I'm using messaging and a process running as a Windows Service. It's a real shame using this framework outside of ASP.NET isn't working. I'd use Postal, who supports this scenario, but it doesn't support changing headers and some other stuff...
I also have the same problem, I'm using scheduling in my Mvc app to send emails at regular intervals. I've defined this code in Application_Start. I'm unable to use MVCMailer because the context is null.
I guess as a workaround, I'll write another action and then from my scheduled task, I'll make an http request to that action.
I'd love to see that feature too!
什么时候可以邮件在后台发布？Email sending from a background process？Time?
Any plan to merge this pull request soon?
It seems possible with Hangfire. It would send the mails in the background, but still within context. See http://docs.hangfire.io/en/latest/tutorials/send-email.html