Skip to content

Prevent Overwriting Custom Content-ID for Attachments #566

Closed

Description

@0VP0

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

  1. Create an email with an attachment using EmailBuilder.startingBlank().
  2. Convert the email to MimeMessage and set a custom Content-ID for the attachment.
  3. Convert it back to an Email using EmailConverter.mimeMessageToEmail(mimeMessage).
  4. Send the email.
  5. 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

Email email = EmailBuilder.startingBlank()
    .from("sender@example.com")
    .to("recipient@example.com")
    .withSubject("Test Content-ID")
    .withAttachment("file.pdf", new FileDataSource("/path/to/file.pdf"))
    .buildEmail();

// Convert to MimeMessage
MimeMessage mimeMessage = EmailConverter.emailToMimeMessage(email);
MimeMultipart multipart = (MimeMultipart) mimeMessage.getContent();

// Set the custom Content-ID
for (int i = 0; i < multipart.getCount(); i++) {
    BodyPart part = multipart.getBodyPart(i);
    if ("file.pdf".equals(part.getFileName())) {
        part.setHeader("Content-ID", "<custom-id-12345>");
    }
}

mimeMessage.setContent(multipart);
mimeMessage.saveChanges();

// Convert back to Email
Email updatedEmail = EmailConverter.mimeMessageToEmail(mimeMessage);

Request

  1. 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.
  2. 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.

Activity

  1. dmittriy13 commented on Feb 6, 2025

    @dmittriy13

    We also need this function. @bbottema, please tell me the approximate release date for a new version with this improvement?

  2. 0VP0 commented on Feb 10, 2025

    @0VP0
    Author

    @dmittriy13
    I created a PR for this feature, #570

  3. bbottema commented on Jul 3, 2026

    @bbottema
    Owner

    Fixed in 56fff10b (feature/recipient-builder).

    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.

    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.

  4. added this to the 8.13.0 milestone on Jul 3, 2026
  5. bbottema commented on Jul 8, 2026

    @bbottema
    Owner

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions