Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13850

Using MVCMailer with ASP.Net Web Api

I'm using ASP.Net Identity and in my Web Api project in its AccountController I want to send verification email to new users. I have plugged my email service using MVCMailer to the ASP.Net identity.

public class EmailService : IIdentityMessageService
{
    private readonly IUserMailer _userMailer;
    public EmailService(IUserMailer userMailer)
    {
        _userMailer = userMailer;
    }
    public Task SendAsync(IdentityMessage message)
    {
        _userMailer.DeliverMessage(message.Body);
        // Plug in your email service here to send an email.
        return Task.FromResult(0);
    }
}
#
public class UserMailer : MailerBase, IUserMailer
{
    public UserMailer()
    {
        MasterName = "_Layout";
    }

    public virtual IMailMessage DeliverMessage(string message)
    {
        var mailMessage = new MailMessage();
        mailMessage.To.Add("[email protected]");
        mailMessage.Subject = "Welcome";

        //ViewData = new System.Web.Mvc.ViewDataDictionary(model);
        PopulateBody(mailMessage, "Welcome");
        mailMessage.Send();
        return mailMessage;
    }

my custom ASP.Net Identiy is in a seperate project. and as you see above EmailService is dependent on IUserMailer interface. and IUserMailer implementation is in another project MyProject.MVCMailer (this project is an MVC project)

in my dependency resolver in web api I try to resolve this dependency container.Bind<IUserMailer>().To<UserMailer>().InSingletonScope();

but MVCMailer has a dependency to System.Web.MVC and ninject complain for this reference to initialize USerMailer.

so the problem is here I dont want to add System.Web.MVC to my Web Api project.

how can I use MVCMailer without referencing to System.Web.MVC in my web api project?

Upvotes: 0

Views: 305

Answers (1)

Stephen Brickner
Stephen Brickner

Reputation: 2602

I'm a little confused on what you're trying to do but if I'm understanding you correctly you are trying to call your API and then send out an email. Since you are not passing anything from the API into the email (and even if you were) just call your API and return a response object. Once the MVC project recieves the response send the email from the MVC project. Your API should know about objects in your MVC project unless there is a really good reason. Think of them (your MVC and API projects) as two separate entities all together that don't know about each other.

Upvotes: 0

Related Questions