I made this form that the user when registering should receive an email in which he will have a link to confirm his email by clicking on the link, so in the way I did he is not receiving the confirmation email. For this I created a class called ServicoEmail:
namespace Ebase.EmissorNFeWeb.Servicos
{
public class ServicoEmail
{
public static async Task Execute(string Email, string Texto, string Mensagem)
{
if (!string.IsNullOrWhiteSpace(Email) && !string.IsNullOrWhiteSpace(Mensagem))
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "email-ssl.com.br ";
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "minhasenha");
MailMessage mail = new MailMessage();
mail.Sender = new System.Net.Mail.MailAddress("[email protected]");
mail.From = new MailAddress("[email protected]");
mail.To.Add(new MailAddress(Email, "RECEBEDOR"));
mail.Subject = "Confirmação de Email";
mail.Body = "Nome: " + Email + " <br/> Mensagem : " + Mensagem;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
try
{
client.Send(mail);
}
catch (System.Exception erro)
{
//trata erro
}
finally
{
mail = null;
}
}
}
}
}
And in the AccountController register I put the following:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser {
NomeCompleto = model.NomeCompleto,
UserName = model.Email,
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>");
Session["NomeUsuario"] = AppDB.PreencherUser(model.Email);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return PartialView("_ExperimenteGratis", model);
}
Could someone identify what's wrong with that?