繁体   English   中英

UserManager.SendEmailAsync不发送

[英]UserManager.SendEmailAsync doesnt send

我正在尝试使用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);
        }

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

在这种情况下,不需要使您的SendAsync方法asyncawait SendMailAsync调用,但是如果您有疑问,则通常应该只是避免某些讨厌的情况。

刚刚和我的服务器人员交谈。 问题在于,电子邮件服务器阻止了support@group.com,但没有阻止no-reply@group.com。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM