使用 SendGrid SMTP 电子邮件提交的错误请求错误 [英] Bad Request error using SendGrid SMTP email submission
问题描述
我目前在 MVC4 ASP.NET 4.5 中使用 C#.
I am currently using C# in MVC4 ASP.NET 4.5.
我的应用程序创建一个用户,然后向该用户提交注册电子邮件以验证他们的电子邮件.为了创建这个脚本,我遵循了这个教程:如何在 Windows Azure 中使用 SendGrid 发送电子邮件
My application creates a user and then submits a registration email to that use to authenticate their email. To create this script, I followed this tutorial: How to Send Email Using SendGrid with Windows Azure
我使用的是最新版本的 SendGrid (2.1.1)
I am using the latest version of SendGrid (2.1.1)
正在使用的代码特定命名空间:
Code specific namespaces being used:
using SendGridMail;
using System.Net;
using System.Net.Mail;
这是我创建 SendGrid 邮件消息的代码:
Here is my code that creates the SendGrid mail message:
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);"的最后一行我收到此错误:
On the last line where "trasnportSMTP.Deliver(message);" I get this error:
/"应用程序中的服务器错误.
Server Error in '/' Application.
错误请求
说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以了解更多信息有关错误及其在代码中的来源的信息.
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.
异常详细信息: System.Exception:错误请求
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,Name = "CheckForErrors",它位于模块 {SendGridMail.dll} 中.
I noticed that the: TargetSite = {Void CheckForErrors(System.Net.Http.HttpResponseMessage)}, ReturnType is {Name = "Void" FullName = "System.Void"}, ReflectedType is {Name = "Web" FullName = "SendGridMail.Web"}, CustomAttributes - Count = 0, Name = "CheckForErrors", It's in the Module {SendGridMail.dll}.
也在我的输出中,有以下内容:
Also in my output, there is the following:
"发生了 'System.FormatException'
类型的第一次机会异常在 System.dll 步入:单步执行非用户代码'SendGridMail.Web.Deliver'
"
"A first chance exception of type
'System.FormatException'
occurred in System.dll Step into: Stepping over non-user code'SendGridMail.Web.Deliver'
"
任何人都可以让我更深入地了解为什么会发生此错误.我使用的是 Azure 上 SendGrid 帐户上的正确用户名和密码.
Can anyone give me some more insight on why this error is happening. I am using the correct username and password that is on the SendGrid account on Azure.
提前谢谢你.
推荐答案
尝试将主题添加到您的消息中
Try adding the subject to your message
message.Subject = "this is the subject";
应该可以,我在玩这个库时遇到了同样的问题.
That should do it, I ran into the same problem when I was playing with this library.
这篇关于使用 SendGrid SMTP 电子邮件提交的错误请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!