2016-08-16 153 views
1

我一直在關注本教程:http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset。我已經通過了三次,並檢查了幾個Stackoverflow的帖子,但我仍然不知道我缺少什麼。通過調試,Visual Studio顯示myMessage具有它所需的一切(要發送給的電子郵件地址,郵件主題,郵件正文,來自哪裏的人等),但實際上我沒有收到確認郵件。這是我目前擁有的代碼:SendGrid不發送電子郵件

IdentityConfig.cs

public class EmailService : IIdentityMessageService 
{ 
    public async Task SendAsync(IdentityMessage message) 
    { 
     // Plug in your email service here to send an email. 
     // line below was commented out and replaced upon tutorial request 
     //return Task.FromResult(0); 
     await configSendGridasync(message); 
    } 
    // Use NuGet to install SendGrid (Basic C# client lib) 
    private async Task configSendGridasync(IdentityMessage message) 
    { 
     var myMessage = new SendGridMessage(); 
     myMessage.AddTo(message.Destination); 
     myMessage.From = new System.Net.Mail.MailAddress(
          "[email protected]", "Robert"); 
     myMessage.Subject = message.Subject; 
     myMessage.Text = message.Body; 
     myMessage.Html = message.Body; 

     var credentials = new NetworkCredential(
        ConfigurationManager.AppSettings["mailAccount"], 
        ConfigurationManager.AppSettings["mailPassword"] 
        ); 

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

     // Send the email. 
     if (transportWeb != null) 
     { 
      await transportWeb.DeliverAsync(myMessage); 
     } 
     else 
     { 
      Trace.TraceError("Failed to create Web transport."); 
      await Task.FromResult(0); 
     } 
    } 
} 

的AccountController:

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; 
      var result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 
       // commented below code and RedirectToAction out so it didn't auto log you in. 
       //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>"); 

       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); 
    } 

Web.config文件:

<appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 

    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 

    <add key="mailAccount" value="[email protected]" /> <!--the mailAccount that SendGrid gave me through Azure marketplace to use--> 
    <add key="mailPassword" value="xxxxxx" /> <!--password taken out, but I used the one from SendGrid--> 
</appSettings> 

的代碼生成,並沒有錯誤運行,但我在測試時從未收到實際的電子郵件(我使用了兩個單獨的Gmail帳戶和一個雅虎會員'mount)。任何意見/幫助將不勝感激!

+1

我曾與sendgrid從我的本地開發機發送的問題,但一旦我把它部署到服務器/主機按預期工作。 (爲什麼它的價值) –

+1

不是100%確定SendGrid方面可用的東西,但它看起來像是某種儀表板。那裏有任何跡象? – mxmissile

+0

@GlennFerrie我想我現在可以嘗試部署,但是我在做之前想要更多的成品。 我查看了從Azure網站的SendGrid的信息,並沒有真正發現任何東西。我會看起來更多一點。感謝您的建議! –

回答

2

看來你可以使用dotNet MailMessageSmtpClient在web.config文件中使用<system.net> <mailSettings> <smpt>這樣配置。

發送:

var mailMessage = new MailMessage(...); 
    var smtpClient = new SmtpClient(); 
    smtpClient.Send(message); 

配置爲SendGrid在你的.config:

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
      <network host="smtp.sendgrid.net" password="PASS`" 
        userName="[email protected]" port="587" /> 
     </smtp> 
    </mailSettings> 
</system.net> 
+0

謝謝@Artyom提供的建議答案!我對Visual Studio MVC相對來說比較陌生,並且無法找到將建議的更改放在哪裏。我假設我會在web.config中添加.config信息,而不是我的,但我無法將「發送」部分放在哪裏。我假設它會取代大部分IdentityConfig.cs,但是當我嘗試切換時,我遇到了很多構建錯誤。如果你可以包含任何關於如何將我的當前代碼與你的代碼結合的細節,我會非常感激。再次感謝! –

+0

是的,[''](https://msdn.microsoft.com/en-us/library/6484zdc1(v = vs.110).aspx)將被放到web.config的' 。 「發送」代碼,[SmtpClient](https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v = vs.110).aspx)(請參閱[msdn] (https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx)更多示例)將進入您的'UserManager.SendEmailAsync'方法。希望能幫助到你!請不要忘記投票答案。 – Artyom

相關問題