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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.core.io.Resource;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.MimeTypeUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for populating a {@link jakarta.mail.internet.MimeMessage}.
|
* 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 {
|
public void addInline(String contentId, Resource resource) throws MessagingException {
|
||||||
Assert.notNull(resource, "Resource must not be null");
|
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);
|
addInline(contentId, resource, contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import jakarta.mail.internet.InternetAddress;
|
|||||||
import jakarta.mail.internet.MimeMessage;
|
import jakarta.mail.internet.MimeMessage;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
import org.springframework.mail.MailParseException;
|
import org.springframework.mail.MailParseException;
|
||||||
import org.springframework.mail.MailSendException;
|
import org.springframework.mail.MailSendException;
|
||||||
import org.springframework.mail.SimpleMailMessage;
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
@@ -270,6 +271,25 @@ class JavaMailSenderTests {
|
|||||||
assertThat(sender.transport.getSentMessages()).containsExactly(message.getMimeMessage());
|
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("you@mail.org");
|
||||||
|
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
|
@Test
|
||||||
void javaMailSenderWithParseExceptionOnSimpleMessage() {
|
void javaMailSenderWithParseExceptionOnSimpleMessage() {
|
||||||
SimpleMailMessage simpleMessage = new SimpleMailMessage();
|
SimpleMailMessage simpleMessage = new SimpleMailMessage();
|
||||||
|
|||||||
Reference in New Issue
Block a user