Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachment names are not always parsed properly from MimeMessage #175

Closed
jkxyx205 opened this issue Oct 26, 2018 · 5 comments
Closed

Attachment names are not always parsed properly from MimeMessage #175

jkxyx205 opened this issue Oct 26, 2018 · 5 comments
Assignees
Milestone

Comments

@jkxyx205
Copy link

jkxyx205 commented Oct 26, 2018

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.

@jkxyx205
Copy link
Author

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);

}

@bbottema
Copy link
Owner

bbottema commented 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
Copy link
Owner

bbottema commented Oct 27, 2018

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:
image
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.

bbottema added a commit that referenced this issue Oct 27, 2018
…e <> wrapping. Also, fixed how the tests deal with these names and made it more clear how the attachment name overrides affect conversion results
@bbottema bbottema changed the title Get wrong attachment name Attachment names are not always parsed properly from MimeMessage Oct 27, 2018
@bbottema bbottema added this to the 5.0.7 milestone Oct 27, 2018
@bbottema
Copy link
Owner

bbottema commented Oct 27, 2018

Fix released under 5.0.7. Please verify, @jkxyx205.

@jkxyx205
Copy link
Author

The bug has been fixed, thank you very much for high efficient modification. @bbottema

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants