-
Notifications
You must be signed in to change notification settings - Fork 38.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
24 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
import jakarta.mail.internet.MimeMessage; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.io.ByteArrayResource; | ||
import org.springframework.mail.MailParseException; | ||
import org.springframework.mail.MailSendException; | ||
import org.springframework.mail.SimpleMailMessage; | ||
|
@@ -270,6 +271,25 @@ void javaMailSenderWithMimeMessageHelperAndDefaultEncoding() throws Exception { | |
assertThat(sender.transport.getSentMessages()).containsExactly(message.getMimeMessage()); | ||
} | ||
|
||
@Test | ||
void javaMailSenderWithMimeMessageHelperAndCustomResource() throws Exception { | ||
sender.setHost("host"); | ||
sender.setUsername("username"); | ||
sender.setPassword("password"); | ||
|
||
MimeMessageHelper message = new MimeMessageHelper(sender.createMimeMessage(), true); | ||
message.setTo("[email protected]"); | ||
message.addInline("id", new ByteArrayResource(new byte[] {1, 2, 3})); | ||
|
||
sender.send(message.getMimeMessage()); | ||
|
||
assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); | ||
assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); | ||
assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); | ||
assertThat(sender.transport.isCloseCalled()).isTrue(); | ||
assertThat(sender.transport.getSentMessages()).containsExactly(message.getMimeMessage()); | ||
} | ||
|
||
@Test | ||
void javaMailSenderWithParseExceptionOnSimpleMessage() { | ||
SimpleMailMessage simpleMessage = new SimpleMailMessage(); | ||
|