Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement the SendEmailAsync interface and send Email

I don't know how to invoke the SendEmailAsync function

Register Post page

public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                // Send an email with this link
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                   $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation(3, "User created a new account with password.");
                return RedirectToLocal(returnUrl);
            }
            AddErrors(result);
        }

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

Email configuration Page

// this code was already there

    public interface IEmailSender
{
    Task SendEmailAsync(string email, string subject, string message);
}

//I got this code from some other site. I don't know how to use this together.

public class sendMail : IEmailSender // this line is written by me
{
    public async Task SendEmailAsync(string email, string subject, string message)
    {
        var emailMessage = new MimeMessage();

        emailMessage.From.Add(new MailboxAddress("Joe Bloggs", "[email protected]"));
        emailMessage.To.Add(new MailboxAddress("", email));
        emailMessage.Subject = subject;
        emailMessage.Body = new TextPart("plain") { Text = message };

        using (var client = new SmtpClient())
        {
            client.LocalDomain = "some.domain.com";
            await client.ConnectAsync("smtp.relay.uri", 25, SecureSocketOptions.None).ConfigureAwait(false);
            await client.SendAsync(emailMessage).ConfigureAwait(false);
            await client.DisconnectAsync(true).ConfigureAwait(false);
        }
    }
}

how the write the class definition to send email.

Break point does not reach here, I think there is some problem with my implementation.

like image 767
Arun Prasad E S Avatar asked Jan 05 '17 12:01

Arun Prasad E S


1 Answers

in Startup make sure you change this:

services.AddTransient<IEmailSender, AuthMessageSender>();

to

services.AddTransient<IEmailSender, sendMail>();

so that your implementaiton gets injected

like image 172
Joe Audette Avatar answered Oct 14 '22 12:10

Joe Audette