You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Attachment names are not always parsed properly from MimeMessage #175
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", newByteArrayDataSource("Black Tie Optional", "text/plain"))
but get attachent name a.txt.txt which is not expect.
Emailemail = EmailConverter.mimeMessageToEmail(message);
if (email.getAttachments().size() > 0) {
System.out.println(email.getAttachments().get(0).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:
@Testpublicvoidforward() throwsMessagingException {
Sessionsession = ServerConfigUtils.getSession();
Storestore = session.getStore("imap");
store.connect(Constants.imap_host, Constants.from, Constants.password);
Folderfolder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Messagemessage = folder.getMessage(2);
// get mail from inboxEmailoldEmail = EmailConverter.mimeMessageToEmail((MimeMessage) message);
// get use old mail contentEmailnewEmail = 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();
Mailermailer = MailerBuilder
.withTransportStrategy(TransportStrategy.SMTP)
.withSMTPServer(Constants.host, 25, Constants.from, Constants.password)
.buildMailer();
mailer.sendMail(newEmail);
}
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.
Activity
jkxyx205 commentedon Oct 26, 2018
I can get real file name by the code below:
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:
bbottema commentedon Oct 27, 2018
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.
bbottema commentedon Oct 27, 2018
Ahh, here's what I found in my test code:
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.
#175: Fixed how attachment names are read back to account for possibl…
[-]Get wrong attachment name[/-][+]Attachment names are not always parsed properly from MimeMessage[/+]bbottema commentedon Oct 27, 2018
Fix released under 5.0.7. Please verify, @jkxyx205.
jkxyx205 commentedon Oct 28, 2018
The bug has been fixed, thank you very much for high efficient modification. @bbottema