Green mail java api tutorial

GreenMail is a popular Java library used for testing and verifying email functionality in Java applications. Here is a step-by-step tutorial on how to use GreenMail in your Java project:

Step 1: Add GreenMail to your project

You can add GreenMail to your Maven project by adding the following dependency to your pom.xml file:

<dependency>
    <groupId>com.icegreen</groupId>
    <artifactId>greenmail</artifactId>
    <version>1.6.6</version>
    <scope>test</scope>
</dependency>

If you're using Gradle, you can add the following dependency to your build.gradle file:

testImplementation 'com.icegreen:greenmail:1.6.6'

Step 2: Create a GreenMail instance

To use GreenMail, you need to create an instance of the GreenMail class. You can do this by calling the GreenMail constructor and passing in the port number you want to use for the email server:

import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.util.GreenMail;

public class MyTest {
    @Rule
    public GreenMailRule greenMailRule = new GreenMailRule(2525); // Use port 2525

    private GreenMail greenMail;

    @Before
    public void setup() {
        greenMail = new GreenMail(greenMailRule);
    }
}

In this example, we're creating a GreenMailRule instance with the port number 2525, and then creating a GreenMail instance using the GreenMailRule.

Step 3: Send and receive emails

To send and receive emails using GreenMail, you can use the sendTextEmail and getMessages methods:

@Test
public void testSendAndReceiveEmail() {
    String to = "[email protected]";
    String subject = "Test email";
    String body = "This is a test email";

    greenMail.sendTextEmail(to, subject, body);

    List<Email> emails = greenMail.getMessages();
    assertEquals(1, emails.size());

    Email email = emails.get(0);
    assertEquals(to, email.getRecipient());
    assertEquals(subject, email.getSubject());
    assertEquals(body, email.getContent());
}

In this example, we're sending an email to the recipient [email protected] with the subject Test email and body This is a test email. We then retrieve the email using the getMessages method and verify its contents.

Step 4: Verify email contents

You can use the getRecipient, getSubject, and getContent methods to verify the contents of the email:

@Test
public void testVerifyEmailContents() {
    String to = "[email protected]";
    String subject = "Test email";
    String body = "This is a test email";

    greenMail.sendTextEmail(to, subject, body);

    Email email = greenMail.getMessages().get(0);
    assertEquals(to, email.getRecipient());
    assertEquals(subject, email.getSubject());
    assertEquals(body, email.getContent());
}

In this example, we're verifying that the email has the correct recipient, subject, and body.

Step 5: Clean up

After you're done testing, you should clean up the email server by calling the stop method:

@After
public void tearDown() {
    greenMail.stop();
}

This will stop the email server and free up any resources it was using.

That's it! With these steps, you should be able to use GreenMail to test and verify email functionality in your Java project.