简体   繁体   English

Java读取MS Outlook收件箱

[英]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. 我想使用Java(SE)在MS Outlook(2010)中阅读我的收件箱,然后将邮件/电子邮件移动到另一个文件夹。 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. 可以使用javax.mail完成,但是很大程度上取决于服务器的协议和身份验证等。

Anyways, here is a snippet (assuming imap): 无论如何,这是一个代码段(假设使用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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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