SmtpClient SendMailAsync无法发送消息。 20个字符。
此代码将与注释行一起使用,该行仅将“ test”作为电子邮件发送,但第二次(当前未注释)SendMailAsync调用失败。
在非.NET核心应用程序中,我可以毫无问题地发送大型电子邮件。
Register.cshtml.cs
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
var user = new IdentityUser { UserName = Input.UserName, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
var roleResult = await _userManager.AddToRoleAsync(user, "Member");
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { userId = user.Id, code = code },
protocol: Request.Scheme);
//await _emailSender.SendEmailAsync(Input.Email, "Confirm Your Email", "test");
await _emailSender.SendEmailAsync(Input.Email, "Confirm Your Email", "Please confirm your account. <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>Verify</a>.");
return LocalRedirect(returnUrl);
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
IEmailSender.cs
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
var client = new SmtpClient(host, port)
{
UseDefaultCredentials = true,
Credentials = CredentialCache.DefaultNetworkCredentials,
EnableSsl = enableSSL,
DeliveryMethod = SmtpDeliveryMethod.Network
};
MailMessage mm = new MailMessage();
mm.To.Add(email);
mm.From = new MailAddress(fromAddress);
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.SubjectEncoding = System.Text.Encoding.UTF8;
mm.HeadersEncoding = System.Text.Encoding.UTF8;
mm.Priority = MailPriority.High;
mm.IsBodyHtml = true;
mm.Subject = subject;
mm.Body = htmlMessage;
await client.SendMailAsync(mm);
}
没有返回异常或错误。