Read Emails

Updated: Jun 4, 2023

In this example you are going to see how simple it is to read emails using DataPipline.

EmailReader will be used to read the emails (and their attachments) from mailboxes.

Java code listing

package com.northconcepts.datapipeline.examples.cookbook;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.email.EmailReader;
import com.northconcepts.datapipeline.email.MailStore;
import com.northconcepts.datapipeline.job.Job;

public class ReadEmails {

    public static void main(String[] args) {
        DataReader reader = new EmailReader(MailStore.POP3, "pop3.example.com", "user", "password");
        DataWriter writer = new StreamWriter(System.out);
        
        Job.run(reader, writer);
    }

}

Configuration steps

To read your emails using EmailReader you have to enable Allow less secure apps in your Gmail account.

  1. Go to your Gmail account.
  2. Navigate to settings and click Accounts and Import.
  3. Go to Other Google Account settings and choose Sign-in & security.
  4. Enable Allow less secure apps and you are good to go.

Code walkthrough

  1. EmailReader accepts four argument, the mailstore (POP3),host (pop3.example.com),user (user) and password (password). If the details are correct the emails will be read and stored in the reader.
  2. MailStore.POP3 is the method of email delivery. using POP3 allows you to store the emails on your local machine rather than it being kept in the mail server which will be the scase if you decide to use MailStore.IMAP.
  3. You can replace pop3.example.com with your email provider,user with your email address and password with your email password.
  4. StreamWriter is used to read the records to an output stream. In this case the output will be displayed in the console.
  5. Data is transferred from the reader to the writer via Job.run() method.

EmailReader

Reads emails (and their attachments) from IMAP mailboxes. It extends IntegrationReader and It's constructor takes a MailStore, an email provider, username and password.

Console Output

The emails will be displayed in the console.

Mobile Analytics