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.
- Go to your Gmail account.
- Navigate to settings and click Accounts and Import.
- Go to Other Google Account settings and choose Sign-in & security.
- Enable
Allow less secure apps
and you are good to go.
Code walkthrough
- 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 thereader
. MailStore.POP3
is the method of email delivery. usingPOP3
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 useMailStore.IMAP
.- You can replace
pop3.example.com
with your email provider,user
with your email address andpassword
with your email password. - StreamWriter is used to read the records to an output stream. In this case the output will be displayed in the console.
- Data is transferred from the
reader
to thewriter
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.