c# - 使用 SendGrid SMTP 电子邮件提交时出现 Bad Request 错误

标签 c# asp.net asp.net-mvc-4 .net-4.5

我目前在 MVC4 ASP.NET 4.5 中使用 C#。

我的应用程序创建了一个用户,然后向该用户提交了一封注册电子邮件,用于验证他们的电子邮件。为了创建这个脚本,我遵循了这个教程:How to Send Email Using SendGrid with Windows Azure

我正在使用最新版本的 SendGrid (2.1.1)

正在使用的代码特定命名空间:

using SendGridMail;
using System.Net;
using System.Net.Mail;

这是我创建 SendGrid 邮件消息的代码:

SendGrid message = SendGrid.GetInstance();
message.From = new MailAddress(fromEmail); //fromEmail is a MailAddress type
message.AddTo(userName); //this is a string
message.Html = body; //this is also a string

然后我去发邮件:

//Create login info for email
NetworkCredential credentials = new NetworkCredential("username", "password");
var trasnportSMTP = Web.GetInstance(credentials);
trasnportSMTP.Deliver(message); //smtp.sendgrid.net Send message

在“trasnportSMTP.Deliver(message);”的最后一行我收到此错误:

Server Error in '/' Application.

Bad Request

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Bad Request

我在跟踪过程中唯一注意到的是查看详细信息和输出窗口中的异常快照。

我注意到: TargetSite = {Void CheckForErrors(System.Net.Http.HttpResponseMessage)}, ReturnType 是 {Name = "Void"FullName = "System.Void"}, ReflectedType 是 {Name = "Web"FullName = "SendGridMail.Web"}, 自定义属性 - 计数 = 0, 名称 = "CheckForErrors", 它位于模块 {SendGridMail.dll} 中。

在我的输出中,还有以下内容:

"A first chance exception of type 'System.FormatException' occurred in System.dll Step into: Stepping over non-user code 'SendGridMail.Web.Deliver'"

任何人都可以让我更深入地了解为什么会发生此错误。我使用的是 Azure 上 SendGrid 帐户上的正确用户名和密码。

提前谢谢你。

最佳答案

尝试将主题添加到您的消息中

message.Subject = "this is the subject";

应该可以了,我在玩这个库的时候遇到了同样的问题。

关于c# - 使用 SendGrid SMTP 电子邮件提交时出现 Bad Request 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892185/

相关文章:

c# - 如何分离 double 和字符串项,出现无效的转换错误

c# - Wpf 的 InteropBitmap 连同 GDI+ : high cpu usage

c# - 使用 ASP.NET C# 在 AD 中创建用户时无法使用 DirectoryEntry.Invoke 设置密码

c# - 从选择选项标签中检索文本(不是值)

asp.net-mvc-3 - MVC3 - 4 使用部分 View 的多种形式

c# - 研究使用 C# 在目录及其子目录中高效搜索文本

c# - Linq 查询按组求和

jquery - 使用 jQuery 动态改变进度条的颜色

c# - ASP.NET如何将ASPX文件解析为可执行代码?

html - 使用 Chosen jquery 后,不会收到 @Html.DropDownListFor() 的验证消息