首页 > 解决方案 > 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;
}

这个错误的原因是什么?

标签: c#sendgridasp.net-core-2.1

解决方案


事实证明,在Sendgrid. 如果输入的 html 内容很大,将不会发送正确的错误信息。相反,会出现此错误。就我而言,apiKey没有找到我的,因此我应该收到一条Unauthorized错误消息。当我将我的 html 更改为更小的 html 时,这给了我正确的错误。

在此处阅读有关该问题的更多信息。


推荐阅读