Fix NPEs in MimeMessageHelper when adding files without names
Prior to this commit, `MimeMessageHelper` would accept `Resource` instances as inline attachments in multipart MIME messages. If the provided `Resource` implementation returns `null` for `getFileName()`, the `addInLine` method would fail with a `NullPointerException` as Jakarta activation fails to detect the content type for a null filename. This commit falls back on "application/octet-stream" when the filename is not known for the given resource instead of failing with an exception. Fixes gh-33527
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -43,6 +43,7 @@ import org.springframework.core.io.InputStreamSource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* Helper class for populating a {@link jakarta.mail.internet.MimeMessage}.
|
||||
@@ -960,7 +961,8 @@ public class MimeMessageHelper {
|
||||
*/
|
||||
public void addInline(String contentId, Resource resource) throws MessagingException {
|
||||
Assert.notNull(resource, "Resource must not be null");
|
||||
String contentType = getFileTypeMap().getContentType(resource.getFilename());
|
||||
String contentType = (resource.getFilename() != null ?
|
||||
getFileTypeMap().getContentType(resource.getFilename()) : MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE);
|
||||
addInline(contentId, resource, contentType);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user