How To Read Emails Using Java Imaps? — Devstringx
Here we are going to see how email messages can be read by Java-Selenium using Java mail API.
Below I have created a 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 Username and Password then will call getFolder() by store reference variable and pass inbox as a parameter so that it will check 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.
Authenticate () method is used to get sessions like hostname, port number user name, password, etc by using 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.
isMimeType() — this method takes an 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.
Originally published at https://www.devstringx.com on Feb 14, 2022.