Skip to main content

Receive Email

EmailMessage[] receiveEmail(String address)

This custom command will return an array of email messages that were sent to the specified email address during test execution. To generate the email address, you should use the genEmail function.

The command itself will not wait for any messages to arrive, so it's recommended to use it within a loop that will wait for the email to be visible within the EmailMessage array.

The email message object will have the following properties:

  • from - The email address of the sender.
  • subject - The subject of the email.
  • headers - The headers of the email.
  • text/plain - The plain text content of the email.
  • text/html - The HTML content of the email.

Emails are stored within EmailMessage objects, which have the following methods to get the email properties:

  • String getFrom() - Returns the email address of the sender.
  • String getSubject() - Returns the subject of the email.
  • Map<String, String> getHeaders() - Returns the headers of the email.
  • String getTextPlain() - Returns the plain text content of the email.
  • String getTextHtml() - Returns the HTML content of the email.
public void test() {
EmailMessage[] emails = receiveEmail(genEmail("test"));

System.out.println("Amount of received emails: " + emails.length);

for (EmailMessage email : emails) {
System.out.println("From: " + email.getFrom());
System.out.println("Subject: " + email.getSubject());
System.out.println("Plain text content: " + email.getTextPlain());
}
}