- Do you have use cases for validating mails or reading data from them?
- Are you trying to automate Google Mail through Frontend?
- Are you blocked by google while login through frontend?
- Are you validating your mails on Mailinator or yopmail not on google?
Then you are on correct location. I will be explaining how easy is to validate mails through java. You can Follow below steps for verifying emails using Java:
Create App password
As a very first step create an app password for authentication purpose on Gmail. With this you need not to worry about two factor authentication.
- Move to Manage your google Account Section
- Select Security
- Select App password
- Select Mail as the app and Select Any device
- Click on Generate button
- Save the token for future purpose
Create Folder and filter
Now to have distinction between mails you want to validate and other random emails in your inbox.
- Click on search bar
- And Click on icon on right side
- And Enter values according to your use case
- And Click on click filter button
- And Click on Apply the label
- Then Click on Create Filter button as shown below
Code
Now we will add code where we will be using above two things
- Include dependency in pom.xml
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.4</version>
</dependency>
- Add code to read Mail
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imap.host", "imap.gmail.com");
properties.put("mail.imap.port", "993");
properties.put("mail.imap.starttls.enable", "true");
Session session = Session.getDefaultInstance(properties);
Store store=session.getStore("imaps");
store.connect("imap.gmail.com",USER_NAME, PASSWORD);
//Put folder you have created instead of Mail Folder
Folder folder = store.getFolder("MailFolder");
folder.open(Folder.READ_ONLY);
int totalMessagesInFolder=folder.getMessageCount();
System.out.println(totalMessagesInFolder);
Message[] message = folder.getMessages();
//Prints Subject
System.out.println(message[0].getSubject());
//Get Email Body
String value = getTextFromMessage(message[0]);
System.out.println(value);
- Now next step is to parse the Email Body since this contains multiple parts like HTML, ICS file, Attachment so it is bit complicated.
public static String getTextFromMessage(Message message) throws MessagingException, IOException {
if (message.isMimeType("text/plain")) {
return message.getContent().toString();
}
if (message.isMimeType("multipart/*")) {
MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
return parseMultiPart(mimeMultipart);
}
return "";
}
public static String parseMultiPart(MimeMultipart multipart) throws IOException, MessagingException {
String result = "";
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart bodyPart = multipart.getBodyPart(j);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) {
System.out.println("Mail have some attachment");
DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
}
else {
if(bodyPart.isMimeType("text/plain")) {
return bodyPart.getContent().toString();
} else {
result+=parseBodyPart(bodyPart);
}
}
}
return result;
}
public static String parseBodyPart(BodyPart bodyPart) throws MessagingException, IOException {
if (bodyPart.isMimeType("text/html")) {
return "\n"+bodyPart.getContent().toString();
}
if (bodyPart.getContent() instanceof MimeMultipart){
return parseMultiPart((MimeMultipart)bodyPart.getContent());
}
return "";
}
With this you can automate your email workflows like reading OTP, verifying mail after particular actions in your automation. You can also use below methods for further validations
message.getAllRecipients() //For verifying Recipient email address
message.getFrom() //For verifying Sender's address
message.getReceivedDate() //For verifying Received date & time
Top comments (0)