]> jaekl.net Git - cfb.git/commitdiff
SendMail: add quotes around boundary type specification.
authorChris Jaekl <cejaekl@yahoo.com>
Sat, 12 Dec 2015 11:44:29 +0000 (20:44 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Sat, 12 Dec 2015 11:44:29 +0000 (20:44 +0900)
prod/net/jaekl/qd/util/SendMail.java
test/net/jaekl/qd/util/SendMailTest.java

index dc65e4fdca9ce84f30e7aae751805795b4bbaeb5..b4c783488431f86bb53774569ac305e71774a9cb 100644 (file)
@@ -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) {
index b7c64ef677488f79ed8ec252398ad4e0cd22bdc9..e420d7aba7b9cb75ba876aac08dabff3ba538710 100644 (file)
@@ -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 = 
+                                 "<HTML>\n"
+                + "<HEAD>\n"
+                               + "<TITLE>Me Tarzan; you Jane\n</TITLE>"
+                               + "</HEAD>\n"
+                               + "<BODY>\n"
+                               + "<P>Tarzan like Jane.<BR/>Tarzan come see Jane soon.</P>\n"
+                               + "<P>Tarzan</P>\n"
+                               + "</BODY>\n"
+                               + "</HTML>";
+               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);
+               }
        }
 }