【问题标题】:SendGrid Tutorial resulting in Bad RequestSendGrid 教程导致错误请求
【发布时间】:2014-11-24 20:20:53
【问题描述】:

如果这是一个愚蠢的问题,我深表歉意,但我在本网站或其他网站上都没有找到有关此问题的任何可靠信息。

话虽如此,我正在开发一个 MVC 5 Web 应用程序。我在 ASP.net 上关注 this tutorial

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 System.Net.Mail.MailAddress(
                        "info@ycc.com", "Your Contractor Connection");
    myMessage.Subject = message.Subject;
    myMessage.Text = message.Body;
    myMessage.Html = message.Body;

    var credentials = new NetworkCredential(
         Properties.Resources.SendGridUser,
         Properties.Resources.SendGridPassword,
         Properties.Resources.SendGridURL // necessary?
         );

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

上述方法每次到达await transportWeb.SendAsync(myMessage)这一行,浏览器都会出现这个错误:


“/”应用程序中的服务器错误。

错误请求

说明:在执行当前 Web 请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详情:System.Exception:错误请求

Line 54:             if (transportWeb != null)
Line 55:             {
Line 56:                 await transportWeb.DeliverAsync(myMessage);
Line 57:             }
Line 58:             else
Line 59:             {
Line 60:                 Trace.TraceError("Failed to create Web transport.");
Line 61:                 await Task.FromResult(0);
Line 62:             }

我在https://sendgrid.com/ 注册了一个免费帐户,使用“Google 免费套餐”,每月可获得 25,000 个积分。帐户已配置。

到目前为止,我已经尝试了很多方法,包括:禁用 SSL、将用户名/密码直接放在代码中而不是从 Resources.resx 文件中提取它们、在 NetworkCredential 对象中指定 SMTP 服务器,并且还尝试过将DeliverAsync(...) 更改为Deliver()

我尝试明确设置subject,而不是像this post 建议的那样使用message.Subject。我还在Account/Register方法中生成的callbackUrl上尝试了HttpUtility.UrlEncodesuggested here。不幸的是,结果相同。

是否有人碰巧对可能导致此功能无法正常运行的原因有所了解?

【问题讨论】:

标签: c# asp.net-mvc email asp.net-mvc-5 sendgrid


【解决方案1】:

我最终使用了内置的SmtpClient 来完成这项工作。这是我正在使用的代码:

private async Task configSendGridasync(IdentityMessage message)
{
    var smtp = new SmtpClient(Properties.Resources.SendGridURL,587);

    var creds = new NetworkCredential(Properties.Resources.SendGridUser, Properties.Resources.SendGridPassword);

    smtp.UseDefaultCredentials = false;
    smtp.Credentials = creds;
    smtp.EnableSsl = false;

    var to = new MailAddress(message.Destination);
    var from = new MailAddress("info@ycc.com", "Your Contractor Connection");

    var msg = new MailMessage();

    msg.To.Add(to);
    msg.From = from;
    msg.IsBodyHtml = true;
    msg.Subject = message.Subject;
    msg.Body = message.Body;

    await smtp.SendMailAsync(msg);
}

即使它不使用 SendGrid 的 C# API,消息仍会显示在我的 SendGrid 仪表板上。

【讨论】:

  • var creds = new NetworkCredential(Properties.Resources.SendGridUser, Properties.Resources.SendGridPassword);这里没有找到 Properties.Resources.SendGridUser 和 Properties.Resources.SendGridPassword
  • @Rashedul.Rubel 嵌入在程序的资源文件中。它们是字符串值。我确实尝试在发布之前将它们进行硬编码,然后找到解决方案,然后发布。
  • 我完全按照这里的描述进行操作,但我得到:“服务器违反了协议”...任何帮助将不胜感激
  • @a.boussema 确保您使用的是 sendgrid 用户名而不是电子邮件地址。服务器应该是 smtp.sendgrid.net。也可以使用您的端口;我相信在我最终成功之前我尝试了三四次。
  • @Anders:非常感谢。我的问题是未支付账单 :)) 错误消息非常令人困惑,我不得不花几个小时才能弄清楚。
【解决方案2】:

您的凭据可能有问题。

如果您通过 Windows Azure 注册了 SendGrid,那么您需要执行以下操作:

  1. 登录您的Azure Portal
  2. 导航到Marketplace
  3. 找到并点击SendGrid应用程序
  4. 在底部,点击Connection Info
  5. 使用列出的UsernamePassword

我最初的印象是我要使用我的 Azure 帐户密码,直到我发现这个。希望这能像对我一样纠正您的问题。

【讨论】:

  • 另外,请注意,在该教程中,“mailAccount”设置应设置为您的 sendgrid 用户名,而不是帐户的电子邮件地址。
  • 生成的是用户名,而不是您创建 sendgrid 资源时提供的名称。
【解决方案3】:

检查您是否使用正确的“用户名”作为“mailAccount”设置。

这应该是您的 sendgrid 用户名,不是您尝试发送邮件的帐户的电子邮件地址。

【讨论】:

    【解决方案4】:

    我通过 Azure 创建了一个 SendGrid 帐户, 我通过在我的 Web.Config 文件中设置这些值来解决这个问题:

    <add key="mailAccount" value="azure_************@azure.com" />
    <add key="mailPassword" value="[My Azure Password]" />
    

    到我的天蓝色用户名和密码。我从 Azure 仪表板找到的用户名,我导航到 SendGrid 帐户 >> [单击我创建的资源] >> 配置。密码与我设置 Azure 帐户时使用的密码相同。

    【讨论】:

      【解决方案5】:

      我遇到了同样的错误。我所要做的就是复制 webconfig 中的 appsettings(见下文)并将其粘贴到 OTHER webconfig 文件中(其中 2 个在 asp.net 项目中)。

      <add key="webpages:Version" value="3.0.0.0" />
      <add key="mailAccount" value="xxUsernamexx" />
      <add key="mailPassword" value="Password" />
      

      【讨论】:

        【解决方案6】:

        我也遇到过这个问题。通过添加 textcontent 和 htmlcontent 来解决。在我发送空字符串之前。现在它正在工作 代码如下

        var client = new SendGridClient(_apiKey);
                    var from = new EmailAddress(_fromEmailAddress, _fromName);
                    var to = new EmailAddress("devanathan.s@somedomain.com", "dev");
                    var textcontent = "This is to test the mail functionality";
                    var htmlcontent = "<div>Devanathan Testing mail</div>";
                    var subject = "testing by sending mail";
                    var msg = MailHelper.CreateSingleEmail(from, to, subject, textcontent, htmlcontent);
                    var response = await client.SendEmailAsync(msg);
        

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题,碰巧拼错了 mailAccount 配置值的名称(用 mainAccount 代替 mailAccount)。

                  NetworkCredential credential = new NetworkCredential(ConfigurationManager.AppSettings["mailAccount"], ConfigurationManager.AppSettings["mailPassword"]);
                  Web transportWeb = new Web(credential);
          

          配置值返回为 null,但未引发异常,而是分配了空用户名。基本上,将断点放在“Web transportWeb = new Web(credential);”行上并查看您在凭据中实际传递的用户名/密码,并查看 nevada_scout 的答案。

          【讨论】:

            【解决方案8】:

            您在 SendGrid 中注册的公司域应该用于调用 MailAddress API。因此,如果您在 SendGrid 中注册的公司网站是 www.###.com,您应该使用:

            var from = MailAddress("info@###.com", "Your Contractor Connection") 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2022-10-20
              • 1970-01-01
              • 1970-01-01
              • 2020-04-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多