c# - SendGrid 抛出“将内容复制到流时出错”
问题描述
我正在 Visual Studio 2017 中创建一个 Core 2.1 解决方案,我通过 Sendgrid 发送电子邮件。尝试通过 发送电子邮件时SendGrid
,我收到以下错误:
处理请求时发生未处理的异常。
IOException:服务器返回无效或无法识别的响应。System.Net.Http.HttpConnection.FillAsync() HttpRequestException:将内容复制到流时出错。
System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(任务 serializeToStreamTask,MemoryStream tempBuffer)
这是我的代码的样子。我在参数中输入以下内容:
收件人:包含“****@gmail.com”的列表
主题:《你好》
正文:通过 Heml 生成的 Html。它在在线编辑器中编译没有问题。
private async Task<bool> SendAsync(List<string> recipients, string subject, string body)
{
var client = new SendGridClient(this.configuration["Sendgrid:ApiKey"]);
var from = new EmailAddress(
this.configuration["Administration:MainEmailAddress"],
this.configuration["Administration:MainEmailName"]);
var tos = await GetRecipientsForEnvironment(recipients);
var message = MailHelper.CreateSingleEmailToMultipleRecipients(
from,
tos,
subject,
"",
body,
false);
var response = await client.SendEmailAsync(message);
return response.StatusCode == HttpStatusCode.Accepted;
}
这个错误的原因是什么?
解决方案
事实证明,在Sendgrid
. 如果输入的 html 内容很大,将不会发送正确的错误信息。相反,会出现此错误。就我而言,apiKey
没有找到我的,因此我应该收到一条Unauthorized
错误消息。当我将我的 html 更改为更小的 html 时,这给了我正确的错误。
在此处阅读有关该问题的更多信息。
推荐阅读
- php - apache 服务器如何知道如何编写未在服务器配置中设置的 PHP 页面?
- python - 如何在不显式转义的情况下保存字符串?
- asp.net-core - 从视图调用操作方法以自动填充表单值
- google-api - 如何创建 OAuth 客户端 ID?
- arrays - 查找对象数组的索引
- javascript - 将应用程序部署到 Heroku 在后端 Java Spring Boot 中引发 500 内部服务器错误,而在本地环境中完美运行
- caching - 当使用“graphql-tools”进行模式拼接时,可以使用 Apollo Server 缓存吗?
- python - 无法将字符串列表转换为二进制
- c# - Visual Studio C#pictureBox - 在 From 构造函数中绘图不显示
- python - Tweepy / StreamListener