Skip to content

MimeMessageHelper does not set filename on inline MimeBodyPart #33230

@rasenfer

Description

@rasenfer

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.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions