From 378e3cda95d14407750348bcf203f5876bc5cc03 Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Sat, 12 Dec 2015 20:44:29 +0900 Subject: [PATCH] SendMail: add quotes around boundary type specification. --- prod/net/jaekl/qd/util/SendMail.java | 2 +- test/net/jaekl/qd/util/SendMailTest.java | 97 +++++++++++++++++++----- 2 files changed, 77 insertions(+), 22 deletions(-) diff --git a/prod/net/jaekl/qd/util/SendMail.java b/prod/net/jaekl/qd/util/SendMail.java index dc65e4f..b4c7834 100644 --- a/prod/net/jaekl/qd/util/SendMail.java +++ b/prod/net/jaekl/qd/util/SendMail.java @@ -146,7 +146,7 @@ public class SendMail { void sendMimeHeaders(PrintWriter pw) { sendLine(pw, "MIME-Version: 1.0"); - sendLine(pw, "Content-Type: multipart/mixed; boundary=" + getBoundary()); + sendLine(pw, "Content-Type: multipart/mixed; boundary=\"" + getBoundary() + "\""); } void sendMimeParts(PrintWriter pw) { diff --git a/test/net/jaekl/qd/util/SendMailTest.java b/test/net/jaekl/qd/util/SendMailTest.java index b7c64ef..e420d7a 100644 --- a/test/net/jaekl/qd/util/SendMailTest.java +++ b/test/net/jaekl/qd/util/SendMailTest.java @@ -45,7 +45,7 @@ public class SendMailTest { + "To: jane@jane.net\r\n" + "Subject: Me Tarzan, you Jane\r\n" + "MIME-Version: 1.0\r\n" - + "Content-Type: multipart/mixed; boundary=snip_snip\r\n" + + "Content-Type: multipart/mixed; boundary=\"snip_snip\"\r\n" + "\r\n" + "--snip_snip\r\n" + "Content-Type: text/plain\r\n" @@ -58,27 +58,82 @@ public class SendMailTest { + "--snip_snip--\r\n" + "\r\n.\r\n" + "QUIT\r\n"; + final String HTML = + "\n" + + "\n" + + "Me Tarzan; you Jane\n" + + "\n" + + "\n" + + "

Tarzan like Jane.
Tarzan come see Jane soon.

\n" + + "

Tarzan

\n" + + "\n" + + ""; + final String HTML_SENT = + "MAIL FROM: tarzan@jane.net\r\n" + + "RCPT TO: jane@jane.net\r\n" + + "DATA\r\n" + + "From: tarzan@jane.net\r\n" + + "To: jane@jane.net\r\n" + + "Subject: Me Tarzan, you Jane\r\n" + + "MIME-Version: 1.0\r\n" + + "Content-Type: multipart/mixed; boundary=\"snip_snip\"\r\n" + + "\r\n" + + "--snip_snip\r\n" + + "Content-Type: text/html\r\n" + + "\r\n" + + HTML + + "\r\n\r\n--snip_snip--\r\n" + + "\r\n.\r\n" + + "QUIT\r\n"; - SmtpConversationMock conversat = new SmtpConversationMock(TYPICAL_INIT, TYPICAL); - SendMailMock smm = new SendMailMock(conversat); - - smm.setFrom("tarzan@jane.net"); - smm.addTo("jane@jane.net"); - smm.setSubject("Me Tarzan, you Jane"); - - MimePart part = new MimePart("text/plain", "Tarzan like Jane\nTarzan come see Jane soon\n\nTarzan"); - smm.addPart(part); - - smm.send(); - - OutputStreamMock osm = conversat.getOutputStream(); - String result = osm.mock_getContent(Charset.forName(FileMock.UTF_8)); - - int pos = result.indexOf("\r\n"); - assertTrue(pos > 0); - String afterHELO = result.substring(pos + 2); + // Try sending a plain-text email + { + SmtpConversationMock conversat = new SmtpConversationMock(TYPICAL_INIT, TYPICAL); + SendMailMock smm = new SendMailMock(conversat); + + smm.setFrom("tarzan@jane.net"); + smm.addTo("jane@jane.net"); + smm.setSubject("Me Tarzan, you Jane"); + + MimePart part = new MimePart("text/plain", "Tarzan like Jane\nTarzan come see Jane soon\n\nTarzan"); + smm.addPart(part); + + smm.send(); + + OutputStreamMock osm = conversat.getOutputStream(); + String result = osm.mock_getContent(Charset.forName(FileMock.UTF_8)); + + int pos = result.indexOf("\r\n"); + assertTrue(pos > 0); + String afterHELO = result.substring(pos + 2); + + assertTrue(result.startsWith("HELO ")); + assertEquals(TYPICAL_SENT, afterHELO); + } - assertTrue(result.startsWith("HELO ")); - assertEquals(TYPICAL_SENT, afterHELO); + // Try sending an HTML email + { + SmtpConversationMock conversat = new SmtpConversationMock(TYPICAL_INIT, TYPICAL); + SendMailMock smm = new SendMailMock(conversat); + + smm.setFrom("tarzan@jane.net"); + smm.addTo("jane@jane.net"); + smm.setSubject("Me Tarzan, you Jane"); + + MimePart part = new MimePart("text/html", HTML); + smm.addPart(part); + + smm.send(); + + OutputStreamMock osm = conversat.getOutputStream(); + String result = osm.mock_getContent(Charset.forName(FileMock.UTF_8)); + + int pos = result.indexOf("\r\n"); + assertTrue(pos > 0); + String afterHELO = result.substring(pos + 2); + + assertTrue(result.startsWith("HELO ")); + assertEquals(HTML_SENT, afterHELO); + } } } -- 2.39.2