Attachment names are not always parsed properly from MimeMessage

Repo maintainer's note: Summarizing, the issue is that resource names that are wrapped inside <..> are not read back properly, resulting in double extensions.


Hi Benny Bottema,

  1. When I send email with attachment a.txt
EmailBuilder.startingBlank()
 .withAttachment("a.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"))
  1. but get attachent name a.txt.txt which is not expect.
Email email = EmailConverter.mimeMessageToEmail(message);
if (email.getAttachments().size() > 0) {
                System.out.println(email.getAttachments().get(0).getName());
            }

When I trace source code,I found

  • send email . "Content-ID" default <a.txt>
attachmentPart.setHeader("Content-ID", format("<%s>", resourceName));
  • get attachment name
        @Nonnull
	private static String parseResourceName(@Nullable final String contentID, @Nonnull final String fileName) {
		String extension = "";
		if (!valueNullOrEmpty(fileName) && fileName.contains(".")) {
			extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		}
		if (!valueNullOrEmpty(contentID)) { 
                        // execute here
			return (contentID.endsWith(extension)) ? contentID : contentID + extension;
		} else {
			return fileName;
		}
	}

How can i get the right file name 'a.txt'. Look forward to your relay, thank you.