Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendGrid throws "Error while copying content to a stream"

I'm creating a Core 2.1 solution in Visual Studio 2017 where I send emails via Sendgrid. When trying to send an email via SendGrid, I get the following error:

An unhandled exception occurred while processing the request.

IOException: The server returned an invalid or unrecognized response. System.Net.Http.HttpConnection.FillAsync() HttpRequestException: Error while copying content to a stream.

System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)

Here is what my code looks like. I'm entering the following in parameters:

recipients: list containing "****@gmail.com"

Subject: "Hello"

Body: Html generated via Heml. It compiles in an online editor without problem.

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;
}

What is the cause of this error?

like image 445
C_Karlslund Avatar asked Dec 08 '22 13:12

C_Karlslund


2 Answers

It turns out that there is a known bug in Sendgrid. If the html content entered is very big, the correct error message will not be sent. Instead, this error will show up. In my case, my apiKey was not found and I should therefore have gotten an Unauthorized error message. When I changed my html into one much smaller, this gave me the correct error.

Read more about the issue here.

like image 89
C_Karlslund Avatar answered Dec 10 '22 03:12

C_Karlslund


I'm having the same problem, but in my case it's because the email quota has expired (I'm using the free version for testing): enter image description here

Lib version 9.21.0

On the mentioned thread https://github.com/sendgrid/sendgrid-csharp/issues/648 some people were passing the wrong apiKey, and others exceeding the HTML size.

As said in the thread above, apparently it's a bug in the sendgrid library when interpreting the error.

Therefore, in addition to the problems mentioned (passing the wrong apiKey, exceeding the HTML size) the return “Error while copying content to a stream” is hiding other problems as well (like in my case: quota expired).

like image 43
Maicon Heck Avatar answered Dec 10 '22 01:12

Maicon Heck