【问题标题】:How to set the date to the message in Google API如何在 Google API 中为消息设置日期
【发布时间】:2016-11-07 09:10:21
【问题描述】:

我有一个要求,比如在某些情况下更新 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;
}

【问题讨论】:

    标签: google-api gmail-api


    【解决方案1】:

    当您send 电子邮件时,您无法设置 Gmail 使用的电子邮件内部消息时间。但是,当您向收件箱发送insert 电子邮件时,您可以这样做。只需将internalDateSource 设置为"dateHeader",告诉Gmail 它应该将内部消息时间基于电子邮件中的日期标题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 2018-09-20
      相关资源
      最近更新 更多