1

GMail API を使用して、単純な html のみの下書きを作成できます。

しかし、添付ファイルをアップロードしようとすると、次のエラーで失敗します:

{
  "code" : 500,
  "errors" : [ {
    "domain" : "global",
    "message" : "Backend Error",
    "reason" : "backendError"
  } ],
  "message" : "Backend Error"
}

これが私のコードです。誰か助けてくれますか?

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

3 に答える 3

1

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.

于 2014-06-27T02:29:02.587 に答える
0

この希望を試して、その作品が最初にテキストコンテンツを配置し、その後htmlコンテンツが適切に表示されるようにします

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)
于 2014-07-25T09:57:50.117 に答える