How to test a mail service without SMTP-server (Java)

To test if a mail was successfully sent using Java you would normally need an SMTP-server to check. If it happens that you don’t have a server available, you can use mailtrap. To setup mailtrap, following code is used: Properties prop = new Properties(); prop.put("mail.smtp.auth", true); prop.put("mail.smtp.starttls.enable", "true"); prop.put("mail.smtp.host", "smtp.mailtrap.io"); prop.put("mail.smtp.port", "25"); prop.put("mail.smtp.ssl.trust", "smtp.mailtrap.io"); session = Session.getInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("YYY", "XXX"); } }); Mails sent using this session token end up in webservice that you can check.

Comments

The problem is clear and understandable. In this particular case it would have been very nice to put the Solution in the Answer section below, because it allows code formatting via <code> tag. Because the answer contains lots of code, which becomes very hard to follow without proper formatting. Besides that, the answer is good and easy to follow. It would have been perfect if there was one sentence in the beginning, which describes what is Mailtrap.
Boian Velitchkov - Sun, 12/02/2018 - 20:21 :::
Nice solution, I think this works perfect for automated testing right? Did you thought about using an instand-mail account like mailinator.com for testing?
Andre Bernecker - Thu, 12/06/2018 - 15:08 :::
Thank you for your comment it really helped me.
Carole Sebah - Mon, 12/10/2018 - 14:26 :::
1 answer