How To Read Emails Using Java Imaps? – Devstringx

Back to Blog

How To Read Emails Using Java Imaps? – Devstringx

Here we are going to see how email content can be read by Java-Selenium using Java mail API.

Method to Get the Body Content of the Mail

It requires 3 parameters username, password, and the subject of the mail. When this method will be called then first it will authenticate the Username and Password and then will call getFolder() by storing the reference variable and passing the inbox as a parameter so that it will check the inbox folder only.

All the messages are stored in an array so will iterate one by one by using For Loop. When The subject will equal to the given in parameter then it will return the body content of that message.

Java Imap

Read Also: – Process Java Script Executor in Selenium Test Automation

The authentication () method is used to get sessions like hostname, port number user name, password, etc by using Java mail API.

Java mail API

getTextFromMimeMultipart () method is used to get the text from Multipurpose Internet Mail Extensions. First, it will get the count of the MIME multipart message then it will iterate one by one to get the body part of that specific MIME part.

Read Also:- Execution Time of Multiple Methods In Selenium Java

isMimeType() – this method takes arguments like “text/plain” for plain text, “text/HTML” for normal webpages.. etc. The return type is boolean( true or false). If the return type is true then it will get the content of that message and if the return type is false then it will be ignored.

Reading E-Mail


  • In Java, how do I read unread emails in Gmail?

Make the following code modifications: ONLY); should be replaced with WRITE);

After reading the message, position the flag as follows: set flag(‘message’, ‘Flags.Flag.SEEN, true);

  • What is JavaMail API?

A platform- and protocol-neutral framework for creating mail and messaging applications are offered by the JavaMail API. The JavaMail API is a component of the Java EE platform as well as a package that may be used with the Java SE platform.

  • How can I read a mailbox in java?

JavaMail: How to Read a Message Import the necessary packages.

  • Search for the JavaMail session.
  • If necessary, override the JavaMail session properties.
  • Obtain a Store object from the Session, then use the connect method of the Store object to establish a connection with the mail server.
  • the INBOX folder, please.
  • What is IMAP in Java?

The term Internet Message Access Protocol (IMAP) is an acronym. It is an Internet protocol at the Application Layer that enables email clients to access email on distant mail servers. A typical IMAP server listens on the well-known port 143.


Our software test engineers have written some other detailed blogs on Java  you might find of interest

Share this post

Back to Blog