2016-03-08 106 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!我編輯了我的答案,希望能更有幫助。 –