2016-08-16 22 views
1

私はこのチュートリアルに従っています:http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset。私は今3回それを通過し、いくつかの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> 

私はそれをテストすると実際の電子メールを得ることはありません(私は2つの別々のgmailアカウントとyahoo accオント)。どんな助言/助けもありがとう!

+1

を/私は私の地元のdevのマシンから送信sendgridに問題があったが、私はそれをサーバーに展開した後それが期待どおりに機能するホスト。 (その価値のために) –

+1

SendGrid側で何が利用できるのかを100%確かめることはできませんが、何らかの種類のダッシュボードがあるようです。そこにどんな徴候がありますか? – mxmissile

+0

@GlennFerrie私は今見たいと思っていますが、私はそれをする前にもっと完成した製品を望んでいました。 私はAzureのウェブサイトからSendGridの情報を見てきましたが、本当に何も見つかりませんでした。私はもう少し見ていきます。提案していただきありがとうございます! –

答えて

2

dotnet MailMessageSmtpClientは、web.configファイルで<system.net> <mailSettings> <smpt>を使用して設定しているようです。

送信:あなたの.configでSendGridため

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

構成:

<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には比較的新しく、提案された変更をどこに置くべきかを見つけるのが難しいです。私はの代わりに.config情報をweb.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

関連する問題