GMail API : unable to add an attachment in a draft

旧时模样 提交于 2020-01-11 07:45:08

问题


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

回答1:


I think that code is reasonable. Can you provide the output from the email.writeTo(System.out) to confirm? Seems like a bug in the Gmail API.

Is this repeatable for any specific user? (Does it always fail or will it eventually work?)

Does it happen for all users or only certain users (e.g. does it work for gmail.com users and not for Google Apps users)?




回答2:


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.




回答3:


Try this hope so its works first put text content and after that html content its display properly

MimeBodyPart textPart = new MimeBodyPart();
        textPart.setText("This is actual message", "utf-8");

        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("<h1>This is a test</h1>"
                + "<img src=\"http://www.rgagnon.com/images/jht.gif\">", "text/html; charset=utf-8");
        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(textPart);<--first 
        multipart.addBodyPart(htmlPart);<--seocnd 
        message.setContent(multipart)


来源:https://stackoverflow.com/questions/24429324/gmail-api-unable-to-add-an-attachment-in-a-draft

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!