From be61a6fdc1047e93301a00890292ff697be0598c Mon Sep 17 00:00:00 2001 From: Marcel May Date: Sun, 17 Jul 2022 21:05:38 +0200 Subject: [PATCH] Problems with unicode in sent message (backport) #470 Backport of #460: Fixes trailing \r\n for IMAP msg.getContent() SmtpConnection now strips the last \r\n before terminating dot of a received message. So for an IMAP plain text message, msg.getContent() equals to the sent plain text content, without being forced to trim off '\r\n' from the received content. Note that messages retrieved with POP3 still have trailing '\r\n'. --- docs/index.html | 16 ++++++------- .../greenmail/pop3/Pop3Connection.java | 9 +++++++ .../greenmail/pop3/commands/RetrCommand.java | 5 ++-- .../greenmail/smtp/SmtpConnection.java | 2 +- .../greenmail/util/GreenMailUtil.java | 7 ++++-- .../greenmail/examples/ExampleRuleTest.java | 9 ++++--- .../examples/ExampleSendNoRuleAdvTest.java | 7 +++--- .../examples/ExampleSendNoRuleSimpleTest.java | 7 ++++-- .../greenmail/examples/ExampleSendTest.java | 7 ++++-- .../test/AuthenticationDisabledTest.java | 2 +- .../greenmail/test/GreenMailUtilTest.java | 6 ++--- .../greenmail/test/ImapServerTest.java | 10 ++++---- .../greenmail/test/Pop3ServerTest.java | 24 ++++++++++--------- ...ndReceiveWithInternationalAddressTest.java | 11 +++++---- .../greenmail/test/SmtpSecureServerTest.java | 2 +- .../greenmail/test/SmtpServerTest.java | 17 ++++++------- .../test/specificmessages/EncodingTest.java | 16 +++++++++++++ .../specificmessages/SenderRecipientTest.java | 4 ++-- .../greenmail/junit5/CustomSetupTests.java | 7 ++++-- .../greenmail/junit5/DefaultSetupTests.java | 6 +++-- .../junit5/WithConfigurationTests.java | 6 +++-- 21 files changed, 114 insertions(+), 66 deletions(-) diff --git a/docs/index.html b/docs/index.html index 6ab8fc2ca..26f914f4c 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 e5bec1fe7..d3882b2ab 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 5b0439b76..9b67bea43 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 @@ -44,12 +44,11 @@ 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) { conn.println("-ERR " + e); } } -} \ No newline at end of file +} 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 196581ae0..fda9449ec 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 35d4b32e4..7c27308a1 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 5a3368ee6..c7872155b 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 javax.mail.MessagingException; import javax.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 9128a0e89..ce2359025 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 7fa71894a..3b19f5b8c 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 javax.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 140dfd55b..4ba16ffa7 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 javax.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 5dbd9a8bb..aa370d17a 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 93c1b7cc9..7f32737d4 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 1e552f8e1..2e18fae96 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 @@ -69,7 +69,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); } } @@ -86,7 +86,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); } } @@ -109,7 +109,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); } } @@ -132,10 +132,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 33cb83c50..987616520 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 @@ -15,6 +15,7 @@ import com.icegreen.greenmail.user.UserException; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.Retriever; +import com.icegreen.greenmail.util.ServerSetup; import com.icegreen.greenmail.util.ServerSetupTest; import com.sun.mail.pop3.POP3Folder; import com.sun.mail.pop3.POP3Store; @@ -25,12 +26,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 +58,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 +81,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 +104,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 +129,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 e575d9394..f0d949726 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 javax.mail.internet.MimeMessage; import javax.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 b80f73c4d..09f36ab58 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 ce39a8dc9..6e7e0a73e 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 @@ -210,7 +211,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 24df4a752..89b4e8310 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 dc7646ddc..88d4cd879 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 7b47d72b9..037ff142a 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,6 +1,7 @@ package com.icegreen.greenmail.junit5; import javax.mail.internet.MimeMessage; +import javax.mail.MessagingException; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetupTest; @@ -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 69a787e79..0e524da38 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 3ab7b17b2..7194c9a7d 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()); } }