Read from Gmail

Updated: Jun 20, 2022

In this example you are going to see how you can read your emails(and their attachments) securely from IMAP mailboxes using Data Pipeline's EmailReader.

IMAP is an Internet standard protocol used by email clients to retrieve email messages from a mail server over a TCP/IP connection. Compared to other protocols such as POP3 IMAP does synchronization on your emails and folders and also IMAP will never delete your emails from the mail server unless you explicitly delete them your self(unlike POP3 which deletes your emails by default after you download them to your local device).

SSL is a cryptographic protocol designed to provide communications security over a computer network. This protocol is widely used in applications such as email, instant messaging and voice over IP.

We don't store your username, password and any of your emails.

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 ReadFromGmail {
    
   /*
     * Google Account->Security->2-Step Verification->Follow the steps
     *
     * Google Account->Security->App Passwords->Enter your password->Select app->
     * Choose Other(Custom name)->Enter NorthConcepts-DataPipeline->Click Generate->
     * Copy generated password
     *
     * Gmail -> Settings -> Forwarding and POP/IMAP -> Enable POP or Enable IMAP
     * 
     */

    public static void main(String... args) {
        DataReader reader = new EmailReader(MailStore.IMAP_OVER_SSL, "imap.gmail.com", "username", "password");
        DataWriter writer = new StreamWriter(System.out);
        
        Job.run(reader, writer);
    }
}

Configuration steps

  1. Go to your Google account.
  2. Navigate to Security setting and Enable 2-Step Verification.
  3. Go back to Security setting.
  4. Click App passwords and enter your password.
  5. Click Select app and choose Other(Custom) option from the drop down.
  6. Enter NorthConcepts-DataPipeline as app name and click Generate.
  7. Copy the generated password and use it to read your emails.

Code Walkthrough

  1. EmailReader is created to read emails from the specified mailbox.
  2. MailStore.IMAP_OVER_SSL indicates a secure IMAP protocol (Secure Socket Layer) is used for reading your emails.
  3. Replace username and password in the parameter with your Gmail username and the password you generated in the above step.
  4. Data are transferred from EmailReader to the console via Job.run() method. See how to compile and run data pipeline jobs.

EmailReader

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

MailStore.IMAP_OVER_SSL

Specifies that IMAP traffic travels over a secure socket to a secure port, typically TCP port 993.

Console output

Your emails will be printed on the console.

Mobile Analytics