google-api - 如何在 Google API 中为消息设置日期

标签 google-api gmail-api

我有一个要求,比如在某些情况下更新 gmail 邮件。所以我试图通过删除现有消息并创建一条新消息来完成它。但我希望保留旧消息的日期。

假设在 7 月 1 日发送了一条消息。我正在尝试在 7 月 3 日更新消息内容。因此,在 7 月 3 日,我使用 messageId 删除了现有消息并创建了一条新消息。但我需要创建日期为 7 月 1 日的消息。

使用下面的代码创建消息并发送

private static MimeMessage createEmail(Activity activity, String userId, String fromAddress) throws MessagingException {

    Properties props = new Properties();
    Session session = Session.getInstance(props);

    MimeMessage email = new MimeMessage(session);
    InternetAddress tAddress = new InternetAddress(userId);
    InternetAddress fAddress = new InternetAddress(fromAddress);

    email.setFrom(fAddress);
    email.addRecipient(RecipientType.TO, tAddress);

    String subject = GoogleUtil.getFormattedEmailSubject(activity);
    String text = GoogleUtil.getFormattedEmailText(activity);

    email.setSubject(subject);
    email.setText(text);

    //TRYING TO SET THE DATE HERE IF AM RIGHT. 
    email.setSentDate(new Date(544543676346L));

    return email;
}

private static Message createMessageWithEmail(MimeMessage email, List<String> labelIds) throws MessagingException, IOException {

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    email.writeTo(bytes);
    String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
    Message message = new Message();
    message.setLabelIds(labelIds);
    message.setRaw(encodedEmail);

    return message;
}

private static Message sendMessage(Gmail service, String userId, MimeMessage email, List<String> labelIds)
        throws MessagingException, IOException {

    Message message = createMessageWithEmail(email, labelIds);
    message = service.users().messages().send(userId, message).execute();

    return message;
}

最佳答案

当您 send 时,您无法设置 Gmail 使用的电子邮件内部消息时间电子邮件。但是当你 insert 时你可以这样做电子邮件到收件箱。只需设置 internalDateSource“dateHeader”,告诉 Gmail 它应该根据电子邮件中的 Date header 确定内部邮件时间。

public static void insertMessage(Gmail gmail, String userId, Message message) throws IOException {
  gmail.users().messages()
     .insert(userId, message)
     .setInternalDateSource("dateHeader")  // <--- The Gmail internal message time will be based on the Date header in the email, when valid.
     .execute();
}

关于google-api - 如何在 Google API 中为消息设置日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38204983/

相关文章:

android - 我可以在我的应用程序中使用 Google Places intent 吗?

google-apps-script - 尝试使用 GmailApp.sendEmail - 缺少授权访问 token

android - 在 gmail-api 中获取发件人邮件

android - PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException : 10: , null, null) 尝试在 flutter 中使用 googleSignIn 时

android - 静态图去除版权水印

php - 如何检查文件是否已删除 google Drive api

android - 使用 G+ 登录 - 为什么范围 "plus.login"不能单独工作?

javascript - 在 'MutationObserver' : parameter 1 is not of type 'Node' 上“观察”

PHP - 带有 Google Gmail OAuth2 API 的 ListMessages

javascript - GmailApp - 为特定邮件添加标签,而不是线程