Extract emails from remote server via POP IMAP or Exchange Web Services protocols

GroupDocs.Parser provides the functionality to extract emails from remote servers. The following email protocols are supported:

  • Post Office Protocol (POP)
  • Internet Message Access Protocol (IMAP)
  • Exchange Web Services (EWS)

To create an instance of Parser class to extract emails from a remote server the following constructor is used:

Parser(EmailConnection connection);
Parser(EmailConnection connection, ParserSettings parserSettings)

The second constructor allows to use ParserSettings object to control the process; for example, by adding logging functionality.

EmailConnection is a base class. The following connection classes are used:

ProtocolClass
Exchange Web ServicesEmailEwsConnection
IMAPEmailImapConnection
POPEmailPopConnection

Here are the steps to extract emails from the remote server:

  • Prepare connection string (see table below);
  • Instantiate Parser object with connection string;
  • Call isContainer property to check if container extraction is supported;
  • Call getContainer method and obtain collection of ContainerItem objects;
  • Iterate through the collection and get Parser object for each item.

The following example shows how to extract emails from Exchange Server:

// Create the connection object for Exchange Web Services protocol
EmailConnection connection = new EmailEwsConnection(
        "https://outlook.office365.com/ews/exchange.asmx",
        "email@server",
        "password");
// Create an instance of Parser class to extract emails from the remote server
try (Parser parser = new Parser(connection)) {
    // Check if container extraction is supported
    if (!parser.getFeatures().isContainer()) {
        System.out.println("Container extraction isn't supported.");
        return;
    }
    // Extract email messages from the server
    Iterable<ContainerItem> emails = parser.getContainer();
    // Iterate over attachments
    for (ContainerItem item : emails) {
        // Create an instance of Parser class for email message
        try (Parser emailParser = item.openParser()) {
            // Extract the email text
            try (TextReader reader = emailParser.getText()) {
                // Print the email text
                System.out.println(reader == null ? "Text extraction isn't supported." : reader.readToEnd());
            }
        }
    }
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document parser App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.