top of page
Search
kingkatharina1984

GreenMail: A Powerful and Flexible Tool for Testing Email-Based Systems




GreenMail Download: How to Test Email-Based Applications with a Virtual Mail Server




If you are a developer who works with email-based applications, services or systems, you know how important it is to test them thoroughly before deploying them to production. However, testing email functionality can be challenging, especially if you don't have access to a live mail server or you don't want to send real emails to real recipients. That's where GreenMail comes in handy.




greenmail download



What is GreenMail and why do you need it?




GreenMail is an intuitive and easy-to-use test suite of email servers for testing purposes. It allows you to send, receive, and verify emails by embedding it in a unit test or running it as a standalone container. GreenMail acts as a virtual (mocking/sandbox) mail server and supports common mail protocols such as SMTP, POP3, IMAP with SSL socket support. It also provides configurable user accounts and credentials, so you can simulate different scenarios and cases.


GreenMail features and benefits




Some of the main features and benefits of using GreenMail are:


  • It is 100% Java-based, so it can run on any platform that supports Java.



  • It is open source and free to use under the Apache License V2.0.



  • It is easy to integrate with popular testing frameworks such as JUnit, TestNG, Spring, etc.



  • It is lightweight and fast, so it does not consume much resources or affect the performance of your application.



  • It is reliable and secure, so you don't have to worry about spamming, phishing, or data leakage.



GreenMail supported protocols and standards




GreenMail supports the following protocols and standards:


  • SMTP (Simple Mail Transfer Protocol) for sending emails.



  • POP3 (Post Office Protocol) for retrieving emails from a server.



  • IMAP (Internet Message Access Protocol) for accessing and managing emails on a server.



  • SSL (Secure Sockets Layer) for encrypting the communication between the client and the server.



  • Jakarta Mail (formerly JavaMail) for providing a platform-independent and protocol-independent framework for sending and receiving emails.



How to download and install GreenMail




Download options and requirements




You can download GreenMail from various sources, depending on your preference and needs. The most common options are:


The official website: [1](


The GitHub repository: [2](


  • The Maven Central repository: com.icegreen:greenmail:



The latest version of GreenMail at the time of writing this article is 2.1.0-alpha-1, which is based on Jakarta Mail 2.1 / Jakarta EE 10. However, you can also use older versions that are compatible with different versions of JavaMail or Jakarta Mail.


To run GreenMail, you need to have Java 11 or higher installed on your system. You also need to have Maven 3.8 or higher if you want to build GreenMail from source or use it as a Maven dependency.


Installation steps and configuration




The installation steps vary depending on how you want to use GreenMail. Here are some common scenarios:



ScenarioStepsScenario


greenmail download sourceforge


greenmail download github


greenmail download release


greenmail download maven


greenmail download jar


greenmail download docker


greenmail download site


greenmail download example


greenmail download javadoc


greenmail download faq


greenmail test suite download


greenmail email server download


greenmail smtp server download


greenmail imap server download


greenmail pop3 server download


greenmail ssl support download


greenmail user accounts download


greenmail credentials download


greenmail java mail download


greenmail jakarta mail download


greenmail spring integration download


greenmail junit integration download


greenmail webapp integration download


greenmail client integration download


greenmail standalone integration download


greenmail mock mail server download


greenmail sandbox mail server download


greenmail virtual mail server download


greenmail email testing tool download


greenmail email verification tool download


greenmail email development tool download


greenmail email simulation tool download


greenmail email debugging tool download


greenmail email mocking tool download


how to download and install greenmail


how to use greenmail in java project


how to configure greenmail in spring boot application


how to run greenmail as a docker container


how to build greenmail from source code


how to generate maven site for greenmail project


how to deploy a release of greenmail project


how to contribute to greenmail project on github


how to report issues and bugs for greenmail project


how to access the web interface of greenmail server


how to send and receive emails using greenmail server


how to verify emails using greenmail api


how to create and delete users using greenmail api


how to enable and disable ssl using greenmail api


how to set custom ports using greenmail api


Steps


  • Embedding GreenMail in a unit testAdd GreenMail as a Maven dependency in your pom.xml file.

  • Create a GreenMail instance with the desired configuration (e.g. port, user, password, etc.) in your test class.

  • Start and stop the GreenMail server in the @Before and @After methods of your test class.

  • Use the GreenMail API to send, receive, and verify emails in your test methods.



  • Running GreenMail as a standalone containerDownload the greenmail-standalone-.jar file from the official website or GitHub repository.

  • Run the jar file with the java -jar command and specify the desired options (e.g. port, user, password, etc.).

  • Use an email client (e.g. Thunderbird, Outlook, etc.) or a web browser to connect to the GreenMail server and send, receive, and verify emails.



  • Using GreenMail as a Docker imagePull the greenmail/standalone image from the Docker Hub repository.

  • Run the image with the docker run command and specify the desired options (e.g. port, user, password, etc.).

  • Use an email client (e.g. Thunderbird, Outlook, etc.) or a web browser to connect to the GreenMail server and send, receive, and verify emails.



How to use GreenMail for testing email-based applications




Once you have installed and configured GreenMail, you can use it for testing various aspects of your email-based applications. Here are some examples of how to use GreenMail for different purposes:


Embedding GreenMail in a unit test




If you want to test your email functionality in isolation from other components or dependencies, you can embed GreenMail in a unit test and use it as a mocking or stubbing tool. For example, you can use GreenMail to test how your application sends an email confirmation after a user registration or how it handles an incoming email request. Here is a sample code snippet that shows how to embed GreenMail in a JUnit test:



import com.icegreen.greenmail.junit5.GreenMailExtension; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetupTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import javax.mail.internet.MimeMessage; import static org.junit.jupiter.api.Assertions.assertEquals; public class EmailTest // Create a GreenMail extension with SMTP and IMAP support @RegisterExtension static GreenMailExtension greenMail = new GreenMailExtension(ServerSetupTest.SMTP_IMAP); @Test public void testSendAndReceiveEmail() throws Exception // Define the sender and receiver addresses String sender = "test@example.com"; String receiver = "user@example.com"; // Send an email using GreenMail's utility method GreenMailUtil.sendTextEmailTest(receiver, sender, "Hello", "This is a test email"); // Wait for the email to arrive greenMail.waitForIncomingEmail(1); // Retrieve the email using IMAP MimeMessage[] messages = greenMail.getReceivedMessages(); // Verify the email content assertEquals(1, messages.length); assertEquals("Hello", messages[0].getSubject()); assertEquals("This is a test email", GreenMailUtil.getBody(messages[0]));


Running GreenMail as a standalone container




If you want to test your email functionality in integration with other components or dependencies, you can run GreenMail as a standalone container and use it as a sandbox or simulator tool. For example, you can use GreenMail to test how your application interacts with an external mail server or how it handles different mail protocols. Here is a sample command that shows how to run GreenMail as a standalone container:



java -jar greenmail-standalone-2.1.0-alpha-1.jar -Dgreenmail.setup.test.all -Dgreenmail.users=test1:pwd1@test.com,test2:pwd2@test.com -Dgreenmail.verbose


This command will start a GreenMail server with SMTP, POP3, IMAP and SSL support on the default ports. It will also create two user accounts: test1@test.com with password pwd1 and test2@test.com with password pwd2. It will also enable verbose logging for debugging purposes.


You can You can then use an email client (e.g. Thunderbird, Outlook, etc.) or a web browser to connect to the GreenMail server and send, receive, and verify emails. For example, you can use the following URL to access the GreenMail web interface:



This URL will show you a list of all the emails that have been sent or received by the GreenMail server. You can also view the details of each email, such as the headers, the body, and the attachments. You can also delete or download the emails as you wish. Using GreenMail as a Docker image




If you want to test your email functionality in a containerized environment, you can use GreenMail as a Docker image and run it as a service. For example, you can use GreenMail to test how your application works in a cloud or a microservice architecture. Here is a sample command that shows how to run GreenMail as a Docker image:



docker run -d -p 3025:3025 -p 3110:3110 -p 3143:3143 -p 3465:3465 -p 3993:3993 -p 3995:3995 -e GREENMAIL_OPTS='-Dgreenmail.setup.test.all -Dgreenmail.users=test1:pwd1@test.com,test2:pwd2@test.com' greenmail/standalone


This command will pull the greenmail/standalone image from the Docker Hub repository and run it as a detached container. It will also expose the ports for SMTP, POP3, IMAP and SSL protocols and set the environment variable GREENMAIL_OPTS to configure the server with two user accounts: test1@test.com with password pwd1 and test2@test.com with password pwd2.


You can then use an email client (e.g. Thunderbird, Outlook, etc.) or a web browser to connect to the GreenMail server and send, receive, and verify emails. You can also use the docker logs command to view the output of the GreenMail server.


How to get help and support for GreenMail




If you encounter any problems or have any questions while using GreenMail, you can get help and support from various sources. Here are some of them:


Documentation and examples




The official website of GreenMail provides a comprehensive documentation that covers all the aspects of using GreenMail, such as installation, configuration, usage, API reference, etc. You can access the documentation here: [3]( The website also provides some examples that demonstrate how to use GreenMail with different testing frameworks and scenarios. You can access the examples here: [4](


FAQ and issues




The GitHub repository of GreenMail provides a FAQ section that answers some of the frequently asked questions about GreenMail, such as compatibility, limitations, troubleshooting, etc. You can access the FAQ here: [5]( The GitHub repository also provides an issue tracker that allows you to report bugs, request features, or ask questions about GreenMail. You can access the issue tracker here: [6](


Contribution and feedback




If you want to contribute to the development of GreenMail or provide feedback on its features or performance, you are welcome to do so. You can fork the GitHub repository of GreenMail and submit pull requests with your changes or improvements. You can also join the mailing list or the chat room of GreenMail and share your ideas or opinions with other users and developers. You can access these channels here: [7]( [8]( [9](


Conclusion




In this article, we have learned what is GreenMail and why do we need it for testing email-based applications. We have also learned how to download and install GreenMail in different ways and how to use it for different purposes. We have also learned how to get help and support for GreenMail from various sources.


GreenMail is a powerful and versatile tool that can help us test our email functionality with ease and confidence. It is easy to use, reliable, secure, and compatible with various platforms and frameworks. It is also open source and free to use under the Apache License V2.0.


If you are looking for a simple and effective way to test your email functionality with ease and confidence, you should definitely give GreenMail a try. You can download it from the official website or GitHub repository and start testing your email-based applications in minutes. You can also get help and support from the documentation, FAQ, issue tracker, mailing list, or chat room of GreenMail. You can also contribute to the development of GreenMail or provide feedback on its features or performance.


We hope you have enjoyed this article and learned something new and useful. If you have any questions or comments, please feel free to leave them below. Thank you for reading and happy testing!


FAQ




Here are some of the frequently asked questions about GreenMail:


  • What is the difference between GreenMail and other email testing tools?



GreenMail is different from other email testing tools in several ways. For example, GreenMail is:


  • 100% Java-based, so it can run on any platform that supports Java.



  • Open source and free to use under the Apache License V2.0.



  • Easy to integrate with popular testing frameworks such as JUnit, TestNG, Spring, etc.



  • Lightweight and fast, so it does not consume much resources or affect the performance of your application.



  • Reliable and secure, so you don't have to worry about spamming, phishing, or data leakage.



Other email testing tools may have different features, advantages, or disadvantages depending on your needs and preferences.


  • How can I change the default ports of GreenMail?



You can change the default ports of GreenMail by using the -Dgreenmail.setup.test option when running GreenMail as a standalone container or a Docker image. For example, you can use the following command to change the SMTP port to 2525 and the IMAP port to 1433:



java -jar greenmail-standalone-2.1.0-alpha-1.jar -Dgreenmail.setup.test.smtp:2525 -Dgreenmail.setup.test.imap:1433


You can also use the ServerSetup class when embedding GreenMail in a unit test. For example, you can use the following code snippet to change the SMTP port to 2525 and the IMAP port to 1433:



import com.icegreen.greenmail.junit5.GreenMailExtension; import com.icegreen.greenmail.util.ServerSetup; public class EmailTest // Create a custom server setup with SMTP and IMAP ports private static final ServerSetup SMTP = new ServerSetup(2525, null, ServerSetup.PROTOCOL_SMTP); private static final ServerSetup IMAP = new ServerSetup(1433, null, ServerSetup.PROTOCOL_IMAP); // Create a GreenMail extension with the custom server setup @RegisterExtension static GreenMailExtension greenMail = new GreenMailExtension(SMTP, IMAP); // The rest of the test class ...


  • How can I enable SSL support for GreenMail?



You can enable SSL support for GreenMail by using the -Dgreenmail.setup.test option when running GreenMail as a standalone container or a Docker image. For example, you can use the following command to enable SSL support for all protocols:



java -jar greenmail-standalone-2.1.0-alpha-1.jar -Dgreenmail.setup.test.all


You can also use the ServerSetupTest class when embedding GreenMail in a unit test. For example, you can use the following code snippet to enable SSL support for all protocols:



import com.icegreen.greenmail.junit5.GreenMailExtension; import com.icegreen.greenmail.util.ServerSetupTest; public class EmailTest // Create a GreenMail extension with SSL support for all protocols @RegisterExtension static GreenMailExtension greenMail = new GreenMailExtension(ServerSetupTest.ALL); // The rest of the test class ...


  • How can I create multiple user accounts for GreenMail?



You can create multiple user accounts for GreenMail by using the -Dgreenmail.users option when running GreenMail as a standalone container or a Docker image. For example, you can use the following command to create three user accounts: test1@test.com with password pwd1, test2@test.com with password pwd2, and test3@test.com with password pwd3:



java -jar greenmail-standalone-2.1.0-alpha-1.jar -Dgreenmail.users=test1:pwd1@test.com,test2:pwd2@test.com,test3:pwd 3@test.com


You can also use the GreenMailUserUtil class when embedding GreenMail in a unit test. For example, you can use the following code snippet to create three user accounts: test1@test.com with password pwd1, test2@test.com with password pwd2, and test3@test.com with password pwd3:



import com.icegreen.greenmail.junit5.GreenMailExtension; import com.icegreen.greenmail.user.GreenMailUser; import com.icegreen.greenmail.user.GreenMailUserUtil; public class EmailTest // Create a GreenMail extension with SMTP and IMAP support @RegisterExtension static GreenMailExtension greenMail = new GreenMailExtension(ServerSetupTest.SMTP_IMAP); // Create three user accounts in the @BeforeAll method of the test class @BeforeAll public static void setUp() GreenMailUser user1 = GreenMailUserUtil.createUser(greenMail.getImap(), "test1@test.com", "pwd1"); GreenMailUser user2 = GreenMailUserUtil.createUser(greenMail.getImap(), "test2@test.com", "pwd2"); GreenMailUser user3 = GreenMailUserUtil.createUser(greenMail.getImap(), "test3@test.com", "pwd3"); // The rest of the test class ...


  • How can I verify emails with GreenMail?



You can verify emails with GreenMail by using the GreenMail API or the web interface. For example, you can use the following methods to verify emails:


  • getReceivedMessages(): returns an array of all the received messages.



  • getReceivedMessagesForDomain(String domain): returns an array of all the received messages for a given domain.



  • getReceviedMessagesForDomain(String domain, String user): returns an array of all the received messages for a given user and domain.



  • getReceivedMessagesByCriteria(String criteria): returns an array of all the received messages that match a given criteria (e.g. subject, from, to, etc.).



  • waitForIncomingEmail(long timeout, int emailCount): waits for a given number of emails to arrive within a given timeout.



You can also use the web interface to view and verify emails by accessing the following URL:




This URL will show you a list of all the emails that have been sent or received by the GreenMail server. You can also view the details of each email, such as the headers, the body, and the attachments. You can also delete or download the emails as you wish. 44f88ac181


0 views0 comments

Recent Posts

See All

Comments


bottom of page