You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prevent Overwriting Custom Content-ID for Attachments #566
Issue:
When I set a custom Content-ID for attachments, it gets overwritten after building the email using EmailBuilder.buildEmail() and send it .
Steps to Reproduce
Create an email with an attachment using EmailBuilder.startingBlank().
Convert the email to MimeMessage and set a custom Content-ID for the attachment.
Convert it back to an Email using EmailConverter.mimeMessageToEmail(mimeMessage).
Send the email.
The Content-ID is changed after building the email and sending the email.
Expected Behavior
The custom Content-ID should stay the same after building the email and sending the email.
Actual Behavior
The Content-ID I set for the attachment gets overwritten after building the email and sending the email.
Example Code
Emailemail = EmailBuilder.startingBlank()
.from("sender@example.com")
.to("recipient@example.com")
.withSubject("Test Content-ID")
.withAttachment("file.pdf", newFileDataSource("/path/to/file.pdf"))
.buildEmail();
// Convert to MimeMessageMimeMessagemimeMessage = EmailConverter.emailToMimeMessage(email);
MimeMultipartmultipart = (MimeMultipart) mimeMessage.getContent();
// Set the custom Content-IDfor (inti = 0; i < multipart.getCount(); i++) {
BodyPartpart = multipart.getBodyPart(i);
if ("file.pdf".equals(part.getFileName())) {
part.setHeader("Content-ID", "<custom-id-12345>");
}
}
mimeMessage.setContent(multipart);
mimeMessage.saveChanges();
// Convert back to EmailEmailupdatedEmail = EmailConverter.mimeMessageToEmail(mimeMessage);
Request
Prevent Overwriting: I need a way to keep the custom Content-ID I set for attachments. It should not be changed when build the email.
Direct Option (Optional): If possible, I would like to set the Content-ID directly on the attachment without converting to MimeMessage, from the AttachmentResource resource directly.
This preserves caller-provided attachment Content-ID values instead of replacing them with generated fallback IDs during MIME -> Email -> MIME round trips. AttachmentResource now carries optional contentId metadata, and EmailConverter treats custom CIDs as user-provided while filtering only Simple Java Mail generated transport IDs.
Activity
dmittriy13 commentedon Feb 6, 2025
We also need this function. @bbottema, please tell me the approximate release date for a new version with this improvement?
0VP0 commentedon Feb 10, 2025
@dmittriy13
I created a PR for this feature, #570
bbottema commentedon Jul 3, 2026
Fixed in
56fff10b(feature/recipient-builder).This preserves caller-provided attachment
Content-IDvalues instead of replacing them with generated fallback IDs during MIME ->Email-> MIME round trips.AttachmentResourcenow carries optionalcontentIdmetadata, andEmailConvertertreats custom CIDs as user-provided while filtering only Simple Java Mail generated transport IDs.Regression coverage:
EmailConverterTest#testGithub566_AttachmentContentIdSurvivesRoundtrip.Verified under Java 8 with
mvn -pl modules/simple-java-mail -am test(303 run, 0 failures/errors, 12 skipped). Closing as completed.bbottema commentedon Jul 8, 2026
Released in Simple Java Mail v9.0.0, which is now available from Maven Central:
https://repo1.maven.org/maven2/org/simplejavamail/simple-java-mail/9.0.0/
Release notes:
https://github.com/bbottema/simple-java-mail/releases/tag/9.0.0