有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中使用Gmail API回复消息?

我正在尝试使用gmail api回复一条消息,但我对设置in reply to和references头感到困惑,我不确定该设置什么值以及如何设置该值。请在下面找到我的代码:

public static Message createMessageWithEmail(MimeMessage emailContent) throws Exception {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    emailContent.addRecipient(javax.mail.Message.RecipientType.TO, emailContent.getFrom()[0]);
    emailContent.setReplyTo(emailContent.getFrom());
    emailContent.setHeader("In-Reply-To", emailContent.getMessageID());
    emailContent.setHeader("References",emailContent.getMessageID());
    emailContent.setText("hi thank you");
    emailContent.writeTo(buffer);
    byte[] bytes = buffer.toByteArray();
    String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
    Message message = new Message();
    message.setRaw(encodedEmail);
    return message;
  }




private static Message replyMessage(Gmail service, String userId, MimeMessage emailContent,Message messages) throws Exception {
    Message message = createMessageWithEmail(emailContent);
    message.setThreadId(messages.getThreadId());
    message.setId(messages.getId());
    message = service.users().messages().send(userId, message).execute();
    System.out.println("Message id: " + message.getId());
    System.out.println(message.toPrettyString());
    return message;
  }

共 (1) 个答案

  1. # 1 楼答案

    这两个头都来自RFC2822

    与您的问题相关的部分包括在第23-24页(重点和格式):

    When creating a reply to a message, the "In-Reply-To:" and "References:" fields of the resultant message are constructed as follows:

    • The "In-Reply-To:" field will contain the contents of the "Message-ID:" field of the message to which this one is a reply (the "parent message"). If there is more than one parent message, then the "In-Reply-To:" field will contain the contents of all of the parents' "Message-ID:" fields. If there is no "Message-ID:" field in any of the parent messages, then the new message will have no "In-Reply-To:" field.
    • The "References:" field will contain the contents of the parent's "References:" field (if any) followed by the contents of the parent's "Message-ID:" field (if any). If the parent message does not contain a "References:" field but does have an "In-Reply-To:" field > containing a single message identifier, then the "References:" field will contain the contents of the parent's "In-Reply-To:" field followed by the contents of the parent's "Message-ID:" field (if any). If the parent has none of the "References:", "In-Reply-To:", or "Message-ID:" fields, then the new message will have no "References:" field.