【发布时间】: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