【问题标题】:UserManager.SendEmailAsync doesnt sendUserManager.SendEmailAsync 不发送
【发布时间】:2017-01-17 03:19:39
【问题描述】:

我正在尝试使用 SmtpClient.SendMailAsync 发送一封电子邮件,但它不起作用,尽管同步版本有效 (SmtpClient.Send)。

我的同步代码:

            MailMessage msg = new MailMessage();
            msg.To.Add(new MailAddress("someEmail@gmail.com", "SomeOne"));
            msg.From = new MailAddress("no-reply@group.com", "You2");
            msg.Subject = "This is a Test Mail";
            msg.Body = "This is a test message";
            msg.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = true;
            client.Port = 25; 
            client.Host = "mail.group.com";
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Send(msg);

我的异步电子邮件代码(我刚刚修改了 EmailService : IIdentityMessageService 类):

public Task SendAsync(IdentityMessage message)
{

    const string sentFrom = "support@group.com";

    // Configure the client:
    var client = new SmtpClient("mail.group.com")
    {
        Port = 25,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = true,
        EnableSsl = true
    };

    // Create the message:
    var mail = new MailMessage(sentFrom, message.Destination, message.Subject, message.Body);

    // Send:
    return client.SendMailAsync(mail);
}

编辑:调用代码由Visual Studio生成,这里是:

public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // 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, "Confirmer votre compte", "S'il vous plaît, cliquez le lien suivant :  <a href=\"" + callbackUrl + "\">Activation</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

【问题讨论】:

标签: c# asynchronous smtp


【解决方案1】:

在您返回Task 而不是void 时,您需要确保在调用代码中await 是返回的Task,或使用.Wait()

在这种情况下,没有必要将SendAsync 方法asyncawait 调用SendMailAsync,但是如果您有疑问,那么您通常应该避免一些讨厌的边缘情况。

【讨论】:

  • 是的,这就是我在这个版本之前尝试过的,但仍然没有成功
  • 那么当你调试它时会发生什么,它是否逐步通过这些方法好吗?有例外吗?
  • 没有例外,一切看起来都很好。
  • 在调用代码sn-p中,你没有在任何地方调用SendAsync,你是在堆栈更深处调用它吗?
  • 它通过以下方式调用:await UserManager.SendEmailAsync(user.Id, "Confirmer votre compte", "S'il vous plaît, cliquez le lien suivant : 激活");
【解决方案2】:

刚刚和我的服务员谈过了。问题是 support@group.com 被电子邮件服务器阻止,而不是 no-reply@group.com。

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 2014-05-27
    • 2020-09-03
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2018-08-07
    相关资源
    最近更新 更多