Java and JUnit
<dependency> <groupId>com.mailcatchr</groupId> <artifactId>mailcatchr-sdk</artifactId> <version>1.0.1</version> <scope>test</scope></dependency>Read the key and mailbox id from the environment, never from source.
A shared client
Section titled “A shared client”import com.mailcatchr.sdk.MailcatchrClient;import com.mailcatchr.sdk.Models.AwaitMessageOptions;
final class Mail { static final MailcatchrClient CLIENT = MailcatchrClient.builder(System.getenv("MAILCATCHR_API_KEY")).build(); static final String MAILBOX_ID = System.getenv("MAILCATCHR_MAILBOX_ID");
/** A fresh address per test so parallel tests never see each other's mail. */ static String uniqueAddress(String label) { return label + "-" + System.currentTimeMillis() + "@<mailbox-id>.mailcatchr.com"; }}The test
Section titled “The test”import static org.junit.jupiter.api.Assertions.*;import java.util.regex.Pattern;import org.junit.jupiter.api.Test;
class PasswordResetTest { @Test void resetEmailContainsAWorkingLink() throws Exception { var address = Mail.uniqueAddress("reset"); app.requestPasswordReset(address);
var summary = Mail.CLIENT.awaitMessage(Mail.MAILBOX_ID, AwaitMessageOptions.builder() .to(address).subject("Reset your password").timeoutSeconds(30).build()) .orElseThrow(() -> new AssertionError("the reset email should arrive within 30 s"));
var message = Mail.CLIENT.getMessage(Mail.MAILBOX_ID, summary.id()); var matcher = Pattern.compile("https://\\S+/reset\\?token=\\S+").matcher(message.textBody()); assertTrue(matcher.find(), "reset link in the email body");
assertEquals(200, app.get(matcher.group()).statusCode()); }}awaitMessage returns an empty Optional on timeout rather than throwing, so the failure message is yours to write.
Two-factor codes
Section titled “Two-factor codes”var code = Mail.CLIENT.totpCode(System.getenv("MAILCATCHR_TOTP_ID")).code();driver.findElement(By.name("otp")).sendKeys(code);Kotlin
Section titled “Kotlin”The client is plain Java with records and a builder, so it reads naturally from Kotlin:
val summary = Mail.CLIENT.awaitMessage(mailboxId, AwaitMessageOptions.builder().to(address).build()) .orElseThrow { AssertionError("no email") }