在我ASP.NET Core 1.1.1
开发的应用程序中VS2017 Ver 15.3.3
,我使用ASP.NET Core和MailKit中的帐户确认和密码恢复来实现上述文章的功能,但出现以下错误:
注意:
- 错误发生在下面
await client.ConnectAsync("smtp.relay.uri", 25, SecureSocketOptions.None).ConfigureAwait(false);
的SendEmailAsync(...)
方法行和 - 在 post 方法的行
await _emailSender.SendEmailAsync(model.Email, "Confirm your account", $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
也Register(...)
如下所示:
错误
SocketException:没有这样的主机是已知的
MessageServices.cs 类
public class AuthMessageSender : IEmailSender, ISmsSender
{
public async Task SendEmailAsync(string email, string subject, string message)
{
// Plug in your email service here to send an email.
//return Task.FromResult(0);
var emailMessage = new MimeMessage();
//You can if required (and I have done in my code) set the LocalDomain used when communicating with the SMTP server
//This will be presented as the origin of the emails. In my case I needed to supply the domain so that our internal testing SMTP server would accept and relay my emails.
//We then asynchronously connect to the SMTP server. The ConnectAsync method can take just the uri of the SMTP server or as I’ve done here be overloaded with a port and SSL option. For my case when testing with our local test SMTP server no SSL was required so I specified this explicitly to make it work.
emailMessage.From.Add(new MailboxAddress("MyName", "MyEmail@MyDomain.com"));
emailMessage.To.Add(new MailboxAddress("", email));
emailMessage.Subject = subject;
emailMessage.Body = new TextPart("plain") { Text = message };
using (var client = new SmtpClient())
{
client.LocalDomain = "smtp.MyDomain.com";
await client.ConnectAsync("smtp.relay.uri", 25, SecureSocketOptions.None).ConfigureAwait(false);
await client.SendAsync(emailMessage).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false);
}
}
在 AccountController.cs 中注册 post 方法
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
$"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
更新
不确定,但该错误可能与我没有正确使用我的电子邮件主机信息有关:
- 我正在使用网站/电子邮件托管公司DiscountASP.NET的网络邮件功能。每个订阅者的 SMTP 服务器名称是 smtp.YourDomainName.com。因此,在上述
SendEmailAsync(...)
方法中,我使用client.LocalDomain = "smtp.MyDomain.com";
- 对于 MailKit 实现,我将关注本文
Sending email via a SMTP server
的部分内容。