Attachment names are not always parsed properly from MimeMessage
jkxyx205 opened this issue · 5 comments
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,
- When I send email with attachment
a.txt
EmailBuilder.startingBlank()
.withAttachment("a.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"))- but get attachent name
a.txt.txtwhich 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.
I can get real file name by the code below:
email.getAttachments().get(0).getDataSource().getName();My original purpose is to get a mail from inbox, and then use it's attachment to create new mail to send, the file name not right.
full code blow:
@Test
public void forward() throws MessagingException {
Session session = ServerConfigUtils.getSession();
Store store = session.getStore("imap");
store.connect(Constants.imap_host, Constants.from, Constants.password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message message = folder.getMessage(2);
// get mail from inbox
Email oldEmail = EmailConverter.mimeMessageToEmail((MimeMessage) message);
// get use old mail content
Email newEmail = EmailBuilder.startingBlank()
.from("Rick", Constants.from)
.to("Ashley", Constants.email)
.withSubject("转发:this is subject")
// from old mail
.withEmbeddedImages(oldEmail.getEmbeddedImages())
.withAttachments(oldEmail.getAttachments())
.withHTMLText(oldEmail.getHTMLText())
.prependTextHTML("this is my own message")
.buildEmail();
Mailer mailer = MailerBuilder
.withTransportStrategy(TransportStrategy.SMTP)
.withSMTPServer(Constants.host, 25, Constants.from, Constants.password)
.buildMailer();
mailer.sendMail(newEmail);
}This seems like a bug to me. Looking into it now.
I do have a junit test that checks this functionality (MailerTest.testParser()), so let me check if I'm missing something there.
Ahh, here's what I found in my test code:
ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
namedAttachment.setName("dresscode.txt"); // normally not needed, but otherwise the equals will fail
If I remove that second line, the test fails and this is the comparison result:

So exactly the same as your situation.
The reason I made this exception in the test is because when creating an attachment with Simple Java Mail, you can override the attachment name, but when parsing a MimeMessage, I cannot determine if this happened, this information is lost. To work around that, in the test I override the attachment name. However, this also hid a bug you discovered.
Thanks for reporting this. I'm fixing it as we speak.