0
I made this form that the user when registering should receive an email in which will have a link to confirm your email by clicking on the link, so the way I did it 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 registry 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);
}
Someone could identify what’s wrong with that??
What is the exception that is appearing in your catch?
– Leandro Angelo
No exception appears, because the registration is performed successfully, but I do not receive the email to confirm the registration!
– WPfan
I refer to the catch in sending email, as you are not handling the error and following the execution with Finally even in case of failure.
– Leandro Angelo
you debugged until catch to see error?
– Leandro Angelo
The solution was to use Sendgrid, I learned to do the authentication, that was what was missing... then everything went well
– WPfan