I have a requirement that needs me to write a List-Unsubscribe header to an outgoing email. I am using the MimeMessage from Jakarta. The value for List-Unsubscribe header has a potential to be very long (more than 1100 characters). According to RFC5322, it's recommended to have each line's length not more than 76 characters.
I am trying to fold the long header value according to the RFC5322
To achieve this, I tried using the MimeUtility's fold method to fold it.
Before I use the fold method, I add a space at every 76th character so that the fold method can fold it correctly.
String replacedString = sourceURL.replaceAll("(.{76})", "$0 "); return MimeUtility.fold(replacedString.length(), replacedString);
The output is right. It does get folded. However, when I send this mail out to a Gmail address, the source of the email does have the header, but the value of the header gets truncated - Due to which the unsubscribe option isn't shown on the client. Is there something wrong in the approach? Is there a better approach?
PS: The link for the List-Unsubscribe header is an HTTP one and not a mailto
Example
String listUnsubHeaderValue = "<https://www.ourcustomisedtrackingandredirectiondomain.com/20221014/track/unsubscribe?R=https%3A%2F%2Fourcustomisedtrackingandredirectiondomain.com%2F20221014%2Ftrack&T=b2NpZDEudGVuYW5jeS5vYzEuLmFhYWFhYWFhcGdxajVmamVrdTUycW9neXdwNGFnc2hkc2Nnb2c0Z2FldWs1dXhmY2N5eDVmb2ZwZzJvYQ==&F=dXNlckB0ZXN0ZG9tYWluNS5lbWFpbC5hcC1zeWRuZXktMS5vY2kub2MtdGVzdC5jb20=&D=c3JpbmlkaGkuay5tdXJ0aHlAZ21haWwuY29t&X=eyJ4LWdyb3VwLWlkIjoie1widHJpZ2dlcl9pZFwiOlwiOWZjNjRkMzctNjU2Ni00ZDhiLTliNGEtMDE4YmM3YzViNTliXCIsXCJjb21tdW5pY2F0aW9uX2lkXCI6XCIxMjM3MDE4ZC1jYWZkLTRiNGYtOTEwMy02NTYwN2QwODFjNjZcIixcInJlY2lwaWVudF9pZFwiOlwiODMzYTE2YzgtYjBlZC00ZTMzLWE5MzItNjI0ZGJjZGZiNzk3XCIsXCJwcm9kdWN0XCI6XCJIRVIzXCIsXCJyZWNpcGllbnRfdHlwZVwiOlwiU1RBTkRBUkRcIixcInBpcGVsaW5lX2tleVwiOlwiYTI1YmU4M2EtNzZlZS00NTg1LTlkMjctZTBlOTc4NjE3NTc2XCJ9IiwieC1jYW1wYWlnbi1pZCI6ImxvY2FsIiwieC1ub3RpZmljYXRpb24taWQiOiJIT01FX0VORVJHWV9WMyIsIlgtUHJvZHVjdC1JRCI6IlVUSUwifQ==&I=PDk4OTQ0NzYwNy4wLjE2ODc5NjEyMjcxNjBAWzE5Mi4xNjguMC4xNTZdPg==&V=1&C=1687961239075&S=xrMoCiRNI5Y1Lcwow0Lh74Yk7EB2-4Rlg0qwzubjL-I%3D>"
emailMimeMessage.setHeader(
MimeConstants.LIST_UNSUBSCRIBE_HEADER,
getfoldedTrackingURL(listUnsubHeaderValue));
private String getfoldedTrackingURL(String sourceURL) {
String replacedString = sourceURL.replaceAll("(.{76})", "$0 ");
return MimeUtility.fold(replacedString.length(), replacedString);
}
So, the above code does set the header fine. When I check the emailMimeMessage outputstream, I am able to see this header value correctly folded. But it gets truncated on the Gmail source - which also doesn't enable the "Unsubscribe" link.