bbottema/simple-java-mail

Feature: Expand email builder API to support Content-Description on attachments

CrisVnait opened this issue · 12 comments

Hello,

I want to send an email with an attachment. The attachment should contain as meta information a Content-Description. But my Content-Description header is ignored:

MimeMessage: Date: Wed, 13 Jul 2022 12:40:46 +0200 (CEST)
From: xxxx
Reply-To: xxxx
To: "xxxx"

Message-ID: 747447859.1.1657708846687@xxxxx
Subject: TEAU0_eb24af60-51f3-45e7-ac7b-2abcc7cdba10
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_1827369718.1657708846523"
X-KIM-Dienstkennung: eAU;Lieferung;V1.0
Disposition-Notification-To: xxxxxx
Return-Receipt-To: xxxxxx

------=_Part_0_1827369718.1657708846523
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

eAU
------=_Part_0_1827369718.1657708846523
Content-Type: application/octet-stream; filename=TEAU0_eb24af60-51f3-45e7-ac7b-2abcc7cdba10.p7s;
name=TEAU0_eb24af60-51f3-45e7-ac7b-2abcc7cdba10.p7s
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=TEAU0_eb24af60-51f3-45e7-ac7b-2abcc7cdba10.p7s
Content-ID: <TEAU0_eb24af60-51f3-45e7-ac7b-2abcc7cdba10.p7s>

Here at the end for 'eAU'-section the 'Content-Description'-element should be added.

My code:

List<AttachmentResource> attachmentResources = new ArrayList<>();

InternetHeaders headers = new InternetHeaders();
        headers.addHeader("Content-Description", storno ? "eAUStorno" : "eAU");
        headers.addHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);

MimeBodyPart mimeBodyPart = new MimeBodyPart(headers, xml, xml.length);

attachmentResources.add(new AttachmentResource(generateFilename(uuid, storno), mimeBodyPart.getDataHandler().getDataSource()));

Email email = EmailBuilder.startingBlank()
                    .withHeader(KimHeader.getDienstKennungHeaderName(), kimDienst.getEmailDienstKennungHeader())
                    .withReturnReceiptTo(from)
                    .withDispositionNotificationTo(from)
                    .withAttachments(attachmentResources)
                    .from(from)
                    .to(toName, toAdress)
                    .withSubject(subject)
                    .withPlainText(emailTextInhalt)
                    .buildEmail();

mailer.sendMail(email);

The 'Content-Type'-header is being processed, but the 'Content-Description' is ignored.
Version is 6.7.6

Any ideas how to solve my problem?
Thanks a lot for help!

Why are you using a MimeBodyPart to add attachments??

Why are you using a MimeBodyPart to add attachments??

@bbottema
There is a setter provided to set description. But it has same behavior like setting with InternetHeader. So it's not required.
mimeBodyPart.setDescription("eAU");
What would you suggest to use instead?

Simple Java Mail indeed does a conversion and only supports selected headers for attachments. Content-Description is not one of them (yet). I'm just wonder what you use this for, actually. I think this is the first time I see this header being used (on an attachment anyway). As far as I know it doesn't do anything, except for providing the given text in the resulting EML source, which is generally never inspected directly except for debugging purposes (and then still you have the attachment's name).

Is Content-Description value extracted and used somewhere in your process?

@bbottema Thanks for your detailed answer.
It is a global requirement from external to set Content-Description to a specific value. Otherwise the attachment is not accepted.
So unfortunately we have to switch the library, because I do not think we can expect a new version in the next days.
Thanks for your help!

So unfortunately we have to switch the library, because I do not think we can expect a new version in the next days.

And you are basing this conclusion on what grounds exactly?

That we can not expect a new version? I'm not used to getting help so quickly.
So could we expect? :) It would save much effort for us.

Well I wanted to look into it this morning, but then someone decided to drive into my car on the road so I had to deal with that. I hope I can take a look soon, though.

Oh, that's annoying. Thank you!

Would this work for you?

String name = generateFilename(uuid, storno);
String description = storno ? "eAUStorno" : "eAU";

yourEmailBuilder
    (..)
    .withAttachment(name, new ByteArrayDataSource(xml, "text/plain"), description)
    // or
    .withAttachment(name, xml.getBytes(defaultCharset()), "text/plain", description)
    (..)
    .buildEmail();

Does it mean that AttachmentResource gets a field 'description', which could be initalized with constructor/setter?
This would be perfect yes!
The number of attachments could be differ in different use-cases, so we want to set attachments as list with

yourEmailBuilder
    (..)
    .withAttachments(attachmentResourceList)
    (..)
    .buildEmail();

Thanks for your effort!

Released in 7.3.0! And yes, you can create AttachmentResources directly with descriptions too.

Thank you very much for the quick help and reactions!
Unfortunately it seems, that we can't migrate to version 7.3.0 with Jakarta 2.0.1, because we use Spring Boot 2.x.x (see https://stackoverflow.com/questions/68955884/update-jakarta-mail-1-6-5-to-2-0-1).
Would it be possible, that you also provide the 'content description'-feature in version 6.x.x?