public async Task<string?> SendPassEmail(string email)
{
//Check email nel DB
var remail = await _grabcadContext.UTENTE.FirstOrDefaultAsync(c => c.EMAIL == email && !c.FLG_DISABILITATO && !c.FLG_ELIMINATO);
if (remail == null)
return "Email non trovata";
var request = _contextAccessor.HttpContext.Request;
var requestUrl = $"{request.Scheme}://{request.Host}{request.PathBase}";
var resetPasswordUrl = $"{requestUrl}/Account/ResetPasswordToken";
var token = await _passResetByEmailService.AddPassResetByEmailService(email);
var subject = "Reset password";
var content = $"link: {resetPasswordUrl}?email={email}&token={token}";
using (var client = new SmtpClient())
{
try
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress(_from, _from));
emailMessage.To.Add(new MailboxAddress(email, email));
emailMessage.Subject = subject;
emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text) { Text = content };
await client.ConnectAsync(_host, _port, true);
client.AuthenticationMechanisms.Remove("XOAUTH2");
await client.AuthenticateAsync(_smtpUser, _smtpPassword);
await client.SendAsync(emailMessage);
}
catch (Exception ex)
{
throw;
}
finally
{
await client.DisconnectAsync(true);
client.Dispose();
}
}
return null;
}
}
}