simple-java-mail icon indicating copy to clipboard operation
simple-java-mail copied to clipboard

Prevent Overwriting Custom Content-ID for Attachments

Open 0VP0 opened this issue 11 months ago • 2 comments

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("[email protected]")
    .to("[email protected]")
    .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.

0VP0 avatar Dec 15 '24 07:12 0VP0