DEV Community

Cover image for Reading email data with Node.Js

Reading email data with Node.Js

Olumide Akinremi on July 24, 2021

Have you ever thought of reading the data of your emails and making decisions like marking it as read or do whatever you choose? Perhaps you are wo...
Collapse
 
qaproengineer profile image
QAProEngineer

As today, the less app secure has been deprecated and then the script will literally fail. However there is an alternative way to set less secure app by creating app password in google mailbox using 2FA. worked for me !!

Collapse
 
vvvutov profile image
VVVutov

Not working. I am also getting the self signed sertificate error. I think it has something to do with Google not allowing "less secure apps" anymore.

Collapse
 
kotran88 profile image
kotran88

is it still working?
I got error

Error: self signed certificate
so I tried with Htts

then have another error
Error: Invalid credentials (Failure)
has something to do with
OAuth2?

Collapse
 
qaproengineer profile image
QAProEngineer

Google has deprecated "less secure app" for the security reason, there is an alternative way to login with less secure app by creating app password in your 2FA

Collapse
 
surya63572659 profile image
Surya

Throwing error if there are no unseen mails, and by that my app is crashing,how to fix or handle that error,pls let me know

Collapse
 
akinmyde profile image
Olumide Akinremi

Hey Surya, I saw your email and I believed my recommended solution works

Collapse
 
arbaz2002 profile image
Arbaz Khan

i need the solution too for this sir

Collapse
 
arbaz2002 profile image
Arbaz Khan

I am also facing the same problem how to solve it ???

Collapse
 
akinmyde profile image
Olumide Akinremi

Hello Arbaz,

You can use try/catch or .catch to handle errors. See the attached some code snippets that can help you handle errors.

Image description

Image description

Thread Thread
 
arbaz2002 profile image
Arbaz Khan

thank you sir how we will fetch the attachments from the email sir can you share this to me plz?????

Thread Thread
 
akinmyde profile image
Olumide Akinremi

You can get the attachment from the "parsed" object.

example is:

const {from, subject, textAsHtml, text, attachments} = parsed; 
Enter fullscreen mode Exit fullscreen mode

Check the mailparser documentation for more information

Thread Thread
 
arbaz2002 profile image
Arbaz Khan

thank you so much sir

Thread Thread
 
akinmyde profile image
Olumide Akinremi

You are welcome sir

Collapse
 
selvamramasamy profile image
selvamramasamy

I am following the same code. Login is happening and connection also established but after some request connection is not establishing and it says authentication error even username and password is correct.

I know after some request imap is blocked the connection temporary. How to fix this? is there any way to open connection always without login each time?

Collapse
 
akinmyde profile image
Olumide Akinremi

I believe it is possible. You can explore several options in the "connection instance methods". Check the imap documentation for more details (npmjs.com/package/imap).

You should look for specific options like: "Keep Alive". You can also explore auto-reconnecting in cases where your connection fails due to error.

I hope this helps.

Collapse
 
vikram125609 profile image
Vikram Singh Dewda

giving an error of self signed certificate