WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Invalid Content-ID generated for attachments with special characters in filename #607

@lburja

Description

@lburja

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!#$%&'*+\\-/=?^_`{|}~]", "_");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions