Skip to content

Commit

Permalink
Preload email files during greenmail start (issue #250)
Browse files Browse the repository at this point in the history
Added example for loading EML files
  • Loading branch information
marcelmay committed Aug 14, 2022
1 parent 4150fbf commit e76e7ae
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
6 changes: 6 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,12 @@ <h4>How can I let GreenMail dynamically choose available port?</h4>
greenMail.getSmtp().getServerSetup());
</code></pre><!-- @formatter:on -->

<h4>How can I pre-load an existing (eml) message?</h4>
<p>You can load an existing electronic mail (eml) messsage and store the message for a user in a mailbox folder.</p>
<p>
Example:
<a href="https://github.com/greenmail-mail-test/greenmail/blob/master/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExamplePreloadMailFromFsTest.java">ExamplePreloadMailFromFsTest.java</a>
</p>
</section>

<section id="download" class="anchor">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.icegreen.greenmail.examples;

import com.icegreen.greenmail.imap.ImapHostManager;
import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.user.UserManager;
import com.icegreen.greenmail.util.ServerSetupTest;
import jakarta.mail.Message;
import jakarta.mail.Session;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.junit.Rule;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

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

public class ExamplePreloadMailFromFsTest {
@Rule
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
public static final String EML_FILE_NAME = ExamplePreloadMailFromFsTest.class.getName() + ".eml";

@Test
public void testPreloadMailFromFs() throws Exception {
final Session session = greenMail.getSmtp().createSession();

// Create test data
final MimeMessage msg = new MimeMessage(session);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("foo@localhost"));
msg.setFrom("bar@localhost");
msg.setSubject("Hello");
msg.setText("Test message saved as eml (electronic mail format, aka internet message format)");
try (FileOutputStream os = new FileOutputStream(EML_FILE_NAME)) {
msg.writeTo(os);
}

// Load msg from file system
final ImapHostManager imapHostManager = greenMail.getManagers().getImapHostManager();
final UserManager userManager = greenMail.getManagers().getUserManager();
final GreenMailUser user = userManager.createUser("foo@localhost", "foo-login", "secret");
try (InputStream source = Files.newInputStream(Paths.get(EML_FILE_NAME))) {
final MimeMessage loadedMsg = new MimeMessage(session, source);
imapHostManager.getFolder(user, "INBOX").store(loadedMsg);
}

// Verify
final MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
assertThat(receivedMessages).hasSize(1);
final MimeMessage receivedMessage = receivedMessages[0];
assertThat(receivedMessage.getSubject()).isEqualTo(msg.getSubject());
assertThat(receivedMessage.getContent()).isEqualTo(msg.getContent());
assertThat(receivedMessage.getFrom()).isEqualTo(msg.getFrom());
assertThat(receivedMessage.getRecipients(Message.RecipientType.TO))
.isEqualTo(msg.getRecipients(Message.RecipientType.TO));
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.icegreen.greenmail.test.specificmessages;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import jakarta.mail.*;
import jakarta.mail.internet.*;

import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.util.EncodingUtil;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetupTest;
import com.sun.mail.imap.IMAPStore;
import jakarta.mail.*;
import jakarta.mail.internet.*;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

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

/**
Expand Down Expand Up @@ -44,15 +43,15 @@ public void testTextPlainWithUTF8() throws MessagingException, IOException {

MimeBodyPart html = new MimeBodyPart();
html.setContent(MimeUtility.encodeText("<!doctype html>" +
"<html lang=en>" +
"<head>" +
"<meta charset=utf-8>" +
"<title>Title with Umlaut \u00FC</title>" +
"</head>" +
"<body>" +
"<p>8BIT Content with umlaut ü</p>" +
"</body>" +
"</html>", "UTF-8", "B"), "text/html; charset=utf-8");
"<html lang=en>" +
"<head>" +
"<meta charset=utf-8>" +
"<title>Title with Umlaut \u00FC</title>" +
"</head>" +
"<body>" +
"<p>8BIT Content with umlaut ü</p>" +
"</body>" +
"</html>", "UTF-8", "B"), "text/html; charset=utf-8");
html.setHeader("Content-Transfer-Encoding", "8BIT");
multipart.addBodyPart(html);

Expand Down Expand Up @@ -84,7 +83,6 @@ public void testTextPlainWithUTF8() throws MessagingException, IOException {
inboxFolder.open(Folder.READ_WRITE);
Message[] messages = inboxFolder.getMessages();
MimeMessage msg = (MimeMessage) messages[0];
message.writeTo(new FileOutputStream("t.eml"));
assertThat(msg.getContentType().startsWith("multipart/alternative")).isTrue();
Multipart multipartReceived = (Multipart) msg.getContent();

Expand Down Expand Up @@ -124,7 +122,7 @@ public void testTextPlainWithUTF8AndGreenMailApi() throws MessagingException, IO

MimeMessage msg = greenMail.getReceivedMessages()[0];
assertThat(msg.getSubject()).isEqualTo(subject);
assertThat(msg.getContentType()).isEqualTo( "text/plain; charset=UTF-8");
assertThat(msg.getContentType()).isEqualTo("text/plain; charset=UTF-8");
assertThat(msg.getContent()).isEqualTo(content);
}
}

0 comments on commit e76e7ae

Please sign in to comment.