获取错误“附件内容必须为基本64编码”。使用sendgrid将pdf作为附件发送ZIP时

发布于 2025-01-18 15:18:05 字数 1585 浏览 5 评论 0原文

我们正在使用SendGrid最新可用的Java API发送电子邮件。我们正在附上一个封闭在zip文件中的PDF。

在发送邮件时,我们正在收到404响应代码和错误消息,指出“附件内容必须是基本64编码”

完整错误消息:

{"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"}\]}

我们做错了什么?我们如何解决此问题并发送此附件?这是我们尝试以zip发送PDF的代码。

代码:

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);

响应:

{"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"}\]}

maven依赖性:

  • com.sendgrid.sendgrid-java 4.9.1

We are using SendGrid latest available Java API to send emails. We are attaching a PDF enclosed in a zip file.

While sending mail we are getting a 404 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"}\]}

What are we doing wrong? How can we solve this issue and send this attachment? Here is the code where we try to send the PDF in a Zip.

Code:

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);

Response:

{"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"}\]}

Maven dependencies:

  • com.sendgrid.sendgrid-java 4.9.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2025-01-25 15:18:05

尝试base64.getencoder()而不是base64.getMimeCoder()。有时,sendgrid无法识别getMimeCoder()的结果。

Try Base64.getEncoder() instead of Base64.getMimeEncoder(). Sometimes Sendgrid does not recognize the result from getMimeEncoder().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文