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