简体   繁体   中英

java read MS Outlook inbox

I would like to use java (SE) to read my Inbox in MS Outlook (2010) and then move message/email to another folder. I have tried to search on web, but found only licensed solutions or posts old couple of years. Does anyone have solution for this step? Thank you very much for your help!

Can be done using javax.mail, but a lot depends on the protocol of the server and authentication etc.

Anyways, here is a snippet (assuming imap):

Set your properties:

Properties props = new Properties();        
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.user", <user>);
props.setProperty("mail.imap.host", <host>);
props.setProperty("mail.imap.port", <port 143>);
...

Get a session and connect

Session mailSession = Session.getInstance(props);   
Store mailStore = mailSession.getStore("imap");
mailStore.connect(<host>, <user>, <passwd>);                    
Folder dFolder = mailStore.getDefaultFolder();
Folder inbox = dFolder.getFolder(<connectFolder=INBOX?>);
inbox.open(Folder.READ_WRITE);

 // Open destination folder, create if reqd
Folder destfolder = mailStore.getFolder(<destination folder>);
if (!destfolder.exists())
   destfolder.create(Folder.HOLDS_MESSAGES);

Message []inMessages = inbox.getMessages();
if (inMessages .length != 0) {
    inbox.copyMessages(inMessages , destfolder);

    for (int i=0; i< inMessages.length; i++) { 
      // Custom Processor which readsMessages and performs some action.
      // getProcessor().readMessage(inMessages[i]);
       inMessages[i].setFlag(Flags.Flag.DELETED, true);
    }
}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM