SendGrid教程导致错误请求 [英] SendGrid Tutorial resulting in Bad Request
问题描述
如果这是一个重复的问题,我很抱歉,但是我还没有在本网站或其他网站上找到关于这个问题的任何可靠的信息。
我正在研究一个MVC 5的Web应用程序。我正在追踪本教程在ASP.net上。
public async任务SendAsync(IdentityMessage消息)
{
await configSendGridasync(message);
}
私有async任务configSendGridasync(IdentityMessage消息)
{
var myMessage = new SendGridMessage();
myMessage.AddTo(message.Destination);
myMessage.From = new System.Net.Mail.MailAddress(
info@ycc.com,您的承包商连接);
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 //必需?
);
//创建用于发送电子邮件的Web传输。
var transportWeb = new Web(credentials);
//发送电子邮件。
if(transportWeb!= null)
{
await transportWeb.DeliverAsync(myMessage);
}
else
{
Trace.TraceError(无法创建Web传输);
等待Task.FromResult(0);
}
}
每次获得的行中,上面的方法,这个错误显示在浏览器中:
'/'应用程序中的服务器错误
不良请求
说明:执行当前Web请求时发生未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息及其在代码中的位置。
异常详细信息: System.Exception:错误请求第54行:if(transportWeb!= null)
第55行:{
第56行:等待transportWeb.DeliverAsync(myMessage);
行57:}
行58:else
行59:{
行60:Trace.TraceError(无法创建Web传输);
第61行:等待Task.FromResult(0);
第62行:}
我签了在 https://sendgrid.com/ 上使用Free Package Google免费帐户,给予我每月25,000学分。该帐户已经被设置。
我已经尝试了一些事情,包括:禁用SSL,将用户名/密码直接放在代码中,而不是从 Resources.resx
文件,指定 NetworkCredential
对象内的SMTP服务器,并尝试更改 DeliverAsync (...)
to Deliver()
。
我尝试明确设置主题
而不是使用 message.Subject
,作为这篇文章建议。我还在帐户/注册$中生成的
callbackUrl
上尝试了 HttpUtility.UrlEncode
c $ c>方法作为建议此处 。不幸的是,相同的结果。
有没有人会有什么可能导致这种功能无法正常工作?
我最终使用内置的 SmtpClient
来实现这一点。这是我使用的代码:
私有async任务configSendGridasync(IdentityMessage消息)
{
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,您的承包商连接);
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仪表板上。
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.
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);
}
}
Each time it gets to the await transportWeb.SendAsync(myMessage)
line in the above method, this error shows up in the browser:
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
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: }
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.
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()
.
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?
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);
}
Even though it doesn't use SendGrid's C# API, the messages still show up on my SendGrid dashboard.
这篇关于SendGrid教程导致错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!