diff --git a/docs/index.html b/docs/index.html index 6ab8fc2cae..26f914f4cd 100644 --- a/docs/index.html +++ b/docs/index.html @@ -213,7 +213,7 @@

Test Your Sending Code

public void testSend() throws MessagingException { GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "some subject", "some body"); // --- Place your sending code here instead - assertEquals("some body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0])); + assertEquals("some body", greenMail.getReceivedMessages()[0].getContent()); } @@ -432,7 +432,7 @@

Using JUnit4 rule based setup


 /** [See code on GitHub](https://github.com/greenmail-mail-test/greenmail/tree/master/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleRuleTest.java) */
 @Rule
-public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.ALL);
+public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
 
 @Test
 public void testSomething() {
@@ -440,7 +440,7 @@ 

Using JUnit4 rule based setup

MimeMessage[] emails = greenMail.getReceivedMessages(); assertEquals(1, emails.length); assertEquals("subject", emails[0].getSubject()); - assertEquals("body", GreenMailUtil.getBody(emails[0])); + assertEquals("body", emails[0].getContent()); // ... }
@@ -460,7 +460,7 @@

Using JUnit5 extension

GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "some subject", "some body"); final MimeMessage[] receivedMessages = greenMail.getReceivedMessages(); final MimeMessage receivedMessage = receivedMessages[0]; - assertEquals("some body", GreenMailUtil.getBody(receivedMessage)); + assertEquals("some body", receivedMessage.getContent()); } @@ -471,7 +471,7 @@

Testing your sending code (simple)

greenMail.start(); GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "some subject", "some body"); // --- Place your sending code here -assertEquals("some body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0])); +assertEquals("some body", greenMail.getReceivedMessages()[0].getContent()); greenMail.stop(); @@ -497,14 +497,14 @@

Testing your sending code (advanced)

// Simple message assertEquals(subject, messages[0].getSubject()); -assertEquals(body, GreenMailUtil.getBody(messages[0]).trim()); +assertEquals(body, messages[0].getContent()); //if you send content as a 2 part multipart... assertTrue(messages[1].getContent() instanceof MimeMultipart); MimeMultipart mp = (MimeMultipart) messages[1].getContent(); assertEquals(2, mp.getCount()); -assertEquals("body1", GreenMailUtil.getBody(mp.getBodyPart(0)).trim()); -assertEquals("body2", GreenMailUtil.getBody(mp.getBodyPart(1)).trim()); +assertEquals("body1", mp.getBodyPart(0).getContent()); +assertEquals("body2", mp.getBodyPart(1).getContent()); greenMail.stop(); diff --git a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3Connection.java b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3Connection.java index e5bec1fe7d..d3882b2ab5 100644 --- a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3Connection.java +++ b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3Connection.java @@ -80,11 +80,20 @@ public void println() { out.flush(); } + public void print(String line) { + out.print(line); + } + public void print(Reader in) throws IOException { StreamUtils.copy(in, out); out.flush(); } + public void println(Reader in) throws IOException { + StreamUtils.copy(in, out); + println(); + } + public String readLine() throws IOException { return in.readLine(); } diff --git a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/RetrCommand.java b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/RetrCommand.java index fb42ffd1fd..f68e20c7fe 100644 --- a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/RetrCommand.java +++ b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/RetrCommand.java @@ -14,7 +14,7 @@ import com.icegreen.greenmail.util.GreenMailUtil; import jakarta.mail.Flags; -import java.io.StringReader; + import java.util.List; @@ -44,8 +44,7 @@ public void execute(Pop3Connection conn, Pop3State state, StoredMessage msg = msgList.get(0); String email = GreenMailUtil.getWholeMessage(msg.getMimeMessage()); conn.println("+OK"); - conn.print(new StringReader(email)); - conn.println(); + conn.println(email); conn.println("."); msg.setFlag(Flags.Flag.SEEN, true); } catch (Exception e) { diff --git a/greenmail-core/src/main/java/com/icegreen/greenmail/smtp/SmtpConnection.java b/greenmail-core/src/main/java/com/icegreen/greenmail/smtp/SmtpConnection.java index 196581ae02..fda9449ec6 100644 --- a/greenmail-core/src/main/java/com/icegreen/greenmail/smtp/SmtpConnection.java +++ b/greenmail-core/src/main/java/com/icegreen/greenmail/smtp/SmtpConnection.java @@ -107,7 +107,7 @@ public InputStream dotLimitedInputStream(byte[] initialContent) { if (cbuf == CR_LF_DOT_CR && b == '\n') { // CRLF-DOT-CRLF final byte[] buf = bos.toByteArray(); - int maxLen = Math.min(bos.size(), bos.size() - 2 /* DOT + CR */); + int maxLen = Math.min(bos.size(), bos.size() - 4 /* CR + LF + DOT + CR */); return new ByteArrayInputStream(buf, 0, maxLen); } else if ((cbuf & 0xffffff) == CR_LF_DOT && b == '.') { // CR_LF_DOT and DOT => Skip dot once // https://tools.ietf.org/html/rfc5321#section-4.5.2 : diff --git a/greenmail-core/src/main/java/com/icegreen/greenmail/util/GreenMailUtil.java b/greenmail-core/src/main/java/com/icegreen/greenmail/util/GreenMailUtil.java index b422d4a426..231b093857 100644 --- a/greenmail-core/src/main/java/com/icegreen/greenmail/util/GreenMailUtil.java +++ b/greenmail-core/src/main/java/com/icegreen/greenmail/util/GreenMailUtil.java @@ -114,7 +114,10 @@ public static int getLineCount(String str) { } /** - * @return The content of an email (or a Part) + * @return The content part of an email (or a Part) + * + * Note: You might have to use MimeUtility.decodeText(contentPart) + * on the result to decode the (possibly) quoted-printable encoded special characters. */ public static String getBody(Part msg) { String all = getWholeMessage(msg); @@ -153,7 +156,7 @@ public static byte[] getHeaderAsBytes(Part part) { } /** - * @return same as {@link #getWholeMessage(jakarta.mail.Part)} } + * @return same as {@link #getWholeMessage(jakarta.mail.Part)} */ public static String toString(Part msg) { return getWholeMessage(msg); diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleRuleTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleRuleTest.java index 7001e7845e..585b595175 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleRuleTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleRuleTest.java @@ -9,6 +9,8 @@ import jakarta.mail.MessagingException; import jakarta.mail.internet.MimeMessage; +import java.io.IOException; + import static org.assertj.core.api.Assertions.assertThat; public class ExampleRuleTest { @@ -16,12 +18,13 @@ public class ExampleRuleTest { public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP_IMAP); @Test - public void testSomething() throws MessagingException { - GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "subject", "body"); + public void testSomething() throws MessagingException, IOException { + GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "subject", "content"); MimeMessage[] emails = greenMail.getReceivedMessages(); assertThat(emails.length).isEqualTo(1); assertThat(emails[0].getSubject()).isEqualTo("subject"); - assertThat(GreenMailUtil.getBody(emails[0])).isEqualTo("body"); + assertThat(emails[0].getContentType()).isEqualTo("text/plain; charset=us-ascii"); + assertThat(emails[0].getContent()).isEqualTo("content"); // ... } } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleAdvTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleAdvTest.java index b2f5dd37ca..df40d2c30c 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleAdvTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleAdvTest.java @@ -36,14 +36,15 @@ public void testSend() throws MessagingException, IOException { // Simple message assertThat(messages[0].getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body); + assertThat(messages[0].getContentType()).isEqualTo("text/plain; charset=us-ascii"); + assertThat(messages[0].getContent()).isEqualTo(body); //if you send content as a 2 part multipart... assertThat(messages[1].getContent() instanceof MimeMultipart).isTrue(); MimeMultipart mp = (MimeMultipart) messages[1].getContent(); assertThat(mp.getCount()).isEqualTo(2); - assertThat(GreenMailUtil.getBody(mp.getBodyPart(0)).trim()).isEqualTo("body1"); - assertThat(GreenMailUtil.getBody(mp.getBodyPart(1)).trim()).isEqualTo("body2"); + assertThat(mp.getBodyPart(0).getContent()).isEqualTo("body1"); + assertThat(mp.getBodyPart(1).getContent()).isEqualTo("body2"); } finally { greenMail.stop(); } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleSimpleTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleSimpleTest.java index 7fa71894ae..ae8df0aeda 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleSimpleTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendNoRuleSimpleTest.java @@ -3,19 +3,22 @@ import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetupTest; +import jakarta.mail.MessagingException; import org.junit.Test; +import java.io.IOException; + import static org.assertj.core.api.Assertions.assertThat; public class ExampleSendNoRuleSimpleTest { @Test - public void testSend() { + public void testSend() throws MessagingException, IOException { GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP); //uses test ports by default try { greenMail.start(); GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "some subject", "some body"); //replace this with your test message content - assertThat(GreenMailUtil.getBody(greenMail.getReceivedMessages()[0])).isEqualTo("some body"); + assertThat((greenMail.getReceivedMessages()[0].getContent())).isEqualTo("some body"); } finally { greenMail.stop(); } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendTest.java index 140dfd55b5..0b5712aed8 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/examples/ExampleSendTest.java @@ -3,9 +3,12 @@ import com.icegreen.greenmail.junit.GreenMailRule; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetupTest; +import jakarta.mail.MessagingException; import org.junit.Rule; import org.junit.Test; +import java.io.IOException; + import static org.assertj.core.api.Assertions.assertThat; public class ExampleSendTest { @@ -13,9 +16,9 @@ public class ExampleSendTest { public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP); @Test - public void testSend() { + public void testSend() throws MessagingException, IOException { GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "some subject", "some body"); // --- Place your sending code here instead - assertThat(GreenMailUtil.getBody(greenMail.getReceivedMessages()[0])).isEqualTo("some body"); + assertThat(greenMail.getReceivedMessages()[0].getContent()).isEqualTo("some body"); } } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/AuthenticationDisabledTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/AuthenticationDisabledTest.java index 0db7e1d07b..cb47919800 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/AuthenticationDisabledTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/AuthenticationDisabledTest.java @@ -38,7 +38,7 @@ public void testSendMailAndReceiveWithAuthDisabled() throws MessagingException, MimeMessage[] emails = greenMail.getReceivedMessages(); assertThat(emails.length).isEqualTo(1); assertThat(emails[0].getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(emails[0])).isEqualTo(body); + assertThat(emails[0].getContent()).isEqualTo(body); greenMail.waitForIncomingEmail(5000, 1); diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/GreenMailUtilTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/GreenMailUtilTest.java index 8621622bf8..1fc268a455 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/GreenMailUtilTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/GreenMailUtilTest.java @@ -58,19 +58,19 @@ public void testGetEmptyBodyAndHeader() throws Exception { MimeMultipart mp = (MimeMultipart) retriever.getMessages(to)[0].getContent(); BodyPart bp; bp = mp.getBodyPart(0); - assertThat(body).isEqualTo(GreenMailUtil.getBody(bp).trim()); + assertThat(body).isEqualTo(GreenMailUtil.getBody(bp)); assertThat( "Content-Type: text/plain; charset=us-ascii\r\n" + "Content-Transfer-Encoding: 7bit").isEqualTo( GreenMailUtil.getHeaders(bp).trim()); bp = mp.getBodyPart(1); - assertThat("AAEC").isEqualTo(GreenMailUtil.getBody(bp).trim()); + assertThat("AAEC").isEqualTo(GreenMailUtil.getBody(bp)); assertThat( "Content-Type: image/gif; name=testimage_filename\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment; filename=testimage_filename\r\n" + - "Content-Description: testimage_description").isEqualTo(GreenMailUtil.getHeaders(bp).trim()); + "Content-Description: testimage_description").isEqualTo(GreenMailUtil.getHeaders(bp)); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GreenMailUtil.copyStream(bp.getInputStream(), bout); diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/ImapServerTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/ImapServerTest.java index 88fc176fe9..db58d1b728 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/ImapServerTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/ImapServerTest.java @@ -64,7 +64,7 @@ public void testRetrieveSimple() throws Exception { Message[] messages = retriever.getMessages(to); assertThat(messages.length).isEqualTo(1); assertThat(messages[0].getSubject()).isEqualTo(subject); - assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body); + assertThat(((String) messages[0].getContent())).isEqualTo(body); } } @@ -81,7 +81,7 @@ public void testImapsReceive() throws Throwable { Message[] messages = retriever.getMessages(to); assertThat(messages.length).isEqualTo(1); assertThat(messages[0].getSubject()).isEqualTo(subject); - assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body); + assertThat(((String) messages[0].getContent())).isEqualTo(body); } } @@ -104,7 +104,7 @@ public void testRetrieveSimpleWithNonDefaultPassword() throws Exception { Message[] messages = retriever.getMessages(to, password); assertThat(messages.length).isEqualTo(1); assertThat(messages[0].getSubject()).isEqualTo(subject); - assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body); + assertThat(((String) messages[0].getContent())).isEqualTo(body); } } @@ -127,10 +127,10 @@ public void testRetrieveMultipart() throws Exception { assertThat(mp.getCount()).isEqualTo(2); BodyPart bp; bp = mp.getBodyPart(0); - assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo(body); + assertThat(bp.getContent()).isEqualTo(body); bp = mp.getBodyPart(1); - assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo("AAEC"); + assertThat(GreenMailUtil.getBody(bp)).isEqualTo("AAEC"); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GreenMailUtil.copyStream(bp.getInputStream(), bout); diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/Pop3ServerTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/Pop3ServerTest.java index 0892f0f174..2ab7c77dd6 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/Pop3ServerTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/Pop3ServerTest.java @@ -5,6 +5,8 @@ package com.icegreen.greenmail.test; import java.io.ByteArrayOutputStream; + +import com.icegreen.greenmail.util.ServerSetup; import jakarta.mail.AuthenticationFailedException; import jakarta.mail.BodyPart; import jakarta.mail.Message; @@ -25,12 +27,12 @@ /** * @author Wael Chatila - * @version $Id: $ - * @since Jan 28, 2006 */ public class Pop3ServerTest { @Rule - public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.ALL); + public final GreenMailRule greenMail = new GreenMailRule(new ServerSetup[]{ + ServerSetupTest.SMTP, ServerSetupTest.SMTPS, + ServerSetupTest.POP3, ServerSetupTest.POP3S}); @Test public void testPop3Capabillities() throws MessagingException, UserException { @@ -57,12 +59,13 @@ public void testRetrieve() throws Exception { try (Retriever retriever = new Retriever(greenMail.getPop3())) { Message[] messages = retriever.getMessages(to); assertThat(messages.length).isEqualTo(1); - assertThat(messages[0].getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body); + final Message message = messages[0]; + assertThat(message.getSubject()).isEqualTo(subject); + assertThat(message.getContent().toString().trim()).isEqualTo(body); // UID - POP3Folder f = (POP3Folder) messages[0].getFolder(); - assertThat(f.getUID(messages[0])).isNotEqualTo("UNKNOWN"); + POP3Folder f = (POP3Folder) message.getFolder(); + assertThat(f.getUID(message)).isNotEqualTo("UNKNOWN"); } } @@ -79,7 +82,7 @@ public void testPop3sReceive() throws Throwable { Message[] messages = retriever.getMessages(to); assertThat(messages.length).isEqualTo(1); assertThat(messages[0].getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body); + assertThat(messages[0].getContent().toString().trim()).isEqualTo(body); } } @@ -102,7 +105,7 @@ public void testRetrieveWithNonDefaultPassword() throws Exception { Message[] messages = retriever.getMessages(to, password); assertThat(messages.length).isEqualTo(1); assertThat(messages[0].getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body); + assertThat(messages[0].getContent().toString().trim()).isEqualTo(body); } } @@ -127,10 +130,10 @@ public void testRetrieveMultipart() throws Exception { assertThat(mp.getCount()).isEqualTo(2); BodyPart bp; bp = mp.getBodyPart(0); - assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo(body); + assertThat(bp.getContent()).isEqualTo(body); bp = mp.getBodyPart(1); - assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo("AAEC"); + assertThat(GreenMailUtil.getBody(bp)).isEqualTo("AAEC"); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GreenMailUtil.copyStream(bp.getInputStream(), bout); diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/SendReceiveWithInternationalAddressTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/SendReceiveWithInternationalAddressTest.java index 2609a83690..0b5e05ab00 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/SendReceiveWithInternationalAddressTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/SendReceiveWithInternationalAddressTest.java @@ -13,6 +13,7 @@ import jakarta.mail.internet.MimeMessage; import jakarta.mail.internet.MimeUtility; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; @@ -33,8 +34,7 @@ public class SendReceiveWithInternationalAddressTest { public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP_IMAP); @Test - public void testSend() throws MessagingException, UnsupportedEncodingException { - + public void testSend() throws MessagingException, IOException { Session session = GreenMailUtil.getSession(ServerSetupTest.SMTP, properties); MimeMessage mimeMessage = new MockInternationalizedMimeMessage(session); mimeMessage.setSubject("subject"); @@ -45,13 +45,14 @@ public void testSend() throws MessagingException, UnsupportedEncodingException { mimeMessage.setRecipients(Message.RecipientType.BCC, "राममो@हन.ईन्फो"); // The body text needs to be encoded if it contains non us-ascii characters - mimeMessage.setText(MimeUtility.encodeText("用户@例子")); + mimeMessage.setText("用户@例子","UTF-8"); GreenMailUtil.sendMimeMessage(mimeMessage); // Decoding the body text to verify equality - String decodedText = MimeUtility.decodeText(GreenMailUtil.getBody(greenMail.getReceivedMessages()[0])); - assertThat(decodedText).isEqualTo("用户@例子"); + final MimeMessage receivedMessage = greenMail.getReceivedMessages()[0]; + assertThat(receivedMessage.getContentType()).isEqualTo("text/plain; charset=UTF-8"); + assertThat(receivedMessage.getContent()).isEqualTo("用户@例子"); } // This is a mock message that doesn't implement the full functionality from MimeMessage. diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpSecureServerTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpSecureServerTest.java index 254c5487ba..d12133622c 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpSecureServerTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpSecureServerTest.java @@ -26,6 +26,6 @@ public void testSmtpsServerReceive() throws Throwable { MimeMessage[] emails = greenMail.getReceivedMessages(); assertThat(emails).hasSize(1); assertThat(emails[0].getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(emails[0]).trim()).isEqualTo(body); + assertThat(emails[0].getContent()).isEqualTo(body); } } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpServerTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpServerTest.java index 856be07bc2..fe212a04ef 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpServerTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/SmtpServerTest.java @@ -8,6 +8,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.net.SocketException; import java.util.Properties; @@ -42,12 +43,12 @@ public class SmtpServerTest { public final GreenMailRule greenMail = new GreenMailRule(new ServerSetup[]{ServerSetupTest.SMTP}); @Test - public void testSmtpServerBasic() throws MessagingException { + public void testSmtpServerBasic() throws MessagingException, IOException { GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "subject", "body"); MimeMessage[] emails = greenMail.getReceivedMessages(); assertThat(emails.length).isEqualTo(1); assertThat(emails[0].getSubject()).isEqualTo("subject"); - assertThat(GreenMailUtil.getBody(emails[0])).isEqualTo("body"); + assertThat(emails[0].getContent()).isEqualTo("body"); } @Test @@ -71,7 +72,7 @@ public void testSmtpServerReceiveWithSetup() throws Throwable { MimeMessage[] emails = greenMail.getReceivedMessages(); assertThat(emails.length).isEqualTo(1); assertThat(emails[0].getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(emails[0]).trim()).isEqualTo(body); + assertThat(emails[0].getContent()).isEqualTo(body); } @@ -113,10 +114,10 @@ public void testSmtpServerReceiveMultipart() throws Exception { assertThat(mp.getCount()).isEqualTo(2); BodyPart bp; bp = mp.getBodyPart(0); - assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo(body); + assertThat(bp.getContent()).isEqualTo(body); bp = mp.getBodyPart(1); - assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo("AAEC"); + assertThat(GreenMailUtil.getBody(bp)).isEqualTo("AAEC"); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GreenMailUtil.copyStream(bp.getInputStream(), bout); @@ -127,13 +128,13 @@ public void testSmtpServerReceiveMultipart() throws Exception { } @Test - public void testSmtpServerLeadingPeriods() throws MessagingException { + public void testSmtpServerLeadingPeriods() throws MessagingException, IOException { String body = ". body with leading period"; GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "subject", body); MimeMessage[] emails = greenMail.getReceivedMessages(); assertThat(emails.length).isEqualTo(1); assertThat(emails[0].getSubject()).isEqualTo("subject"); - assertThat(GreenMailUtil.getBody(emails[0])).isEqualTo(body); + assertThat(emails[0].getContent()).isEqualTo(body); } @Test @@ -211,7 +212,7 @@ public void testAuth() throws Throwable { assertThat(emails.length).isEqualTo(2); for (MimeMessage receivedMsg : emails) { assertThat(receivedMsg.getSubject()).isEqualTo(subject); - assertThat(GreenMailUtil.getBody(receivedMsg).trim()).isEqualTo(body); + assertThat(receivedMsg.getContent()).isEqualTo(body); } } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/EncodingTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/EncodingTest.java index dc557cef8f..e4fadcd072 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/EncodingTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/EncodingTest.java @@ -111,4 +111,20 @@ public void testTextPlainWithUTF8() throws MessagingException, IOException { store.close(); } } + + @Test + public void testTextPlainWithUTF8AndGreenMailApi() throws MessagingException, IOException { + String content = "This is a test with ünicöde: \uD83C\uDF36"; + String subject = "Some sübject"; + + GreenMailUtil.sendTextEmailTest( + "to@localhost", "from@localhost", subject, + content + ); + + MimeMessage msg = greenMail.getReceivedMessages()[0]; + assertThat(msg.getSubject()).isEqualTo(subject); + assertThat(msg.getContentType()).isEqualTo( "text/plain; charset=UTF-8"); + assertThat(msg.getContent()).isEqualTo(content); + } } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/SenderRecipientTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/SenderRecipientTest.java index 49e4353ef8..441c6d5c9a 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/SenderRecipientTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/specificmessages/SenderRecipientTest.java @@ -86,10 +86,10 @@ public void testSendersAndRecipients() throws MessagingException { } @Test - public void testSendWithoutSubject() { + public void testSendWithoutSubject() throws MessagingException, IOException { GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", null, "some subject less body"); - assertThat(GreenMailUtil.getBody(greenMail.getReceivedMessages()[0])).isEqualTo("some subject less body"); + assertThat(greenMail.getReceivedMessages()[0].getContent()).isEqualTo("some subject less body"); } @Test diff --git a/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/CustomSetupTests.java b/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/CustomSetupTests.java index a574c11f47..cf39cc58a3 100644 --- a/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/CustomSetupTests.java +++ b/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/CustomSetupTests.java @@ -1,5 +1,6 @@ package com.icegreen.greenmail.junit5; +import jakarta.mail.MessagingException; import jakarta.mail.internet.MimeMessage; import com.icegreen.greenmail.util.GreenMailUtil; @@ -8,6 +9,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import java.io.IOException; + import static org.junit.jupiter.api.Assertions.assertEquals; @DisplayName("GreenMail with custom ServerSetup tests") @@ -17,10 +20,10 @@ class CustomSetupTests { @Test @DisplayName("Send test") - void testSend() { + void testSend() throws MessagingException, IOException { GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "some subject", "some body"); final MimeMessage[] receivedMessages = greenMail.getReceivedMessages(); final MimeMessage receivedMessage = receivedMessages[0]; - assertEquals("some body", GreenMailUtil.getBody(receivedMessage)); + assertEquals("some body", receivedMessage.getContent()); } } diff --git a/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/DefaultSetupTests.java b/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/DefaultSetupTests.java index f1e5187873..19f6c15a7a 100644 --- a/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/DefaultSetupTests.java +++ b/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/DefaultSetupTests.java @@ -9,6 +9,8 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.RegisterExtension; +import java.io.IOException; + import static org.junit.jupiter.api.Assertions.assertEquals; @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -19,12 +21,12 @@ class DefaultSetupTests { @Test @DisplayName("Send test") - void testSend() throws MessagingException { + void testSend() throws MessagingException, IOException { GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "subject", "body"); final MimeMessage[] emails = greenMail.getReceivedMessages(); assertEquals(1, emails.length); final MimeMessage email = emails[0]; assertEquals("subject", email.getSubject()); - assertEquals("body", GreenMailUtil.getBody(email)); + assertEquals("body", email.getContent()); } } diff --git a/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/WithConfigurationTests.java b/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/WithConfigurationTests.java index 6022bb31a8..fb818d0cb5 100644 --- a/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/WithConfigurationTests.java +++ b/greenmail-junit5/src/test/java/com/icegreen/greenmail/junit5/WithConfigurationTests.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.RegisterExtension; +import java.io.IOException; + import static org.junit.jupiter.api.Assertions.assertEquals; @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -22,12 +24,12 @@ class WithConfigurationTests { @Test @DisplayName("Receive test") - void testReceive() throws MessagingException { + void testReceive() throws MessagingException, IOException { GreenMailUtil.sendTextEmailTest("to@localhost", "from@localhost", "subject", "body"); final MimeMessage[] emails = greenMail.getReceivedMessages(); assertEquals(1, emails.length); final MimeMessage email = emails[0]; assertEquals("subject", email.getSubject()); - assertEquals("body", GreenMailUtil.getBody(email)); + assertEquals("body", email.getContent()); } }