简体   繁体   English

如何使用 Java 代码阅读 MS Outlook 收件箱邮件?

[英]How to read MS Outlook inbox mail using Java code?

I need to read the subject,message from the inbox of Outlook, using java code.我需要使用 java 代码从 Outlook 的收件箱中读取主题和消息。 Is any sample code/Idea for the same, please help to get the same.是否有相同的示例代码/想法,请帮助获得相同的。

I search with StackOverflow, it gives the code in C#.我用 StackOverflow 搜索,它给出了 C# 中的代码。

Also i check with Javamail, But i didnt found anything about Outlook.我还检查了 Javamail,但我没有发现有关 Outlook 的任何信息。

If you want to you read a .pst file using Java may not be a good option.如果您想使用 Java 读取 .pst 文件可能不是一个好的选择。 To me it makes more sense to get the mail directly form server if you have the server details.对我来说,如果您有服务器详细信息,直接从服务器获取邮件更有意义。

I got this link from Google-- "reading pst file".我从谷歌得到了这个链接——“阅读 pst 文件”。

When you say "the inbox of Outlook", do you meanthe data Outlook stores on your local computer?当您说“Outlook 的收件箱”时,您是指 Outlook 存储在您本地计算机上的数据吗? Or do you mean the data in the Inbox mail folder in your remote mail server, possibly Exchange?或者您是指远程邮件服务器(可能是 Exchange)中收件箱邮件文件夹中的数据? If the latter, you can use JavaMail to do that, but you have to configure the Exchange server to allow IMAP access.如果是后者,您可以使用 JavaMail 来执行此操作,但您必须配置 Exchange 服务器以允许 IMAP 访问。

This is how I have done.这就是我所做的。

   /**
 * Connects to email server with credentials provided to read from a given
 * folder of the email application
 * 
 * @param username Email username (e.g. janedoe@email.com)
 * @param password Email password
 * @param server   Email server (e.g. smtp.email.com)
 * @param INBOX    Folder in email application to interact with
 * @throws Exception
 */
    public OutlookEmail() throws Exception {
    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imap");
    props.setProperty("mail.imap.ssl.enable", "true");
    props.setProperty("mail.imaps.partialfetch", "false");
    props.put("mail.mime.base64.ignoreerrors", "true");

    Session mailSession = Session.getInstance(props);
    mailSession.setDebug(true);
    Store store = mailSession.getStore("imap");
    store.connect("outlook.office365.com", "YOUREMAILADDRESS", "YOUR PASSWORD");


    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    System.out.println("Total Message:" + folder.getMessageCount());
    System.out.println("Unread Message:" + folder.getUnreadMessageCount());

    messages = folder.getMessages();

    for (Message mail : messages) {         



            System.out.println("*********************************");
            System.out.println("MESSAGE : \n");

            System.out.println("Subject: " + mail.getSubject());
            System.out.println("From: " + mail.getFrom()[0]);
            System.out.println("To: " + mail.getAllRecipients()[0]);
            System.out.println("Date: " + mail.getReceivedDate());
            System.out.println("Size: " + mail.getSize());
            System.out.println("Flags: " + mail.getFlags());
            System.out.println("ContentType: " + mail.getContentType());
            System.out.println("Body: \n" + getEmailBody(mail));    
            System.out.println("*******************************");          

    }
}

**Read from config and Pass credentials, uname, pwd as arguments and masked. **从配置中读取并传递凭据、uname、pwd 作为参数并进行屏蔽。

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

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