Repository navigation
PDF Attachments That Worked with v6.3.4 Generate a 415 Unsupported Media Type with v7.0.2 #244
Description
Activity
- changed the title
[-]PDF Attachments That Worked with v6.3.4 Generate an 415 Unsupported Media Type with v7.0.2[/-][+]PDF Attachments That Worked with v6.3.4 Generate a 415 Unsupported Media Type with v7.0.2[/+]on Jun 17, 2016 Hello @DonWMason,
Thanks for submitting the issue with detail, that is much appreciated!
I'm thinking the issue is that attachment.Type should be "application/pdf".
What is the value of attachmentMimeType?
Thanks!
Elmer
- addedtype: questionquestion directed at the libraryquestion directed at the library
on Jun 17, 2016 I'm passing it System.Net.Mime.MediaTypeNames.Application.Pdf which is "application/pdf".
I switched back to an easier test without an attachment and it seems I might have had conflicting issues.
- Issue 1: UnsupportedMediaType
- Issue 2: BadRequest
Working on issue 2 using a Chrome's Simple REST Client, I discovered that I cannot submit an email that has the same address in the To and Bcc fields. I'm fairly certain I've tested with this scenario on the older version and it wasn't an issue. I set the Bcc to an alias for that email address and solved that problem.
Went back to Issue #1 and that still seems to be broken. For the record, how should an attachment be encoded before being stored in the Attachment object?
Thanks for the follow up @DonWMason.
The attachment needs to be Base64 encoded.
Here is the complete documentation for the v3 /mail/send endpoint: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
Changed the code to:
string attachmentMimeType = System.Net.Mime.MediaTypeNames.Application.Pdf;
byte[] pdfBuf = null;
pdfBuf = <>;
List fileNames = new List();
List byte[]= new List();
fileNames.Add("Test.pdf")
attachments.Add(pdfBuf);
.
.
.
attachment.Content = Convert.ToBase64String(attachments[index]);The last change is critical. I used to pass in a MemoryStream attachment directly with v6.x:
message.AddAttachment(ms, fileName);Now with 7.0.2 we're forced to convert to Base64 before passing in the attachment.
Any chance of getting some overloads on the Attachment object?
Still don't see any reference to Base64 at all in that link. I read it several times yesterday and today.
Oh, need to click that caret on the right for the important info ;-)
FYI, the browser search in Chrome doesn't find the text unless the caret is clicked.
Yes, will you please submit a new issue for that feature?
Also, thanks for providing a solution and great feedback! Please email us your T-shirt size at dx@sendgrid.com so we can send you some swag.
It would be implemented as part of our Mail Helper: https://github.com/sendgrid/sendgrid-csharp/tree/master/SendGrid/SendGrid/Helpers/Mail
I'll pass your feedback regarding the documentation to the docs team, thanks again!
@DonWMason I have created a ticket regarding making this experience better here: #248
Thanks again for your support and feedback!
You're very welcome! I had been working on updating that client for 2 days.
Glad to have gotten it up and running by COB on Friday! Thanks for your
help!Don Mason
503-888-3355On Mon, Jun 20, 2016 at 11:10 AM, Elmer Thomas notifications@github.com
wrote:@DonWMason https://github.com/DonWMason I have created a ticket
regarding making this experience better here: #248
#248Thanks again for your support and feedback!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#244 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AFtB3Q9s2CdYs8CEBltgYVIdsvrzkStpks5qNteBgaJpZM4I4voH
.Reacted by Elmer ThomasJust in case this could be of any help!!!
Instead of making:
System.Text.Encoding.UTF8.GetString(pdfBuf);I did a:
attachment.Content = Convert.ToBase64String(pdfBuf);And it worked like Charm!
Issue Summary
PDF attachments that worked immediately before updating to v7.0.2 from v6.3.4 are now generating a 415 Unsupported Media Type.
Steps to Reproduce
byte[] pdfBuf = null;
pdfBuf = <>;
List fileNames = new List();
List attachments = new List();
fileNames.Add("Test.pdf")
attachments.Add(System.Text.Encoding.UTF8.GetString(pdfBuf));
SendGrid.Helpers.Mail.Mail mail = new SendGrid.Helpers.Mail.Mail();
SendGrid.Helpers.Mail.Personalization personalization = new SendGrid.Helpers.Mail.Personalization();
SendGrid.Helpers.Mail.Email email = new SendGrid.Helpers.Mail.Email();
// From
from = "webmaster@fmtsolutions.com";
email.Address = from;
mail.From = email;
// To
email = new SendGrid.Helpers.Mail.Email();
email.Name = "Xxx Xxx";
email.Address = "xxx@xxx.com".Trim();
personalization.AddTo(email);
// Subject
personalization.Subject = subject;
// Load personalization
mail.AddPersonalization(personalization);
// Body
SendGrid.Helpers.Mail.Content content = new SendGrid.Helpers.Mail.Content();
content.Type = "text/html";
content.Value = body;
mail.AddContent(content);
// Attachments
foreach (string fileName in fileNames)
{
SendGrid.Helpers.Mail.Attachment attachment = new SendGrid.Helpers.Mail.Attachment();
attachment.Content = attachments[index];
attachment.Type = attachmentMimeType;
attachment.Filename = fileName;
//attachment.Disposition = "attachment";
//attachment.ContentId = "Attachment" + (index).ToString();
mail.AddAttachment(attachment);
index++;
}
string sendGridAPI = ConfigurationManager.AppSettings["SendGridAPIv3"];
dynamic sg = new SendGridAPIClient(sendGridAPI, "https://api.sendgrid.com", "v3");
string requestBody = mail.Get();
dynamic response = sg.client.mail.send.post(requestBody: requestBody);