userone
userone

Reputation: 321

Sendgrid Attachment contains broken text

Hi I'm attaching a file (plain old .txt) to an email and sending it via Sendgrid.

Although the file is attached, when I open it, it contains broken text/gibberish.

var sendGridMessage = new SendGridMessagae();
sendGridMessage.AddTo(receiverEmail, receiverName);
sendGridMessage.From = new EmailAddress(senderEmail, senderName);
sendGridMessage.Subject = subjectName;
sendGridMessage.PlainTextContent = plainText;
sendGridMessage.HtmlContent = htmlText;

sendGridMessage.AddAttachment(fileName, "test");

Opening the attached file contains the following text: µë-.

Any idea what's causing this?

FYI. When I add a more complex string like:

"\"blah\", \"blah\", \"blah\"

the send fails with a BadRequest.

Upvotes: 0

Views: 398

Answers (1)

userone
userone

Reputation: 321

I figured out what the issue is. SendGrid doesn't let just regular string be used in the AddAttachment method, I had to first encode the string into a byte array before converting to base 64 string.

EX.

sendGridMessage.AddAttachment(fileName, "test") // no good

var content = Encoding.ASCII.GetBytes("test");

sendGridMessage.AddAttachment(fileName, Convert.ToBase64String(content)) //works

Upvotes: 1

Related Questions