2016-03-08 52 views
0

我试图发送自动电子邮件给用户完成注册,我测试本地主机上我的应用程序ASP净MVC sendgrid帐户电子邮件验证

这是我的注册控制器:

// POST: /Account/Register 
[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
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) 
     { 
      // Comment the following line to prevent log in until the user is confirmed. 
      // await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

      // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
      // 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, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

      // Uncomment to debug locally 
      // TempData["ViewBagLink"] = callbackUrl; 

      ViewBag.Message = "Check your email and confirm your account, you must be confirmed " 
          + "before you can log in."; 

      return View("Info"); 
      //return RedirectToAction("Index", "Home"); 


     } 
     AddErrors(result); 
    } 

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

这是我的电子邮件服务类

public class EmailService : IIdentityMessageService 
{ 
    public async Task SendAsync(IdentityMessage message) 
    { 
     // Create the email object first, then add the properties. 
     var myMessage = new SendGridMessage(); 

     // this defines email and name of the sender 
     myMessage.From = new MailAddress("[email protected]", "My Example Admin"); 

     // set where we are sending the email 
     myMessage.AddTo(message.Destination); 

     myMessage.Subject = message.Subject; 

     // make sure all your messages are formatted as HTML 
     myMessage.Html = message.Body; 

     // Create credentials, specifying your SendGrid username and password. 
     var credentials = new NetworkCredential(
      ConfigurationManager.AppSettings["SendGridLogin"], 
      ConfigurationManager.AppSettings["SendGridPassword"] 
               ); 

     // Create an Web transport for sending email. 
     var transportWeb = new Web(credentials); 

     // Send the email. 
     await transportWeb.DeliverAsync(myMessage); 
    } 
} 

我使用SendGridSmtpApi 1.3.1 & SendGrid C#cilent库6.3.4。 问题在哪里,为什么这不起作用?

+0

你有没有注册与SendGrid账号? – severin

+0

是的。我有积极的sendgrid帐户。 –

+0

好的。我不确定您是否可以使用凭据发送电子邮件,但您可能需要在SendGrid帐户上创建一个ApiKey。我个人发送只使用ApiKey,如下所示:var transportWeb = new SendGrid.Web(); – severin

回答

1

因为您正在进行异步调用,所以您的程序可能在调用之前退出并有机会完成。因此,以下是必要的,如果你想阻止,直到通话结束后,你的程序继续之前:

transportWeb.DeliverAsync(message).Wait(); 

另外,也可以从调用函数取决于你所期望的结果“等待()或结果” 。

欲了解更多信息,请查看以下内容:

What happens while waiting on a Task's Result?

Await on a completed task same as task.Result?

+1

虽然这段代码可能会回答这个问题,但提供关于* why和/或* how *这个代码如何回答这个问题的附加上下文会提高它的长期价值。 –

+1

谢谢你的追随Benjamin!我编辑了我的答案,希望能更有帮助。 –