Skip to content

Commit 49d65d5

Browse files
committed
Configurable filename encoding in MimeMessageHelper
Closes gh-25755
1 parent 16d125c commit 49d65d5

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -159,8 +159,6 @@ public class MimeMessageHelper {
159159

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

162-
private static final String HEADER_CONTENT_ID = "Content-ID";
163-
164162

165163
private final MimeMessage mimeMessage;
166164

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

176174
private FileTypeMap fileTypeMap;
177175

176+
private boolean encodeFilenames = true;
177+
178178
private boolean validateAddresses = false;
179179

180180

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

481481
/**
482482
* Return the {@code FileTypeMap} used by this MimeMessageHelper.
483+
* @see #setFileTypeMap
483484
*/
484485
public FileTypeMap getFileTypeMap() {
485486
return this.fileTypeMap;
486487
}
487488

488489

490+
/**
491+
* Set whether to encode attachment filenames passed to this helper's
492+
* {@code #addAttachment} methods.
493+
* <p>The default is {@code true} for compatibility with older email clients;
494+
* turn this to {@code false} for standard MIME behavior. On a related note,
495+
* check out JavaMail's {@code mail.mime.encodefilename} system property.
496+
* @since 5.2.9
497+
* @see #addAttachment(String, DataSource)
498+
* @see MimeBodyPart#setFileName(String)
499+
*/
500+
public void setEncodeFilenames(boolean encodeFilenames) {
501+
this.encodeFilenames = encodeFilenames;
502+
}
503+
504+
/**
505+
* Return whether to encode attachment filenames passed to this helper's
506+
* {@code #addAttachment} methods.
507+
* @since 5.2.9
508+
* @see #setEncodeFilenames
509+
*/
510+
public boolean isEncodeFilenames() {
511+
return this.encodeFilenames;
512+
}
513+
489514
/**
490515
* Set whether to validate all addresses which get passed to this helper.
491-
* Default is "false".
492-
* <p>Note that this is by default just available for JavaMail >= 1.3.
493-
* You can override the default {@code validateAddress method} for
494-
* validation on older JavaMail versions (or for custom validation).
516+
* <p>The default is {@code false}.
495517
* @see #validateAddress
496518
*/
497519
public void setValidateAddresses(boolean validateAddresses) {
@@ -500,6 +522,7 @@ public void setValidateAddresses(boolean validateAddresses) {
500522

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

526547
/**
527548
* Validate all given mail addresses.
528-
* Default implementation simply delegates to validateAddress for each address.
549+
* <p>The default implementation simply delegates to {@link #validateAddress}
550+
* for each address.
529551
* @param addresses the addresses to validate
530552
* @throws AddressException if validation failed
531553
* @see #validateAddress(InternetAddress)
@@ -885,9 +907,7 @@ public void addInline(String contentId, DataSource dataSource) throws MessagingE
885907
Assert.notNull(dataSource, "DataSource must not be null");
886908
MimeBodyPart mimeBodyPart = new MimeBodyPart();
887909
mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
888-
// We're using setHeader here to remain compatible with JavaMail 1.2,
889-
// rather than JavaMail 1.3's setContentID.
890-
mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">");
910+
mimeBodyPart.setContentID("<" + contentId + ">");
891911
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
892912
getMimeMultipart().addBodyPart(mimeBodyPart);
893913
}
@@ -997,7 +1017,8 @@ public void addAttachment(String attachmentFilename, DataSource dataSource) thro
9971017
try {
9981018
MimeBodyPart mimeBodyPart = new MimeBodyPart();
9991019
mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
1000-
mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
1020+
mimeBodyPart.setFileName(isEncodeFilenames() ?
1021+
MimeUtility.encodeText(attachmentFilename) : attachmentFilename);
10011022
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
10021023
getRootMimeMultipart().addBodyPart(mimeBodyPart);
10031024
}

0 commit comments

Comments
 (0)