1

The code in my AdminController.cs

await UserManager.SendEmailAsync(
    user.Id, "Confirm your account", 
    "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"
);

Does not appear to attempt to send an email.

IdentityConfig.cs

public Task SendAsync(IdentityMessage message)
    {            

        // Plug in your email service here to send an email.
        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = true;

        MailMessage mail = new MailMessage("noreply@website.com", message.Destination);
        mail.Subject = message.Subject;
        mail.Body = message.Body;
        mail.IsBodyHtml = true;

        return client.SendMailAsync(mail);            
    }

I use the following code elsewhere in my application which does work.

IdentityMessage emailMessage = new IdentityMessage
  {
    Destination = recipient.Email,
    Subject = "You have a new message - " + message.Subject,
    Body = "<p>Message from: " + recipient.UserName + "</p>" + message.Body
  };
  EmailService emailService = new EmailService();
  emailService.SendAsync(emailMessage);

I have seen a few similar questions but as far as I can see, my setup should be working.

Any advice much appreciated.

Thanks!

Gordon Quinn
  • 132
  • 6
  • `await emailService.SendAsync(emailMessage);`? – GSerg Sep 05 '17 at 15:04
  • @GSerg `emailService.SendAsync(emailMessage);` works OK, if I add await before it I receive the following error: `The await operator can only be used with an async method. Consider making this method with the async modifier and changing its return type to Task` – Gordon Quinn Sep 05 '17 at 15:50
  • Yes, you need to make the method `async` to use `await`. Without `await` in that context the call to `emailService.SendAsync` is a ["fire and forget"](https://stackoverflow.com/q/46053175/11683). Note that you don't need `async` on `public Task SendAsync` because it's fine to return a `Task`. – GSerg Sep 05 '17 at 18:05
  • @GSerg What about the out of the box code in AdminController.cs i.e. `await UserManager.SendEmailAsync( user.Id, "Confirm your account", "Please confirm your account by clicking here" );` - this does not send the email but I cannot work out why not – Gordon Quinn Sep 15 '17 at 12:07

2 Answers2

1

Found a solution, the issue was not actually what I thought.

I had changed the default username from the out of the box choice of email, there are several places within the Account Controller which lookup the users Id based on their username but searching by email, so essentially it was using the username to search for an email, which out of the box is fine but since I had made the change this was broken.

For example within "ForgotPassword" there is the following lookup:

UserManager.FindByEmailAsync(model.Email);

This was previously using model.Username which was causing the emails to not be sent for me.

Gordon Quinn
  • 132
  • 6
0

Without a 'async await', the error will not be shown

public async Task<ActionResult> EmailMethod()
        {
            IdentityMessage emailMessage = new IdentityMessage
            {
                Destination = recipient.Email,
                Subject = "You have a new message - " + message.Subject,
                Body = "<p>Message from: " + recipient.UserName + "</p>" + message.Body
            };
            EmailService emailService = new EmailService();
            await emailService.SendAsync(emailMessage);
            return View();
        }

What kind of error occurs when sending email?

savl
  • 1
  • The part not using async does work, it is actually the out of the box code in the AdminController which does not do anything. No errors appear to be thrown. `await UserManager.SendEmailAsync( user.Id, "Confirm your account", "Please confirm your account by clicking here" );` - The issue being that no email appears to be attempted to be sent – Gordon Quinn Sep 15 '17 at 12:05