0

i am new to mvc and i am trying to make confirmation email on registration in mvc5. i have followed sometutorials. lots of them use sendgrid but i want to use smtp. i have done the tutorials step by step by i cant get email after registration. i cant find the problem becuase i have done what i was read in tutorials. any help is appritiated.

in identityConfig.cs

 public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        //return Task.FromResult(0);

        // Credentials:
        var credentialUserName = "[myEmail]";
        var sentFrom = "[myEmail]";
        var pwd = "[myPassword]";

        // Configure the client:
        System.Net.Mail.SmtpClient client =
            new System.Net.Mail.SmtpClient("smtp.gmail.com");

        client.Port = 587;
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;

        // Create the credentials:
        System.Net.NetworkCredential credentials =
            new System.Net.NetworkCredential(credentialUserName, pwd);

        client.EnableSsl = true;
        client.Credentials = credentials;


        // Create the message:
        var mail =
            new System.Net.Mail.MailMessage(sentFrom, message.Destination);

        mail.Subject = message.Subject;
        mail.Body = message.Body;

        // Send:
        return client.SendMailAsync(mail);

    }
}


 public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
        this.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<ApplicationUser, string>();
        this.EmailService = new EmailService();
    }

and in my AccontController.cs

 public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

and i havnt change anything else. am i missing something? thank you very much.

  • Firstly, it seems you haven't posted the complete code. I don't see a call to `EmailService.SendAsync`. Secondly, I suggest you create a [mcv](http://stackoverflow.com/help/mcve) to ensure that the email component works as expected. Last time I tried using gmail smtp, it didn't work for me. – Vishal Shah Nov 27 '15 at 06:03
  • this is exactly my problem. i cant see a clear chain in theese codes. and i cant see one in tutorials too. i know i havnt call EmailService.SendAsync but i didnt see anybody use it. – Ashkan Hafezi Nov 27 '15 at 10:06

0 Answers0