SendGrid教程导致错误的请求(SendGrid Tutorial resulting in Bad Request)

编程入门 行业动态 更新时间:2024-10-10 18:23:14
SendGrid教程导致错误的请求(SendGrid Tutorial resulting in Bad Request)

如果这是一个愚蠢的问题,我很抱歉,但是我没有在这个网站或其他网站上发现任何有关此问题的可靠信息。

据说,我正在开发一个MVC 5 web应用程序。 我正在关注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); } }

错误的请求

说明:执行当前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: }

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?

最满意答案

这可能是您的凭据有问题。

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

登录到Azure Portal 导航到Marketplace 找到并单击SendGrid应用程序 在底部,点击Connection Info 使用列出的Username和Password 。

我最初的印象是我使用我的Azure帐户密码,直到找到它。 希望这可以纠正你的问题,就像它为我做的那样。

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.

更多推荐

本文发布于:2023-07-06 07:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   教程   SendGrid   Tutorial   Request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!