diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java index b5e37e3698ab..c9d21201a67f 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java @@ -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 @@ 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. - *

Default is the {@code FileTypeMap} that the underlying + *

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 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. + *

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". - *

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). + *

The default is {@code false}. * @see #validateAddress */ public void setValidateAddresses(boolean validateAddresses) { @@ -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; @@ -508,10 +531,8 @@ public boolean isValidateAddresses() { /** * Validate the given mail address. * Called by all of MimeMessageHelper's address setters and adders. - *

Default implementation invokes {@code InternetAddress.validate()}, + *

The default implementation invokes {@link InternetAddress#validate()}, * provided that address validation is activated for the helper instance. - *

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 @@ protected void validateAddress(InternetAddress address) throws AddressException /** * Validate all given mail addresses. - * Default implementation simply delegates to validateAddress for each address. + *

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 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); } @@ -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); }