0

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("hashemp206@yahoo.com");
        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?

Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75

1 Answers1

0

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.

Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
  • thanks for answer. api consumer is an iOS app. user just register from iOS app. after successful registration `_appUserManager` which is subclass of `ASP.Net Identity UserManager` tries to send email. `AppUserManager` is in a separate class. in that separate class I have a Class named `EmailService` which needs an IUserMailer instance to send email. IUserMailer implementation is in another project named `MyApp.MVCMailer` which is an MVC project. my dependency injection is resided in MyApp.Web.API project. dependency injector needs to know about System.Web.MVC to create IUserMailer. – Hashem Aboonajmi Jul 21 '15 at 16:43
  • all the Thing I do I come to here to add a reference System.Web.MVC to my Web.Api project. can you create a sample web api which sends email with MVCMailer? – Hashem Aboonajmi Jul 21 '15 at 16:45