Skip to content

Commit

Permalink
Configurable filename encoding in MimeMessageHelper
Browse files Browse the repository at this point in the history
Closes gh-25755
  • Loading branch information
jhoeller committed Sep 14, 2020
1 parent 16d125c commit 49d65d5
Showing 1 changed file with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -159,8 +159,6 @@ public class MimeMessageHelper {

private static final String HEADER_PRIORITY = "X-Priority";

private static final String HEADER_CONTENT_ID = "Content-ID";


private final MimeMessage mimeMessage;

Expand All @@ -175,6 +173,8 @@ public class MimeMessageHelper {

private FileTypeMap fileTypeMap;

private boolean encodeFilenames = true;

private boolean validateAddresses = false;


Expand Down Expand Up @@ -464,7 +464,7 @@ protected FileTypeMap getDefaultFileTypeMap(MimeMessage mimeMessage) {
* Set the Java Activation Framework {@code FileTypeMap} to use
* for determining the content type of inline content and attachments
* that get added to the message.
* <p>Default is the {@code FileTypeMap} that the underlying
* <p>The default is the {@code FileTypeMap} that the underlying
* MimeMessage carries, if any, or the Activation Framework's default
* {@code FileTypeMap} instance else.
* @see #addInline
Expand All @@ -480,18 +480,40 @@ public void setFileTypeMap(@Nullable FileTypeMap fileTypeMap) {

/**
* Return the {@code FileTypeMap} used by this MimeMessageHelper.
* @see #setFileTypeMap
*/
public FileTypeMap getFileTypeMap() {
return this.fileTypeMap;
}


/**
* Set whether to encode attachment filenames passed to this helper's
* {@code #addAttachment} methods.
* <p>The default is {@code true} for compatibility with older email clients;
* turn this to {@code false} for standard MIME behavior. On a related note,
* check out JavaMail's {@code mail.mime.encodefilename} system property.
* @since 5.2.9
* @see #addAttachment(String, DataSource)
* @see MimeBodyPart#setFileName(String)
*/
public void setEncodeFilenames(boolean encodeFilenames) {
this.encodeFilenames = encodeFilenames;
}

/**
* Return whether to encode attachment filenames passed to this helper's
* {@code #addAttachment} methods.
* @since 5.2.9
* @see #setEncodeFilenames
*/
public boolean isEncodeFilenames() {
return this.encodeFilenames;
}

/**
* Set whether to validate all addresses which get passed to this helper.
* Default is "false".
* <p>Note that this is by default just available for JavaMail >= 1.3.
* You can override the default {@code validateAddress method} for
* validation on older JavaMail versions (or for custom validation).
* <p>The default is {@code false}.
* @see #validateAddress
*/
public void setValidateAddresses(boolean validateAddresses) {
Expand All @@ -500,6 +522,7 @@ public void setValidateAddresses(boolean validateAddresses) {

/**
* Return whether this helper will validate all addresses passed to it.
* @see #setValidateAddresses
*/
public boolean isValidateAddresses() {
return this.validateAddresses;
Expand All @@ -508,10 +531,8 @@ public boolean isValidateAddresses() {
/**
* Validate the given mail address.
* Called by all of MimeMessageHelper's address setters and adders.
* <p>Default implementation invokes {@code InternetAddress.validate()},
* <p>The default implementation invokes {@link InternetAddress#validate()},
* provided that address validation is activated for the helper instance.
* <p>Note that this method will just work on JavaMail >= 1.3. You can override
* it for validation on older JavaMail versions or for custom validation.
* @param address the address to validate
* @throws AddressException if validation failed
* @see #isValidateAddresses()
Expand All @@ -525,7 +546,8 @@ protected void validateAddress(InternetAddress address) throws AddressException

/**
* Validate all given mail addresses.
* Default implementation simply delegates to validateAddress for each address.
* <p>The default implementation simply delegates to {@link #validateAddress}
* for each address.
* @param addresses the addresses to validate
* @throws AddressException if validation failed
* @see #validateAddress(InternetAddress)
Expand Down Expand Up @@ -885,9 +907,7 @@ public void addInline(String contentId, DataSource dataSource) throws MessagingE
Assert.notNull(dataSource, "DataSource must not be null");
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
// We're using setHeader here to remain compatible with JavaMail 1.2,
// rather than JavaMail 1.3's setContentID.
mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">");
mimeBodyPart.setContentID("<" + contentId + ">");
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
getMimeMultipart().addBodyPart(mimeBodyPart);
}
Expand Down Expand Up @@ -997,7 +1017,8 @@ public void addAttachment(String attachmentFilename, DataSource dataSource) thro
try {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
mimeBodyPart.setFileName(isEncodeFilenames() ?
MimeUtility.encodeText(attachmentFilename) : attachmentFilename);
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
getRootMimeMultipart().addBodyPart(mimeBodyPart);
}
Expand Down

0 comments on commit 49d65d5

Please sign in to comment.