How do I confirm the email using Sendgrid?

0

In my application when the user registers, an email has to be sent to him to confirm his account. I did everything here so something is wrong, could anyone help me? Initially I created a class called ServicoEmail:

namespace Ebase.EmissorNFeWeb.Servicos
{
    public class ServicoEmail
    {

            public static async Task Execute(string Email, string Texto, string Mensagem)
            {
                try
                {
                    MailMessage mailMsg = new MailMessage();

                    // To
                    mailMsg.To.Add(new MailAddress(Email, "Contoso"));

                    // From
                    mailMsg.From = new MailAddress("[email protected]", "Ebase");

                // Subject and multipart/alternative Body
                mailMsg.Subject = "subject";
                string text = "text body";
                string html = @"<p>html body</p>";
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

                // Init SmtpClient and send
                SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
                    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("[email protected]", "minhasenha");
                    smtpClient.Credentials = credentials;

                    smtpClient.Send(mailMsg);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

Then I added the following in webconfig:

<system.net>
  <mailSettings>
    <smtp from="[email protected]">
      <network host="smtp.contoso.com" password="john123" userName="john.smith" port="587" />
    </smtp>
  </mailSettings>
</system.net>

In AccountController's register I did this:

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    NomeCompleto = model.NomeCompleto,
                    UserName = model.NomeCompleto,
                    Email = model.Email,
                    EmpresaNome = model.EmpresaNome,
                    Telefone = model.Telefone
                };
                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 https://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>");

                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await ServicoEmail.Execute(model.Email, "Confirme a sua conta", "Confirme a sua conta clicando <a href=\"" + callbackUrl + "\">AQUI</a>");



                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }

And lastly I modified the confirmEmail view:

@{
ViewBag.Title = "Confirm Email";
}
<h2>@ViewBag.Title.</h2>
<div>
   <p>
      Obrigado por confirmar o seu email. Por favor @Html.ActionLink("Clique aqui para fazer o Login", "OnePage", "Home", routeValues: null, htmlAttributes: new { id = "loginLink" })
   </p>
</div>

Then find out that I was using an old method, then after the new one as it was ... but the email does not arrive to confirm:

    
asked by anonymous 24.04.2018 / 14:57

1 answer

0

I discovered the error, I just changed var plainTextContent = Texto; to var plainTextContent = Mensagem; hence the email confirmation message arrived perfectly !!

    
24.04.2018 / 21:48