Author: tn Date: Tue May 6 20:32:09 2014 New Revision: 1592866 URL: http://svn.apache.org/r1592866 Log: [EMAIL-138] Filenames of attachments were not properly encoded. Thanks to qed.
Added: commons/proper/email/trunk/src/test/resources/eml/html-attachment-encoded-filename.eml Modified: commons/proper/email/trunk/src/changes/changes.xml commons/proper/email/trunk/src/main/java/org/apache/commons/mail/MultiPartEmail.java commons/proper/email/trunk/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java Modified: commons/proper/email/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/changes/changes.xml?rev=1592866&r1=1592865&r2=1592866&view=diff ============================================================================== --- commons/proper/email/trunk/src/changes/changes.xml (original) +++ commons/proper/email/trunk/src/changes/changes.xml Tue May 6 20:32:09 2014 @@ -23,6 +23,10 @@ <body> <release version="1.3.3" date="xxx"> + <action dev="tn" type="fix" issue="EMAIL-138" date="2014-05-06" due-to="qed"> + The filename of an attachment was not properly encoded in case it contained + non-ascii characters. + </action> <action dev="tn" type="fix" issue="EMAIL-137" date="2014-04-30" due-to="Alex Kogan"> MimeMessageParser did not correctly parse MimeMessage objects created by calling HtmlEmail.buildMimeMessage() and HtmlEmail.getMimeMessage(). Modified: commons/proper/email/trunk/src/main/java/org/apache/commons/mail/MultiPartEmail.java URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/main/java/org/apache/commons/mail/MultiPartEmail.java?rev=1592866&r1=1592865&r2=1592866&view=diff ============================================================================== --- commons/proper/email/trunk/src/main/java/org/apache/commons/mail/MultiPartEmail.java (original) +++ commons/proper/email/trunk/src/main/java/org/apache/commons/mail/MultiPartEmail.java Tue May 6 20:32:09 2014 @@ -19,6 +19,7 @@ package org.apache.commons.mail; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.net.URL; import javax.activation.DataHandler; @@ -30,6 +31,7 @@ import javax.mail.MessagingException; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimePart; +import javax.mail.internet.MimeUtility; /** * A multipart email. @@ -462,12 +464,17 @@ public class MultiPartEmail extends Emai BodyPart bodyPart = createBodyPart(); try { - getContainer().addBodyPart(bodyPart); - bodyPart.setDisposition(disposition); - bodyPart.setFileName(name); + bodyPart.setFileName(MimeUtility.encodeText(name)); bodyPart.setDescription(description); bodyPart.setDataHandler(new DataHandler(ds)); + + getContainer().addBodyPart(bodyPart); + } + catch (UnsupportedEncodingException uee) + { + // in case the filename could not be encoded + throw new EmailException(uee); } catch (MessagingException me) { Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java?rev=1592866&r1=1592865&r2=1592866&view=diff ============================================================================== --- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java (original) +++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java Tue May 6 20:32:09 2014 @@ -20,6 +20,8 @@ import java.io.File; import java.io.IOException; import java.net.URL; +import javax.mail.internet.MimeUtility; + import org.apache.commons.mail.mocks.MockHtmlEmailConcrete; import org.apache.commons.mail.settings.EmailConfiguration; import org.junit.Before; @@ -110,7 +112,7 @@ public class SendWithAttachmentsTest ext EmailAttachment attachment = new EmailAttachment(); File testFile = null; - /** File to used to test file attachmetns (Must be valid) */ + /** File to used to test file attachments (Must be valid) */ testFile = File.createTempFile("commons-email-testfile", ".txt"); // ==================================================================== @@ -126,8 +128,9 @@ public class SendWithAttachmentsTest ext this.email.setFrom(this.strTestMailFrom); this.email.addTo(this.strTestMailTo); - /** File to used to test file attachmetns (Must be valid) */ - attachment.setName("Test Attachment"); + /** File to be used to test file attachments (Must be valid) */ + /** Use umlaut characters to test if the filename is properly encoded */ + attachment.setName("Test Attachment - a>ä, o>ö, u>ü, au>äu"); attachment.setDescription("Test Attachment Desc"); attachment.setPath(testFile.getAbsolutePath()); this.email.attach(attachment); @@ -137,11 +140,7 @@ public class SendWithAttachmentsTest ext this.email.setCharset(EmailConstants.ISO_8859_1); this.email.setSubject(strSubject); - URL url = new URL(EmailConfiguration.TEST_URL); - String cid = this.email.embed(url, "Apache Logo"); - - String strHtmlMsg = - "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>"; + String strHtmlMsg = "<html>Test Message<html>"; this.email.setHtmlMsg(strHtmlMsg); this.email.setTextMsg( @@ -175,7 +174,7 @@ public class SendWithAttachmentsTest ext validateSend( this.fakeMailServer, strSubject, - attachment.getName(), + MimeUtility.encodeText(attachment.getName()), this.email.getFromAddress(), this.email.getToAddresses(), this.email.getCcAddresses(), Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java?rev=1592866&r1=1592865&r2=1592866&view=diff ============================================================================== --- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java (original) +++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java Tue May 6 20:32:09 2014 @@ -121,6 +121,38 @@ public class MimeMessageParserTest assertEquals("application/pdf", dataSource.getContentType()); } + @Test + public void testParseHtmlEmailWithAttachmentAndEncodedFilename() throws Exception + { + DataSource dataSource; + Session session = Session.getDefaultInstance(new Properties()); + MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/html-attachment-encoded-filename.eml")); + MimeMessageParser mimeMessageParser = new MimeMessageParser(message); + + mimeMessageParser.parse(); + + assertEquals("Test HTML Send #1 Subject (w charset)", mimeMessageParser.getSubject()); + assertNotNull(mimeMessageParser.getMimeMessage()); + assertTrue(mimeMessageParser.isMultipart()); + assertTrue(mimeMessageParser.hasHtmlContent()); + assertTrue(mimeMessageParser.hasPlainContent()); + assertNotNull(mimeMessageParser.getPlainContent()); + assertNotNull(mimeMessageParser.getHtmlContent()); + assertTrue(mimeMessageParser.getTo().size() == 1); + assertTrue(mimeMessageParser.getCc().size() == 0); + assertTrue(mimeMessageParser.getBcc().size() == 0); + assertEquals("test_f...@apache.org", mimeMessageParser.getFrom()); + assertEquals("test_f...@apache.org", mimeMessageParser.getReplyTo()); + assertTrue(mimeMessageParser.hasAttachments()); + List<?> attachmentList = mimeMessageParser.getAttachmentList(); + assertTrue(attachmentList.size() == 1); + + dataSource = mimeMessageParser.getAttachmentList().get(0); + assertNotNull(dataSource); + assertEquals("text/plain", dataSource.getContentType()); + assertEquals("Test Attachment - a>ä, o>ö, u>ü, au>äu", dataSource.getName()); + } + /** * This test parses an "email read notification" where the resulting data source has no name. Originally * the missing name caused a NPE in MimeUtility.decodeText(). Added: commons/proper/email/trunk/src/test/resources/eml/html-attachment-encoded-filename.eml URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/resources/eml/html-attachment-encoded-filename.eml?rev=1592866&view=auto ============================================================================== --- commons/proper/email/trunk/src/test/resources/eml/html-attachment-encoded-filename.eml (added) +++ commons/proper/email/trunk/src/test/resources/eml/html-attachment-encoded-filename.eml Tue May 6 20:32:09 2014 @@ -0,0 +1,40 @@ +Received: from tn-laptop (localhost [127.0.0.1]) + by tn-laptop + with SMTP (SubEthaSMTP 3.1.7) id HUVN974B + for test...@apache.org; + Tue, 06 May 2014 22:22:51 +0200 (CEST) +Date: Tue, 6 May 2014 22:22:51 +0200 (CEST) +From: test_f...@apache.org +To: test...@apache.org +Message-ID: <9955173.2.1399407771445.JavaMail.tn@tn-laptop> +Subject: Test HTML Send #1 Subject (w charset) +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="----=_Part_0_22474382.1399407771403" + +------=_Part_0_22474382.1399407771403 +Content-Type: multipart/alternative; + boundary="----=_Part_1_29246539.1399407771404" + +------=_Part_1_29246539.1399407771404 +Content-Type: text/plain; charset=ISO-8859-1 +Content-Transfer-Encoding: 7bit + +Your email client does not support HTML emails +------=_Part_1_29246539.1399407771404 +Content-Type: text/html; charset=ISO-8859-1 +Content-Transfer-Encoding: 7bit + +<html>Test Message<html> +------=_Part_1_29246539.1399407771404-- + +------=_Part_0_22474382.1399407771403 +Content-Type: text/plain; charset=us-ascii; + name="=?utf-8?Q?Test_Attachment_-_a>=C3=A4,_o>=C3=B6,_u>=C3=BC,_au>=C3=A4u?=" +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; + filename="=?utf-8?Q?Test_Attachment_-_a>=C3=A4,_o>=C3=B6,_u>=C3=BC,_au>=C3=A4u?=" +Content-Description: Test Attachment Desc + + +------=_Part_0_22474382.1399407771403--