Send Mail Out of Context #12

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
@jwynveen

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.

@smsohan

This comment has been minimized.

Show comment
Hide comment
@smsohan

smsohan Apr 28, 2011

Owner

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.

Owner

smsohan commented Apr 28, 2011

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.

@TylerBrinks

This comment has been minimized.

Show comment
Hide comment
@TylerBrinks

TylerBrinks May 3, 2011

I'm sure there are plenty of people waiting for this feature, myself included. Looking forward to it (since it's early May already)!

I'm sure there are plenty of people waiting for this feature, myself included. Looking forward to it (since it's early May already)!

@jwynveen

This comment has been minimized.

Show comment
Hide comment
@jwynveen

jwynveen May 11, 2011

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.

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.

@smsohan

This comment has been minimized.

Show comment
Hide comment
@smsohan

smsohan May 11, 2011

Owner

Nope, its not ready to pull. But I will keep you posted when its ready.

Owner

smsohan commented May 11, 2011

Nope, its not ready to pull. But I will keep you posted when its ready.

@xiaobao

This comment has been minimized.

Show comment
Hide comment
@xiaobao

xiaobao May 18, 2011

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.

xiaobao commented May 18, 2011

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.

@smsohan

This comment has been minimized.

Show comment
Hide comment
@smsohan

smsohan May 18, 2011

Owner

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.

Owner

smsohan commented May 18, 2011

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.

@chobo2

This comment has been minimized.

Show comment
Hide comment
@chobo2

chobo2 May 22, 2011

@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"?

chobo2 commented May 22, 2011

@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"?

@daniel-sim

This comment has been minimized.

Show comment
Hide comment
@daniel-sim

daniel-sim Jun 11, 2011

I'm very interested in using MVC mailer + Quartz.net

I'm very interested in using MVC mailer + Quartz.net

@smsohan

This comment has been minimized.

Show comment
Hide comment
@smsohan

smsohan Jun 11, 2011

Owner

Any idea how this can be used?

On Sat, Jun 11, 2011 at 8:54 AM, daniel-sim <
reply@reply.github.com>wrote:

I'm very interested in using MVC mailer + Quartz.net

Reply to this email directly or view it on GitHub:
#12 (comment)

Owner

smsohan commented Jun 11, 2011

Any idea how this can be used?

On Sat, Jun 11, 2011 at 8:54 AM, daniel-sim <
reply@reply.github.com>wrote:

I'm very interested in using MVC mailer + Quartz.net

Reply to this email directly or view it on GitHub:
#12 (comment)

@daniel-sim

This comment has been minimized.

Show comment
Hide comment
@daniel-sim

daniel-sim Jun 11, 2011

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?

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?

@smsohan

This comment has been minimized.

Show comment
Hide comment
@smsohan

smsohan Jun 11, 2011

Owner

I think there are multiple issues with trying to make email sending offline
workable while using the MVC framework. For example, all your http and url
helpers will be uncertain. Also things like partials/master
pages/stylesheets are all dependent on the http context - which is possible
to fake only upto a certain level with a limiting confidence. I will wait
and see it I have a robust solution, or it will break at some of the use
cases.

Thank you for your time.

On Sat, Jun 11, 2011 at 9:05 AM, daniel-sim <
reply@reply.github.com>wrote:

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?

Reply to this email directly or view it on GitHub:
#12 (comment)

Owner

smsohan commented Jun 11, 2011

I think there are multiple issues with trying to make email sending offline
workable while using the MVC framework. For example, all your http and url
helpers will be uncertain. Also things like partials/master
pages/stylesheets are all dependent on the http context - which is possible
to fake only upto a certain level with a limiting confidence. I will wait
and see it I have a robust solution, or it will break at some of the use
cases.

Thank you for your time.

On Sat, Jun 11, 2011 at 9:05 AM, daniel-sim <
reply@reply.github.com>wrote:

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?

Reply to this email directly or view it on GitHub:
#12 (comment)

@daniel-sim

This comment has been minimized.

Show comment
Hide comment
@daniel-sim

daniel-sim Jun 11, 2011

Yes, I can see that so hesitated pulling the fork. I'm going to look at going through an asmx.

Yes, I can see that so hesitated pulling the fork. I'm going to look at going through an asmx.

@smsohan

This comment has been minimized.

Show comment
Hide comment
@smsohan

smsohan Jun 11, 2011

Owner

Hmm, going through a web service is likely the best alternative, that way
you have the full stack on your hand. I would suggest going RESTful, so you
have a simplified API.

Sohan
http://smsohan.com
skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Sat, Jun 11, 2011 at 9:45 AM, daniel-sim <
reply@reply.github.com>wrote:

Yes, I can see that so hesitated pulling the fork. I'm going to look at
going through an asmx.

Reply to this email directly or view it on GitHub:
#12 (comment)

Owner

smsohan commented Jun 11, 2011

Hmm, going through a web service is likely the best alternative, that way
you have the full stack on your hand. I would suggest going RESTful, so you
have a simplified API.

Sohan
http://smsohan.com
skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Sat, Jun 11, 2011 at 9:45 AM, daniel-sim <
reply@reply.github.com>wrote:

Yes, I can see that so hesitated pulling the fork. I'm going to look at
going through an asmx.

Reply to this email directly or view it on GitHub:
#12 (comment)

@daniel-sim

This comment has been minimized.

Show comment
Hide comment
@daniel-sim

daniel-sim Jun 12, 2011

REST is best

REST is best

@daniel-sim

This comment has been minimized.

Show comment
Hide comment
@daniel-sim

daniel-sim Jun 14, 2011

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:

  • Check if there's a scheduled email to send
  • Attempt to send it using MvcMailer
  • Log the result of the send attempt
  • Return true if there was one eligible to send, false if none eligible (a JsonResult is overkill at the moment, but will be useful as it expands)

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.

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:

  • Check if there's a scheduled email to send
  • Attempt to send it using MvcMailer
  • Log the result of the send attempt
  • Return true if there was one eligible to send, false if none eligible (a JsonResult is overkill at the moment, but will be useful as it expands)

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.

@Korayem

This comment has been minimized.

Show comment
Hide comment
@Korayem

Korayem Jul 16, 2011

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

Korayem commented Jul 16, 2011

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

@dvdstelt

This comment has been minimized.

Show comment
Hide comment
@dvdstelt

dvdstelt Jun 26, 2014

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'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... 😒

@salmankhann

This comment has been minimized.

Show comment
Hide comment
@salmankhann

salmankhann Aug 6, 2014

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 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.

@gzurbach

This comment has been minimized.

Show comment
Hide comment
@gzurbach

gzurbach Aug 19, 2014

I'd love to see that feature too! 👍

I'd love to see that feature too! 👍

@molinjinyi

This comment has been minimized.

Show comment
Hide comment
@molinjinyi

molinjinyi Nov 24, 2014

什么时候可以邮件在后台发布?Email sending from a background process?Time?

什么时候可以邮件在后台发布?Email sending from a background process?Time?

@nicolocodev

This comment has been minimized.

Show comment
Hide comment
@nicolocodev

nicolocodev Dec 7, 2014

Any plan to merge this pull request soon?

Any plan to merge this pull request soon?

@Jogai

This comment has been minimized.

Show comment
Hide comment
@Jogai

Jogai Jan 10, 2015

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

Jogai commented Jan 10, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment