andrewdavey / postal

Email sending for asp.net mvc using the view engine system to render emails.
http://aboutcode.net/postal
MIT License
536 stars 168 forks source link

How to use Postal with Unity IoC Container MVC 5? #182

Open haseebIC opened 3 years ago

haseebIC commented 3 years ago

I am having trouble with Postal setup. I have registered dependencies like below:

container.RegisterType<IEmailParser, EmailParser>(); container.RegisterType<IEmailViewRenderer, EmailViewRenderer>(); container.RegisterType<IEmailService, EmailService>();

On the controller side I have the following code:

private readonly IEmailService _emailService;

public EmailsController(IEmailService emailService) { this._emailService = emailService; }

public ActionResult Test() { var email = new TestEmail {

        };
        var message = _emailService.CreateMailMessage(email);
        return new EmailViewResult(email);

}

The dependencies are being injected properly by the unity container but the target view ("Test") is not being found by ViewEngine.

However, when I have the following code in the controller everything works fine.

private readonly IEmailService _emailService = new EmailService(ViewEngines.Engines);;

public ActionResult Test() { var email = new TestEmail {

        };
        var message = _emailService.CreateMailMessage(email);
        return new EmailViewResult(email);

}

I believe I'm doing something wrong while registering the unity container. Please help me out.

Thanks