Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error "The attachment content must be base64 encoded" while sending pdf in zip as an attachment using SendGrid #729

Open
surajintellicus opened this issue Apr 4, 2022 · 0 comments

Comments

@surajintellicus
Copy link

surajintellicus commented Apr 4, 2022

We are using SendGrid latest JAVA API's available to send emails. As part of attachment we are attaching a pdf enclosed in a zip file.
While sending mail we are getting 404 as response code and error message stating The attachment content must be base64 encoded
Complete Error Message: {"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"}]}

Wanted help if we are doing anything wrong and need more clarity how can we solve this issue.
Please find attached code snippet.

Code Snippet :

Path file = Paths.get(this.file.getAbsolutePath());
Attachments attachments = new Attachments();
attachments.setFilename(Base64.getMimeEncoder().encodeToString(file.getFileName().toString().getBytes(StandardCharsets.UTF_8)));
//attachments.setType("application/pdf");
attachments.setDisposition("attachment");
byte[] attachmentContentBytes = Files.readAllBytes(file);
String attachmentContent = Base64.getMimeEncoder().encodeToString(attachmentContentBytes);
attachments.setContent(attachmentContent);
mail.addAttachments(attachments);

com.sendgrid.SendGrid sg = new com.sendgrid.SendGrid(this.apiKey);
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);

Error :

Complete Error Message: {"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"}]}

Using Maven Repo :

com.sendgrid
sendgrid-java
4.9.1

Sheet 1(1).zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant