1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -159,8 +159,6 @@ public class MimeMessageHelper {
159
159
160
160
private static final String HEADER_PRIORITY = "X-Priority" ;
161
161
162
- private static final String HEADER_CONTENT_ID = "Content-ID" ;
163
-
164
162
165
163
private final MimeMessage mimeMessage ;
166
164
@@ -175,6 +173,8 @@ public class MimeMessageHelper {
175
173
176
174
private FileTypeMap fileTypeMap ;
177
175
176
+ private boolean encodeFilenames = true ;
177
+
178
178
private boolean validateAddresses = false ;
179
179
180
180
@@ -464,7 +464,7 @@ protected FileTypeMap getDefaultFileTypeMap(MimeMessage mimeMessage) {
464
464
* Set the Java Activation Framework {@code FileTypeMap} to use
465
465
* for determining the content type of inline content and attachments
466
466
* 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
468
468
* MimeMessage carries, if any, or the Activation Framework's default
469
469
* {@code FileTypeMap} instance else.
470
470
* @see #addInline
@@ -480,18 +480,40 @@ public void setFileTypeMap(@Nullable FileTypeMap fileTypeMap) {
480
480
481
481
/**
482
482
* Return the {@code FileTypeMap} used by this MimeMessageHelper.
483
+ * @see #setFileTypeMap
483
484
*/
484
485
public FileTypeMap getFileTypeMap () {
485
486
return this .fileTypeMap ;
486
487
}
487
488
488
489
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
+
489
514
/**
490
515
* 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}.
495
517
* @see #validateAddress
496
518
*/
497
519
public void setValidateAddresses (boolean validateAddresses ) {
@@ -500,6 +522,7 @@ public void setValidateAddresses(boolean validateAddresses) {
500
522
501
523
/**
502
524
* Return whether this helper will validate all addresses passed to it.
525
+ * @see #setValidateAddresses
503
526
*/
504
527
public boolean isValidateAddresses () {
505
528
return this .validateAddresses ;
@@ -508,10 +531,8 @@ public boolean isValidateAddresses() {
508
531
/**
509
532
* Validate the given mail address.
510
533
* 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()},
512
535
* 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.
515
536
* @param address the address to validate
516
537
* @throws AddressException if validation failed
517
538
* @see #isValidateAddresses()
@@ -525,7 +546,8 @@ protected void validateAddress(InternetAddress address) throws AddressException
525
546
526
547
/**
527
548
* 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.
529
551
* @param addresses the addresses to validate
530
552
* @throws AddressException if validation failed
531
553
* @see #validateAddress(InternetAddress)
@@ -885,9 +907,7 @@ public void addInline(String contentId, DataSource dataSource) throws MessagingE
885
907
Assert .notNull (dataSource , "DataSource must not be null" );
886
908
MimeBodyPart mimeBodyPart = new MimeBodyPart ();
887
909
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 + ">" );
891
911
mimeBodyPart .setDataHandler (new DataHandler (dataSource ));
892
912
getMimeMultipart ().addBodyPart (mimeBodyPart );
893
913
}
@@ -997,7 +1017,8 @@ public void addAttachment(String attachmentFilename, DataSource dataSource) thro
997
1017
try {
998
1018
MimeBodyPart mimeBodyPart = new MimeBodyPart ();
999
1019
mimeBodyPart .setDisposition (MimeBodyPart .ATTACHMENT );
1000
- mimeBodyPart .setFileName (MimeUtility .encodeText (attachmentFilename ));
1020
+ mimeBodyPart .setFileName (isEncodeFilenames () ?
1021
+ MimeUtility .encodeText (attachmentFilename ) : attachmentFilename );
1001
1022
mimeBodyPart .setDataHandler (new DataHandler (dataSource ));
1002
1023
getRootMimeMultipart ().addBodyPart (mimeBodyPart );
1003
1024
}
0 commit comments