-
-
Notifications
You must be signed in to change notification settings - Fork 275
Description
Summary
When building emails with attachments using emailBuilder.withAttachment(...), SimpleJavaMail generates down the road a Content-ID header based on the attachment name. For filenames that contain characters not allowed by RFC 5322’s msg-id syntax, the resulting Content-ID is invalid.
Details
The Content-ID is currently generated like this:
Content-ID: <attachment-name@random-UUID>
(see MimeMessageHelper::getBodyPartFromDatasource()).
However, the Content-ID field should use the msg-id syntax from RFC 5322, which restricts the allowed characters to letters, digits, and some symbols: ! # $ % & ' * + - / = ? ^ _ { | } ~`
Below is a snippet of an email generated by SimpleJavaMail:
------=_Part_10_854134233.1763546189190
Content-Type: text/plain;
filename="Attachment %^$(()_()&^&^^:@/\\|{}[]#~`- special chars.txt";
name="Attachment %^$(()_()&^&^^:@/\\|{}[]#~`- special chars.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="Attachment %^$(()_()&^&^^:@/\\|{}[]#~`- special chars.txt"
Content-ID: <Attachment %^$(()_()&^&^^:@/\|{}[]#~`- special chars.txt@c4c3733b-786d-45be-ae15-ba78f11246f9>
Attachment with special chars
------=_Part_10_854134233.1763546189190--
Notice how the Content-ID contains multiple characters not permitted by RFC 5322.
Expected result
Process the Content-ID header to replace invalid characters with underscore, or similar. For example, something like this (AI generated):
fileName.replaceAll("[^A-Za-z0-9!#$%&'*+\\-/=?^_`{|}~]", "_");