Configurable filename encoding in MimeMessageHelper
Closes gh-25755
This commit is contained in:
parent
16d125ce76
commit
49d65d5c41
|
|
@ -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.
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -175,6 +173,8 @@ public class MimeMessageHelper {
|
|||
|
||||
private FileTypeMap fileTypeMap;
|
||||
|
||||
private boolean encodeFilenames = true;
|
||||
|
||||
private boolean validateAddresses = false;
|
||||
|
||||
|
||||
|
|
@ -464,7 +464,7 @@ public class MimeMessageHelper {
|
|||
* 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
|
||||
|
|
@ -480,18 +480,40 @@ public class MimeMessageHelper {
|
|||
|
||||
/**
|
||||
* 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) {
|
||||
|
|
@ -500,6 +522,7 @@ public class MimeMessageHelper {
|
|||
|
||||
/**
|
||||
* Return whether this helper will validate all addresses passed to it.
|
||||
* @see #setValidateAddresses
|
||||
*/
|
||||
public boolean isValidateAddresses() {
|
||||
return this.validateAddresses;
|
||||
|
|
@ -508,10 +531,8 @@ public class MimeMessageHelper {
|
|||
/**
|
||||
* 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()
|
||||
|
|
@ -525,7 +546,8 @@ public class MimeMessageHelper {
|
|||
|
||||
/**
|
||||
* 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)
|
||||
|
|
@ -885,9 +907,7 @@ public class MimeMessageHelper {
|
|||
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);
|
||||
}
|
||||
|
|
@ -997,7 +1017,8 @@ public class MimeMessageHelper {
|
|||
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue