[EMAIL-138] Filenames of attachments were not properly encoded. Thanks to qed.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/email/trunk@1592866 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 16545dd..3771fb0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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().
diff --git a/src/main/java/org/apache/commons/mail/MultiPartEmail.java b/src/main/java/org/apache/commons/mail/MultiPartEmail.java
index eb62b1e..b127feb 100644
--- a/src/main/java/org/apache/commons/mail/MultiPartEmail.java
+++ b/src/main/java/org/apache/commons/mail/MultiPartEmail.java
@@ -19,6 +19,7 @@
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.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;
+import javax.mail.internet.MimeUtility;
/**
* A multipart email.
@@ -462,12 +464,17 @@
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)
{
diff --git a/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java b/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java
index b37c693..02f2e3f 100644
--- a/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java
+++ b/src/test/java/org/apache/commons/mail/SendWithAttachmentsTest.java
@@ -20,6 +20,8 @@
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 @@
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 @@
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 @@
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 @@
validateSend(
this.fakeMailServer,
strSubject,
- attachment.getName(),
+ MimeUtility.encodeText(attachment.getName()),
this.email.getFromAddress(),
this.email.getToAddresses(),
this.email.getCcAddresses(),
diff --git a/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java b/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java
index 05dc6a1..4f8a4ef 100644
--- a/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java
+++ b/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java
@@ -121,6 +121,38 @@
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_from@apache.org", mimeMessageParser.getFrom());
+ assertEquals("test_from@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().
diff --git a/src/test/resources/eml/html-attachment-encoded-filename.eml b/src/test/resources/eml/html-attachment-encoded-filename.eml
new file mode 100644
index 0000000..91571be
--- /dev/null
+++ b/src/test/resources/eml/html-attachment-encoded-filename.eml
@@ -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_to@apache.org;
+ Tue, 06 May 2014 22:22:51 +0200 (CEST)
+Date: Tue, 6 May 2014 22:22:51 +0200 (CEST)
+From: test_from@apache.org
+To: test_to@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--