Hello!
Having spent a lot of time in finding a solution to my problem, I did not find a solution. In general, the problem is as follows: There is a web application where you can send invitations to work on projects, that is, a standard form for filling in data including mail to which the letter will be sent. And here is the task to cover this functionality with auto tests. Initially I thought of doing this all on selenium, after sending an invitation to open a new browser instance, go to the postal service where the invitation will be sent and there to make manipulations on searching and opening a letter and clicking on the invite button. I realized that this is a bit tough process and I found a solution for my task to use the JavaMail library to receive emails. Ie we connect to the mail service in this example, I use Gmail and get all the letters from there.
public class PropertiesEmail { String host = "imap.gmail.com"; String user = "userEmail"; String password = "password"; int port = 993; public Properties setServerProperties(){ Properties properties = new Properties(); properties.put("mail.imap.host", host); properties.put("mail.imap.port", port); properties.put("mail.imap.starttls.enable", "true"); properties.put("mail.store.protocol", "imaps"); return properties; } } import javax.mail.*; import javax.mail.search.FlagTerm; import java.util.Properties; public class CheckUnreadEmail { public static void checkUnreadEmail(){ try{ //Create object email properties PropertiesEmail propertiesEmail = new PropertiesEmail(); //Set email server properties Properties props = propertiesEmail.setServerProperties(); Session session = Session.getDefaultInstance(props); Store store = session.getStore(); store.connect(propertiesEmail.host, propertiesEmail.user, propertiesEmail.password); //Create the folder object and open it Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); //Total unread messages System.out.println("Total messages: " + folder.getMessageCount()); System.out.println("Unread messages: " + folder.getUnreadMessageCount()); //Create variable for search unread message FlagTerm flag = new FlagTerm(new Flags(Flags.Flag.SEEN), false); //Retrieve all messages //Message [] messages = folder.getMessages(); //Retrieve unread messages from the folder INBOX Message [] unreadMessage = folder.search(flag); for (int i = 0, n = unreadMessage.length; i < n; i++){ Message message = unreadMessage[i]; System.out.println("--------------"); System.out.println("Subject: " + message.getSubject()); } //folder.setFlags(unreadMessage, new Flags(Flags.Flag.SEEN), true); //close the store and folder objects folder.close(false); store.close(); }catch(Exception e){ e.printStackTrace(); } } } There are several questions: 1. How to wait for a letter to appear in the mail? (because after sending the invitation, the letter does not arrive immediately) 2. How to parse the received letter and pull out the link to the invite from there?