Skip to content

Commit

Permalink
Fix NPEs in MimeMessageHelper when adding files without names
Browse files Browse the repository at this point in the history
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
bclozel committed Sep 13, 2024
1 parent 6a20987 commit 412f5f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -43,6 +43,7 @@
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}.
Expand Down Expand Up @@ -960,7 +961,8 @@ public void addInline(String contentId, File file) throws MessagingException {
*/
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 412f5f6

Please sign in to comment.