I have the below code to send email after a user is registered but I am getting that it is not a real email!
I am not getting errors I am only getting that the email is not real (default page) after registration.
This app does not currently have a real email sender registered, see these docs for how to configure a real email sender.
public async Task<bool> SendEmailAsync(string email, string subject, string confirmLink)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
message.From = new MailAddress("ramiwahdan2023@gmail.com");
message.To.Add(email);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = confirmLink;
smtpClient.Port = 465;
smtpClient.Host = "smtp.ionos.com";
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("info@dr-maha-habib.com", "Fatima@2020");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
return true;
}
catch (Exception)
{
return false;
}
}
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl ??= Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
var user = new AppUser
{
FirstName = Input.FirstName,
LastName = Input.LastName,
UserName = Input.Email,
Email = Input.Email
};
await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var userId = await _userManager.GetUserIdAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
await SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
}
else
{
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}