3

在这里,我按升序收到所有电子邮件,表示根据最后日期检索的邮件。我需要反过来。

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

1 回答 1

1

您可以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);
于 2013-10-24T11:39:33.403 回答