API deny my request when i add image in the html of the mail.
I am using this example: sendgrid-csharp/SendGrid/Example/Example.cs and helper class: SendGrid.Helpers.Mail.Email
And this source: https://sendgrid.com/blog/embedding-images-emails-facts/
`
var mailFrom = new Email("[email protected]");
var mailTo = new Email("[email protected]");
Content content = new Content("text/html", "does not matter what is here");
Mail mail = new Mail(mailFrom, "XXX", mailTo, content);
mail.TemplateId = "template_id";
mail.Personalization[0].Substitutions = PrepareSubstitutionsDictionary();
var base64image = "data:image/png;base64,/sdfsjf2j3flkjwj....";
mail.AddAttachment(new Attachment()
{
Content = base64image,
ContentId = "qrTicket",
Disposition = "inline",
Filename = "qrTicket.png",
Type = "image/png"
});
`
HTML snippet from sendgrid template:
/< img src="cid:qrTicket" //>
Sending the mail with the snippet above results in:
response.StatusCode : BadRequest
response.Body.ReadAsStringAsync().Result : "{\"errors\":[{\"message\":\"The attachment content must be base64 encoded.\",\"field\":\"attachments.0.content\",\"help\":\"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.attachments.content\"}]}"
esponse.Headers.ToString(): "Connection: keep-alive\r\nX-Frame-Options: DENY\r\nDate: Fri, 12 Aug 2016 07:37:07 GMT\r\nServer: nginx\r\n"
@ivivanov,
You will need to encode your file using this: https://msdn.microsoft.com/en-us/library/system.convert.tobase64string(v=vs.110).aspx
From your example, it looks like you have some additional information in your base64image that is not base64 encoded.
@thinkingserious, thanks for the answer. It guided me to try few things and it is now working. The only thing i did is to remove the metadata from the base 64 encoding.
this: "data:image/png;base64,/sdfsjf2j3flkjwj...." is now: "/sdfsjf2j3flkjwj....".
Awesome! Thanks for sharing the final solution!
Most helpful comment
@thinkingserious, thanks for the answer. It guided me to try few things and it is now working. The only thing i did is to remove the metadata from the base 64 encoding.
this: "data:image/png;base64,/sdfsjf2j3flkjwj...." is now: "/sdfsjf2j3flkjwj....".