Author: tn Date: Fri Dec 26 14:25:27 2014 New Revision: 1647983 URL: http://svn.apache.org/r1647983 Log: [EMAIL-147] Added work-around for broken Html email when using Apache Geronimo JavaMail implementation. Thanks to Can Eskikaya
Modified: commons/proper/email/trunk/src/changes/changes.xml commons/proper/email/trunk/src/main/java/org/apache/commons/mail/HtmlEmail.java commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.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=1647983&r1=1647982&r2=1647983&view=diff ============================================================================== --- commons/proper/email/trunk/src/changes/changes.xml (original) +++ commons/proper/email/trunk/src/changes/changes.xml Fri Dec 26 14:25:27 2014 @@ -23,6 +23,10 @@ <body> <release version="1.4.0" date="2014-??-??"> + <action dev="tn" type="fix" issue="EMAIL-147" date="2014-12-26" due-to="Can Eskikaya"> + Html emails did not have the correct content-type set when using the Apache Geronimo + JavaMail implementation v1.8.x. + </action> <action dev="tn" type="add" issue="EMAIL-146" date="2014-12-01" due-to="Alexander Lehmann"> Added getter for the bounce address of an email to class Email. </action> Modified: commons/proper/email/trunk/src/main/java/org/apache/commons/mail/HtmlEmail.java URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/main/java/org/apache/commons/mail/HtmlEmail.java?rev=1647983&r1=1647982&r2=1647983&view=diff ============================================================================== --- commons/proper/email/trunk/src/main/java/org/apache/commons/mail/HtmlEmail.java (original) +++ commons/proper/email/trunk/src/main/java/org/apache/commons/mail/HtmlEmail.java Fri Dec 26 14:25:27 2014 @@ -585,6 +585,26 @@ public class HtmlEmail extends MultiPart // (property "mail.mime.charset") in case none has been set msgHtml.setText(this.html, this.charset, EmailConstants.TEXT_SUBTYPE_HTML); + // EMAIL-147: work-around for buggy JavaMail implementations; + // in case setText(...) does not set the correct content type, + // use the setContent() method instead. + final String contentType = msgHtml.getContentType(); + if (contentType == null || !contentType.equals(EmailConstants.TEXT_HTML)) + { + // apply default charset if one has been set + if (EmailUtils.isNotEmpty(this.charset)) + { + msgHtml.setContent(this.html, EmailConstants.TEXT_HTML + "; charset=" + this.charset); + } + else + { + // unfortunately, MimeUtility.getDefaultMIMECharset() is package private + // and thus can not be used to set the default system charset in case + // no charset has been provided by the user + msgHtml.setContent(this.html, EmailConstants.TEXT_HTML); + } + } + for (final InlineImage image : this.inlineEmbeds.values()) { bodyEmbedsContainer.addBodyPart(image.getMbp()); Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java?rev=1647983&r1=1647982&r2=1647983&view=diff ============================================================================== --- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java (original) +++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java Fri Dec 26 14:25:27 2014 @@ -16,12 +16,17 @@ */ package org.apache.commons.mail; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.List; + import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.internet.MimeMessage; @@ -30,6 +35,7 @@ import org.apache.commons.mail.mocks.Moc import org.apache.commons.mail.settings.EmailConfiguration; import org.apache.commons.mail.util.MimeMessageParser; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -316,7 +322,7 @@ public class HtmlEmailTest extends Abstr attachment.setPath(testFile.getAbsolutePath()); this.email.attach(attachment); - this.email.setAuthentication(this.strTestUser, this.strTestPasswd); + //this.email.setAuthentication(this.strTestUser, this.strTestPasswd); this.email.setCharset(EmailConstants.ISO_8859_1); this.email.setSubject(strSubject); @@ -496,8 +502,14 @@ public class HtmlEmailTest extends Abstr } @Test + @Ignore public void testSendWithDefaultCharset() throws Exception { + // Test is disabled as its result is dependent on the execution order: + // the mail.mime.charset property is normally cached by the MimeUtility + // class, thus setting it to another value while running the tests + // might not have the expected result. + // ==================================================================== // Test Success // ====================================================================