UserManager.SendEmailAsync不发送

时间:2017-01-16 18:23:56

标签: c# asynchronous smtp

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

2 个答案:

答案 0 :(得分:0)

当您返回<table class="table" id="aut"> <tr> <th>Pt</th> <th>Eas</th> <th>Cs</th> <th>Or</th> <th>Pr</th> <th>Ct?</th> <th>Al</th> </tr> <tr> <td><input type="text" name="Pt" value="<?php echo $pt; ?>" class="form-control" readonly=""></td> <td><input type="text" name="Eas" value="<?php echo $eas; ?>" class="form-control" readonly=""></td> <td><input type="text" name="Cs" value="<?php echo $tpc; ?>" class="form-control" readonly=""></td> <td><input type="text" name="Or" value="<?php echo $shd; ?>" class="form-control" readonly=""></td> <td><input type="text" name="Pr" value="<?php echo $usd; ?>" class="form-control" readonly=""></td> <td><input type="text" name="Al" value="<?php echo $epc; ?>" class="form-control" disabled></td> </tr> </table> 而不是Task时,您需要在调用代码中确保void返回await,或使用{ {1}}。

在这种情况下,您无需使用Task方法.Wait()SendAsync async来电,但如果您有疑问,那么您通常应该只是为了避免一些令人讨厌的边缘情况。

答案 1 :(得分:0)

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