package com.icegreen.greenmail.examples;

import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.mail.MailAddress;
import com.icegreen.greenmail.mail.MovingMessage;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.user.MessageDeliveryHandler;
import com.icegreen.greenmail.user.UserException;
import com.icegreen.greenmail.user.UserManager;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.Rule;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

import javax.mail.MessagingException;

public class ExampleUndeliverableTest {
    @Rule
    public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);

    @Test
    public void testSend() throws MessagingException {
        final UserManager userManager = greenMail.getManagers().getUserManager();
        MessageDeliveryHandler defaultMessageDeliveryHandler = userManager.getMessageDeliveryHandler();
        userManager.setMessageDeliveryHandler(new MessageDeliveryHandler() {
            @Override
            public GreenMailUser handle(MovingMessage msg, MailAddress mailAddress)
                    throws MessagingException, UserException {
                msg.getMessage().setSubject("Delivery Report");
                return defaultMessageDeliveryHandler.handle(msg, mailAddress);
            }
        });
        GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost",
                "some subject", "some body"); // --- Place your sending code here instead
        assertThat(greenMail.getReceivedMessages()[0].getSubject()).isEqualTo("Delivery Report");
    }
}

@@ -623,28 +623,109 @@
 l.Messag
-ingException
+e.RecipientType;%0Aimport javax.mail.MessagingException;%0Aimport javax.mail.internet.MimeMessage
 ;%0A%0Apubli
@@ -908,32 +908,47 @@
 ssagingException
+, UserException
  %7B%0A        final
@@ -1012,24 +1012,110 @@
 rManager();%0A
+        userManager.createUser(%22from@localhost%22, %22from@localhost%22, %22from@localhost%22);%0A
         Mess
@@ -1472,133 +1472,965 @@
     
-msg.getMessage().setSubject(%22Delivery Report%22);%0A                return defaultMessageDeliveryHandler.handle(msg, mailAddress)
+String email = mailAddress.getEmail();%0A                GreenMailUser user = userManager.getUserByEmail(email);%0A                if (user == null) %7B%0A                    user = userManager.getUserByEmail(msg.getReturnPath().getEmail());%0A                    if (user != null) %7B%0A                        MimeMessage dsnMessage = new MimeMessage(msg.getMessage().getSession());%0A                        dsnMessage.setRecipients(RecipientType.TO, msg.getReturnPath().getEmail());%0A                        dsnMessage.setSubject(%22Delivery Report%22);%0A                        dsnMessage.setText(%22...%22);%0A                        msg.setMimeMessage(dsnMessage);%0A                    %7D else %7B%0A                        user = defaultMessageDeliveryHandler.handle(msg, mailAddress);%0A                        // user = userManager.createUser(mailAddress.getEmail(), mailAddress.getEmail(), mailAddress.getEmail());%0A                    %7D%0A                %7D%0A                return user
 ;%0A  
@@ -2711,20 +2711,150 @@
 y Report%22);%0A
+        assertThat(greenMail.getReceivedMessages()%5B0%5D.getRecipients(RecipientType.TO)%5B0%5D.toString()).isEqualTo(%22from@localhost%22);%0A
     %7D%0A%7D%0A

Extend sample to send DSN to Return-Path