When we use MimeMessageHelper to add inline images those appears as "noname" attachment in gmail client, but are inlined correctly. inline-with-noname

Even if we define.

            var originalFileName = Objects.requireNonNull(image.getOriginalFilename());
            var contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(originalFileName);
            var byteArrayDataSource = new ByteArrayDataSource(image.getInputStream(), contentType);
            byteArrayDataSource.setName(originalFileName);
            mimeMessageHelper.addInline(originalFileName, byteArrayDataSource);

But it not is an elegant presentation for us.

To solve this we override MimeMessageHelper to add fileName on "public void addInline(String contentId, DataSource dataSource) throws MessagingException"

public class InlineFileNameMimeMessageHelper extends MimeMessageHelper {

public InlineFileNameMimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {
    super(mimeMessage, multipart);
}

public void addInline(String contentId, DataSource dataSource) throws MessagingException {
    Assert.notNull(contentId, "Content ID must not be null");
    Assert.notNull(dataSource, "DataSource must not be null");
    var mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setDisposition(Part.INLINE);
    mimeBodyPart.setContentID("<" + contentId + ">");
    mimeBodyPart.setDataHandler(new DataHandler(dataSource));
    // MimeMessageHelper do not adds filename
    mimeBodyPart.setFileName(dataSource.getName());
    getMimeMultipart().addBodyPart(mimeBodyPart);
}

}

And with this change we have te expected name on preview.

inline-with-name

It is possible to add this or similiar behaviour on future releases?

Thank you.

Comment From: snicoll

Thanks for the suggestion. On surface it looks sensible but I wonder if that wouldn't have unwanted side effects. We don't use the DataSource#name anywhere at the moment.

@jhoeller what do you think?

Comment From: simonbasle

FYI I had a look at the various DataSource#getName Jakarta implementations and they seem to either return an empty String or a significant name (e.g. for FileDataSource, the File#getName()).

The only one I'm wondering about is URLDataSource#getName(), which returns the path (and query) of the URL. So:

dataSource = new URLDataSource(new URL("https://example.org/other/path?with-query=true"));

Would have a name of /other/path?with-query=true 🤔

That said, I would still advocate for an internal discussion on this: do we want the behavior to change or would we rather add a new overload of MimeMessageHelper#addInline?

Comment From: simonbasle

we'll go in the additional overload direction, and add a few overloads of the addInline methods with an additional inlineFilename parameter.