Sendgrid-csharp: Attachments with accents in the name result in an error from SendGrid

Created on 11 Apr 2014  Â·  23Comments  Â·  Source: sendgrid/sendgrid-csharp

When sending attachments with accents in the file name, SendGrid responds with the following error:

<result>
  <message>error</message>
  <errors>
    <error>Parameter =?utf-8?B?ZmlsZXNbTVBHw5bDicOLLnBkZl0=?= is not utf8</error>
  </errors>
 </result>

Sample code for adding the attachment to reproduce the error above is:

C# mailMessage.AddAttachment(fs, "MPGÖÉË.pdf");

Is this a bug in the library or with SendGrid? Or should the file name be encoded or escaped in some way?

Thanks.

unknown or a waiting for feedback question

Most helpful comment

@brandonmwest is on a very good track mentioning RFC 2047. I was able to use RFC 2047 section 2, Syntax Of Encoded Words, in order to attach emails whose file name had an umlaut vowel.

What I did, using the C# functionality, was to

string encodedFileName = "=?UTF-8?B?" + Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes("namewithspecials.pdf")) + "?=";

into the
message's AddAttachment(stream, encodedFileName) method.

This technique also works when rolling headers more manually with the Content-Disposition and the Name property "files[" + encodedFileName + "]". Don't forget to use the FileNameStar property Microsoft provided for the ContentDispositionHeaderValue class, too.

I do not know whether it would work for the "To" header. I've not considered that in reading any RFC's.

Of course, when and if, the API is "fixed" it will have to consider these types of workarounds.

All 23 comments

I tested the SendGrid Web API via curl and it appears to handle the encoding fine
screen shot 2014-05-22 at 6 42 17 am

So it is an issue with the library. I will investigate. Thanks!

Any progress on this issue? Same problem occurs when using the norwegian characters æ ø å.

It works ok when only using ascii-characters.

Relevant portion of the http request grabbed with Fiddler:

--c3176356-64a7-4536-9854-6c3ec2cde739
Content-Disposition: form-data; name="=?utf-8?B?ZmlsZXNbQW5sZWdnX21lZF8xX23DpWxlci0yMDE0LjA1LjAyLUFra3JlZGl0ZXJ0LVByw7h2ZXV0dGFrLTEwMzItR29ka2plbnQucGRmXQ==?="; filename="=?utf-8?B?QW5sZWdnX21lZF8xX23DpWxlci0yMDE0LjA1LjAyLUFra3JlZGl0ZXJ0LVByw7h2ZXV0dGFrLTEwMzItR29ka2plbnQucGRm?="
Content-Type: application/octet-stream

%PDF-1.4
....

I think I've got a solution for this. I need to do some more testing today but it's looking promising.

I've checked the MIME messages being built in the library and compared them to what gets sent via curl.

curl just sends the unicode chars:

Content-Disposition: form-data; name="files[MPG���.txt]"; filename="test�.txt"

but they are encoded as part of the mime message created in the library:

Content-Disposition: form-data; name="=?utf-8?B?ZmlsZXNbdGVzdMO4LnR4dF0=?="; filename="=?utf-8?B?dGVzdMO4LnR4dA==?="

So now I just need to figure out why :)

The API doesn't decode RFC 2047 encoded headers, nor URL encoded characters. And there's no way to disable the UTF-8 base64 encoding that HttpClient performs. To fix this will require refactoring the code to use HttpWebRequest instead, which can pass unicode directly to the API as we need. I don't see any issues with doing this so I am going to proceed.

Trying to see if we can add support for RFC 2047 to the API.

What happened? It looks like this is still an issue.

I'm still working on getting things supported via the API. At the moment it's not something we can accomplish in the library -- I looked at refactoring to HttpWebRequest and for a number of reasons it ended up being a bad idea. The best solution right now is to add a function that replaces unicode chars in filenames with ascii.

I'm closing this because I've determined we can't address it at the library level. Still working on getting it fixed.

Hello
I need to ask, is there any progress in this bug?

Cheers
Janus

Yep, still not fixed. I'm using C# Sendgrid and I try to send a an attachment containing the Swedish letter Ö and the received mail isn't containing any attachment at all.

@HugCoder, @janus007 Could you both please report this issue over at [email protected]? That would be very helpful toward gaining resolution. Thanks!

@thinkingserious OK, I reported it to them now. It's not a "game breaking" issue for me, but it would of course be nice and perhaps more modern to support UTF-8 names. ANSI only feels pretty outdated these days.

Thank you!

I already got an answer:
"
We are aware of the issue of attachments with special characters not working. Unfortunately I don't have a timeline of of when it will be fixed. Thank you for keeping us informed about this and I will pass it on.
"
So, they are aware and working on it! Nice to hear!

Would be great if you could fix this :-)

There are email addresses with the Swedish 'ö' which I can't get to work with the sendgrid API. This means that a portion of our recepients can never receive emails from Sendgrid.

@brandonmwest is on a very good track mentioning RFC 2047. I was able to use RFC 2047 section 2, Syntax Of Encoded Words, in order to attach emails whose file name had an umlaut vowel.

What I did, using the C# functionality, was to

string encodedFileName = "=?UTF-8?B?" + Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes("namewithspecials.pdf")) + "?=";

into the
message's AddAttachment(stream, encodedFileName) method.

This technique also works when rolling headers more manually with the Content-Disposition and the Name property "files[" + encodedFileName + "]". Don't forget to use the FileNameStar property Microsoft provided for the ContentDispositionHeaderValue class, too.

I do not know whether it would work for the "To" header. I've not considered that in reading any RFC's.

Of course, when and if, the API is "fixed" it will have to consider these types of workarounds.

Thanks for sharing a workaround @markbarkell!

Do you mind emailing us at [email protected] with your mailing address, phone number and T-shirt size, so we can send you some swag?

Still seems there is an issue with special characters "Æ,Ø,Å" in attachment file names. Hope to see it resolved at some point.

Hi @kekwin,

Do you mind opening a new ticket with the code that you are having trouble with?

I was unable to reproduce with the following code:

using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;
using System.IO;

namespace Example
{
    internal class Example
    {
        private static void Main()
        {
            Execute().Wait();
        }

        static async Task Execute()
        {
            var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress("[email protected]");
            var subject = "Subject";
            var to = new EmailAddress("[email protected]");
            var body = "Email Body";
            var msg = MailHelper.CreateSingleEmail(from, to, subject, body, "");
            var bytes = File.ReadAllBytes("/temp/ÆØÅ.txt");
            var file = Convert.ToBase64String(bytes);
            msg.AddAttachment("ÆØÅ.txt", file);
            var response = await client.SendEmailAsync(msg);
        }
    }
}

I have this issue with ÆØÅ in Xamarin projects on Android (have not tested with iOS). I am testing the solution from @markbarkell this moment. I do not know if I can take out a sample to test, but I thought it would be prudent to inform that with ÆØÅ in the file name no attachments are delivered, while without we experience no problems. And differentiating from the sample above we are on Xamarin.

Thanks for the heads up @petero-dk! If @markbarkell's solution does not work for you, please create a new issue in this repo.

With Best Regards,

Elmer

Whoops, forgot to report back, with @markbarkell solution it works like a charm.

Was this page helpful?
0 / 5 - 0 ratings