SendGrid教程导致错误请求 [英] SendGrid Tutorial resulting in Bad Request
问题描述
我道歉,如果这是一个欺骗的问题,但我还没有发现这个问题无论是在本网站或对别人的任何可靠的信息。
I apologize if this is a dupe question, but I have not found any solid information about this issue either on this site or on others.
有了这样说,我在一个MVC 5 Web应用程序的工作。我下面<一个href=\"http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset\">this 的教程超过上ASP.net。
With that being said, I am working on an MVC 5 web application. I am following this tutorial over on ASP.net.
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);
}
}
每个它会在上述方法等待transportWeb.SendAsync(myMessage)
行的时候,这个错误在浏览器中显示:
Each time it gets to the await transportWeb.SendAsync(myMessage)
line in the above method, this error shows up in the browser:
错误请求
说明:当前Web请求的执行过程中发生未处理的异常。请检查堆栈跟踪有关该错误的详细信息以及它起源于code。
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.异常详细信息:的System.Exception:坏请求
Exception Details: System.Exception: Bad Request
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/ ,使用自由套餐谷歌,让我25000每月学分。该帐户已提供。
I signed up for a free account over at https://sendgrid.com/, using the "Free Package Google", giving me 25,000 monthly credits. The account has been provisioned.
我已经尝试了一堆东西,到目前为止,包括:禁用SSL,直接把用户名/密码在code,而不是从他们拉的 Resources.resx
文件中,指定的NetworkCredential
对象中的SMTP服务器,并且还试图改变 DeliverAsync(...)
到交付()
。
I have tried a bunch of things so far including: disabling SSL, putting username/password directly in the code instead of pulling them from the Resources.resx
file, specifying the SMTP server inside the NetworkCredential
object, and also tried changing DeliverAsync(...)
to Deliver()
.
我想明确地设置主题
而不是使用 message.Subject
,作为<一个href=\"http://stackoverflow.com/questions/21892185/bad-request-error-using-sendgrid-smtp-email-submission\">this帖子建议。我也试过 HttpUtility.UrlEn code
在 callbackUrl
在帐户生成/注册
方法<一个href=\"http://stackoverflow.com/questions/25212263/getting-bad-request-in-rick-andersons-$c$c-for-password-recovery\">suggested这里。同样的结果,很遗憾。
I tried explicitly setting the subject
instead of using message.Subject
, as this post suggested. I also tried HttpUtility.UrlEncode
on the callbackUrl
generated in the Account/Register
method as suggested here. Same results, unfortunately.
有谁恰好有一些见解,以什么可能会导致这种不正常?
Does anyone happen to have some insight as to what might be causing this to not function correctly?
推荐答案
我结束了使用内置的 SmtpClient
来得到这个工作。这里是code,我现在用:
I ended up using the built-in SmtpClient
to get this working. Here is the code that I am using:
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仪表板。
Even though it doesn't use SendGrid's C# API, the messages still show up on my SendGrid dashboard.
这篇关于SendGrid教程导致错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!