2016-05-08 12 views
1

ASP.NET MVCアプリケーションで標準のアカウント確認用の標準電子メールを送信しようとしていますが、電子メールは配信されず、送信されません。 sendgridダッシュボードがデフォルトに設定されていますASP.NET MVC sendgridの電子メール配信が失敗する

sendgrid Dashboardメニューの「IPアクセス管理」タブで、私のIPアドレスが「最近のアクセス試行回数」リストに表示されるので、私のアプリケーションからの接続が確立しようとしています。

Azureでホスティングしているサイトから生成されたAPIキーを使用して接続しようとしています。

私はSendgrid C#クライアントライブラリv6.3.4を使用しています。とSendgrid Smtp.Api v1.3.1。ここでNuGet

を介してインストールは私のサンプルコードです:

public class EmailService : IIdentityMessageService 
{ 
    public async Task SendAsync(IdentityMessage message) 
    { 
     await configSendGridasync(message); 

    } 
    private async Task configSendGridasync(IdentityMessage message) 
    { 
     var myMessage = new SendGridMessage(); 
     myMessage.AddTo(message.Destination); 
     myMessage.From = new MailAddress("[email protected]", "Joe S."); 
     myMessage.Subject = message.Subject; 
     myMessage.Text = message.Body; 
     myMessage.Html = message.Body; 

     var transportWeb = new Web("SG.sendgrid general api key blah blah blah"); 

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

これは私のレジスタコントローラです:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> _Register(BigViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     var user = new ApplicationUser { UserName = model.RegisterViewModel.Email, Email = model.RegisterViewModel.Email }; 
     var result = await UserManager.CreateAsync(user, model.RegisterViewModel.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"); 

     } 
     AddErrors(result); 
    } 

    return View("Register"); 

} 

問題がありますか?

ありがとうございます。

+0

可能な重複http://stackoverflow.com/questions/35865859/asp- net-mvc-sendgrid-account-email-verification) –

答えて

0
private async Task configSendGridasync(IdentityMessage message,FormCollection fc) 
{ 
    var myMessage = new SendGridMessage(); 
    myMessage.AddTo(message.Destination); 
    myMessage.From = new MailAddress("YourEmail", "Joe S."); 
    myMessage.AddTo(fc["Email"]); 
    myMessage.Subject = message.Subject; 
    myMessage.Text = message.Body; 
    myMessage.Html = message.Body; 
    var credentials = new NetworkCredential(
      ConfigurationManager.AppSettings["mailAccount"], 
      ConfigurationManager.AppSettings["mailPassword"] 
      ); 
    var transportWeb = new Web(credentials); 

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

この仲間をお試しください。 web.configファイル内のアプリの設定

<appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <!-- Markup removed for clarity. -->  
    <add key="mailAccount" value="xyz" /> 
    <add key="mailPassword" value="password" /> 
</appSettings> 

店...

[ASPネットMVC sendgridアカウントの電子メールの確認](の
+0

よろしくお願いします。私は前にこのアプローチを試しましたが、結果はありません。確かに...ウェブ設定では、 "メールアカウントの値"プロパティはsengridアカウントのログイン用です。 –

+0

それが正しい@joint_ops –

+0

@joint_opsすみません....私はスニペットを更新しました... myMessage.AddTo( "[email protected]");もう一度やり直してみてください... –

関連する問題