Index: src/java/org/apache/commons/mail/Email.java =================================================================== --- src/java/org/apache/commons/mail/Email.java (revision 219266) +++ src/java/org/apache/commons/mail/Email.java (working copy) @@ -15,6 +15,7 @@ */ package org.apache.commons.mail; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -23,6 +24,8 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.regex.Pattern; +import java.util.regex.Matcher; import javax.mail.Authenticator; import javax.mail.Message; @@ -265,37 +268,7 @@ public void setContent(Object aObject, String aContentType) { this.content = aObject; - if (!StringUtils.isNotEmpty(aContentType)) - { - this.contentType = null; - } - else - { - // set the content type this.contentType = aContentType; - - // set the charset if the input was properly formed - String strMarker = "; charset="; - int charsetPos = aContentType.toLowerCase().indexOf(strMarker); - - if (charsetPos != -1) - { - // find the next space (after the marker) - charsetPos += strMarker.length(); - int intCharsetEnd = - aContentType.toLowerCase().indexOf(" ", charsetPos); - - if (intCharsetEnd != -1) - { - this.charset = - aContentType.substring(charsetPos, intCharsetEnd); - } - else - { - this.charset = aContentType.substring(charsetPos); - } - } - } } /** @@ -400,14 +373,7 @@ name = email; } - if (StringUtils.isNotEmpty(this.charset)) - { - address = new InternetAddress(email, name, this.charset); - } - else - { address = new InternetAddress(email, name); - } address.validate(); } @@ -706,6 +672,12 @@ */ public abstract Email setMsg(String msg) throws EmailException; + /* + * to be written... + */ + private static final Pattern charsetParamPattern = Pattern.compile( + ";\\s*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]\\s*=\\s*((\"[^\"&&\\S]*\")|([^;^\"&&\\S]*))"); + /** * Does the work of actually sending the email. * @@ -718,37 +690,77 @@ this.getMailSession(); this.message = new MimeMessage(this.session); - if (StringUtils.isNotEmpty(this.subject)) + if (this.content != null) { - if (StringUtils.isNotEmpty(this.charset)) + if (StringUtils.isNotEmpty(this.contentType)) { - this.message.setSubject(this.subject, this.charset); + StringBuffer contentTypeBuf = new StringBuffer(); + Matcher matcher = charsetParamPattern.matcher(this.contentType); + if (matcher.find()) + { + while (matcher.find()) + matcher.appendReplacement(contentTypeBuf, ""); + matcher.appendTail(contentTypeBuf); } else { - this.message.setSubject(this.subject); + contentTypeBuf.append(this.contentType); + if (StringUtils.isNotEmpty(this.charset)) + contentTypeBuf.append("; charset=").append(this.charset); } + this.message.setContent(this.content, contentTypeBuf.toString()); } - - // ======================================================== - // Start of replacement code - if (this.content != null) + else { - this.message.setContent(this.content, this.contentType); + if (StringUtils.isNotEmpty(this.charset)) + { + this.message.setText((String)this.content, this.charset); + } + else + { + this.message.setText((String)this.content); + } + } } - // end of replacement code - // ======================================================== else if (this.emailBody != null) { this.message.setContent(this.emailBody); } else { - this.message.setContent("", Email.TEXT_PLAIN); + this.message.setText(""); } + if (StringUtils.isNotEmpty(this.subject)) + { + if (StringUtils.isNotEmpty(this.charset)) + { + this.message.setSubject(this.subject, this.charset); + } + else + { + this.message.setSubject(this.subject); + } + } + + if (this.fromAddress != null) { + if (StringUtils.isNotEmpty(this.charset)) + { + String personal = this.fromAddress.getPersonal(); + if (StringUtils.isNotEmpty(personal)) + { + try + { + fromAddress.setPersonal(personal, this.charset); + } + catch (UnsupportedEncodingException e) + { + throw new EmailException(e); + } + } + } this.message.setFrom(this.fromAddress); } else @@ -851,11 +863,29 @@ * @param list A List. * @return An InternetAddress[]. */ - protected InternetAddress[] toInternetAddressArray(List list) + protected InternetAddress[] toInternetAddressArray(List list) throws EmailException { InternetAddress[] ia = (InternetAddress[]) list.toArray(new InternetAddress[0]); + if (StringUtils.isNotEmpty(this.charset)) + { + for (int i = 0; i < ia.length; i++) + { + String personal = ia[i].getPersonal(); + if (StringUtils.isNotEmpty(personal)) + { + try + { + ia[i].setPersonal(personal, this.charset); + } + catch (UnsupportedEncodingException e) + { + throw new EmailException(e); + } + } + } + } return ia; }