gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:48:47 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com