获取错误“附件内容必须为基本64编码”。使用sendgrid将pdf作为附件发送ZIP时
我们正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
base64.getencoder()
而不是base64.getMimeCoder()
。有时,sendgrid无法识别getMimeCoder()的结果。Try
Base64.getEncoder()
instead ofBase64.getMimeEncoder()
. Sometimes Sendgrid does not recognize the result from getMimeEncoder().