2016-08-16 150 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

相关问题