GMail API:无法在草稿中添加附件 [英] GMail API : unable to add an attachment in a draft
本文介绍了GMail API:无法在草稿中添加附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够使用GMail API创建简单的仅html草稿.
I am able to create simple html-only drafts using the GMail API.
但是当我尝试上传附件时,它失败并显示以下错误:
But when I try to upload attachments it fails with the following error :
{
"code" : 500,
"errors" : [ {
"domain" : "global",
"message" : "Backend Error",
"reason" : "backendError"
} ],
"message" : "Backend Error"
}
这是我的代码,有人可以帮助我吗?
Here is my code, can anyone help me ?
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress("to@to.in");
InternetAddress fAddress = new InternetAddress("from@from.in");
email.setFrom(fAddress);
email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress);
email.setSubject("subject");
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent("thebody", "text/plain");
mimeBodyPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\"");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
mimeBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("C:\\Users\\David\\Downloads\\test.pdf");
mimeBodyPart.setDataHandler(new DataHandler(source));
mimeBodyPart.setFileName("thepdf.pdf");
String contentType = "application/pdf";
mimeBodyPart.setHeader("Content-Type", contentType + "; name=\"" + "test.pdf" + "\"");
mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64");
multipart.addBodyPart(mimeBodyPart);
email.setContent(multipart);
email.writeTo(System.out);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
email.writeTo(bytes);
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
Draft draft = new Draft();
draft.setMessage(message);
draft = new Gmail(new NetHttpTransport(), new JacksonFactory(), getCredential()).users().drafts().create("me", draft).execute();
推荐答案
第二天自行解决问题!我不知道GMail API团队是否修复了某些问题,或者这是否是临时错误.
Problem solved by itself the next day ! I don't know if the GMail API team fixed something or if it was a temporary bug.
这篇关于GMail API:无法在草稿中添加附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文