2

I have hybrid project of ASP Web Api and angular project similar to this one and I need to send emails from ApiControllers in it.

I like MvcMailer and I'd prefer to use that. I managed it to actually send emails. However, I'm not sure how to pass custom data to the view since I do not have access to ViewBag or ViewData in ApiController context.

Any help is appreciated.

Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79
  • Hey! I'm having the same issue. Actually my email is just empty. Did you find a workaround by any chance? You might want to look at this: https://github.com/smsohan/MvcMailer/issues/126 – Pedro Apr 04 '14 at 02:14
  • Your issue is not quite the same (and the issue you referenced does not help me). I got to the point where my email sends and it is not empty. I just can't enhance it with dynamic data at the moment. I'll write a comment here if I find a solution. – Dmitry Efimenko Apr 04 '14 at 06:26

1 Answers1

1

I understand you do not have access to ViewBag/ViewData, which is probably related that you have a WebAPI context (and not MVC). But why don't you just instantiate the mailer and pass parameters?

[RoutePrefix("api/foo")]
public class FooController : ApiController
{
    [HttpPost]
    [Route("bar")]
    public void Bar(string foo, string bar)
    {
        MyMailer myMailer = new MyMailer();
        myMailer.MyAction(foo, bar).Send();
    }
}

Then:

public class MyMailer : MailerBase, IMyMailer   
{
    public virtual MvcMailMessage MyAction(string foo, string bar)
    {
        ViewBag.Foo = foo;
        ViewBag.Bar = bar;
        // Populate ...
    }
}

I know it's not optimal but that's what I ended up doing and it works fine. Would that help?

Pedro
  • 3,511
  • 2
  • 26
  • 31