ASP.NET MVC sendgrid电子邮件传送失败

时间:2016-05-08 15:05:08

标签: asp.net asp.net-mvc email sendgrid

我正在尝试在我的ASP.NET MVC应用程序中发送标准帐户验证的标准电子邮件,但是电子邮件未发送甚至已发送...我的sendgrid仪表板上的所有设置都设置为默认值,< / p>

在sendgrid仪表板菜单中的“IP访问管理”选项卡上,我在“最近访问尝试次数”列表中看到了我的IP地址,因此我认为我的应用程序中的连接正在尝试建立......

我正在尝试通过生成的API密钥从我在Azure上托管的网站进行连接。

我正在使用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("Joe@contoso.com", "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");

}

问题出在哪里?

提前致谢。

1 个答案:

答案 0 :(得分: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);
    }
}

试试这个兄弟。

<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>

将应用设置存储在web.config文件中......