gpt4 book ai didi

java - 使用 pop3 按日期降序获取邮件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:28 24 4
gpt4 key购买 nike

这里我得到了所有邮件的升序排列,意思是邮件是根据最后日期检索的。我需要反过来。

public void downloadEmailAttachments(String host, int port,String userName, String password)
{
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", port);
properties.put("mail.pop3.user",userName);
properties.put("mail.password",password);
properties.setProperty("mail.pop3.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.socketFactory.port",
String.valueOf(port));

Session session = Session.getDefaultInstance(properties);

try {
Store store = session.getStore("pop3");
store.connect(host,userName, password);

Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
Message[] arrayMessages = folderInbox.getMessages();
for (int i = 0; i < arrayMessages.length; i++) //max=puruna date=after,min=ajika date...
{
if (arrayMessages[i].getSentDate().after(maxDate) && arrayMessages[i].getSentDate().before(minDate))
{
Message message = arrayMessages[i];
Address[] fromAddress = message.getFrom();
String from = fromAddress[0].toString();
Address[]toAdress=message.getAllRecipients();
String to=toAdress[0].toString();
String subject = message.getSubject();
String sentDate = message.getSentDate().toString();
String contentType = message.getContentType().toString();
String messageContent = "";
String attachFiles = "";

if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDirectory + File.separator + fileName);

} else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}

if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain")
|| contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}

}

System.out.println("Message #" + (i + 1) + ":");
System.out.println("\t From: " + from);
System.out.println("\t to: " + to);
System.out.println("\t Subject: " + subject);
System.out.println("\t Sent Date: " + sentDate);
System.out.println("\t Message: " + messageContent);
System.out.println("\t Attachments: " + attachFiles);
}

new EmailAttachmentReceiver().setSaveDirectory(saveDirectory);
}
folderInbox.close(false);
store.close();
}

最佳答案

您可以使用Comparator 进行排序

Message[] arrayMessages = folderInbox.getMessages();

Comparator<Message> messageComparator = new Comparator<Message>( {
public int compare(Message m1, Message m2) {
if (m1.getSentDate() == null || m2.getSentDate() == null)
return 0;
return m2.getSentDate().compareTo(m1.getSentDate());
}
});

Arrays.sort(arrayMessages, messageComparator);

关于java - 使用 pop3 按日期降序获取邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19563491/

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