greenmail icon indicating copy to clipboard operation
greenmail copied to clipboard

Add a test for an attachment with UTF-8 symbols

Open boris-petrov opened this issue 3 years ago • 14 comments

... which fails with:

jakarta.mail.MessagingException: Unable to load BODYSTRUCTURE
	at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1540)
	at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:784)
	at jakarta.mail.internet.MimeMessage.getContent(MimeMessage.java:1484)
	at com.icegreen.greenmail.test.specificmessages.EncodingTest.testAttachmentWithUTF8NameAndGreenMailApi(EncodingTest.java:231)
        ...

That's because of MimeUtility.encodeText. If I remove that method call, the test passes. Not sure what's going on but I believe this is a bug in Greenmail.

cc @marcelmay

boris-petrov avatar Sep 19 '22 13:09 boris-petrov

@boris-petrov , thx alot! I will investigate the issue(s) next week

marcelmay avatar Sep 19 '22 20:09 marcelmay

Getting the same issue when trying to read emails from Greenmail using Apache Camel.

Mulgish avatar Oct 27 '22 11:10 Mulgish

Thanks alot, @boris-petrov and @Mulgish .

I created a separate issue #498 (and for 1.6.x backport #499 ).

Looks like something breaks (CRLF in header Content-Disposition) when creating/sending the mail, and I added a workaround for it.

marcelmay avatar Nov 05 '22 13:11 marcelmay

@marcelmay thanks for the new release! It fixed the test that I had written here. However, I pushed another commit which just adds a check for the attachment's file name. And it fails. Please check it out. I believe it should be passing?

boris-petrov avatar Dec 28 '22 09:12 boris-petrov

Thanks again @boris-petrov for another failing example. Will investigate the issue.

marcelmay avatar Dec 31 '22 15:12 marcelmay

Use MimeUtility.decodeText(....getFileName()) when fetching the name, or set mail.mime.decodefilename property to true.

marcelmay avatar Feb 12 '23 19:02 marcelmay

@marcelmay MimeUtility.decodeText of course would fix it. As for mail.mime.decodefilename - that's what I have in my own code but something like this fails there. How do I set mail.mime.decodefilename in this test here to prove or disprove that?

boris-petrov avatar Feb 13 '23 14:02 boris-petrov

@boris-petrov , just added #541 which simplifies adding such custom session properties like 'mail.mime.decodefilename' by default. Will be available with versions 1.6.14 / 2.0.x .

Otherwise you'd have to do something like this:

        Session smtpSession = greenMail.getSmtp().createSession();
        smtpSession.getProperties().setProperty("mail.mime.encodefilename","true");
        MimeMessage mimeMessage = new MimeMessage(smtpSession);
        ...
        Session imapSession = greenMail.getImap().createSession();
        imapSession.getProperties().setProperty("mail.mime.decodefilename","true");
        ... // Receive mail using IMAP session

marcelmay avatar Feb 25 '23 17:02 marcelmay

@boris-petrov , I believe this PR was accidentally re-opened by your force-push (see history above, search for force-pushed) => I would close this PR, as it was merged before.

marcelmay avatar Feb 25 '23 17:02 marcelmay

@boris-petrov , I believe this PR was accidentally re-opened by your force-push (see history above, search for force-pushed) => I would close this PR, as it was merged before.

I believe this comment was meant for some other PR? This hasn't been merged.

@boris-petrov , just added #541 which simplifies adding such custom session properties like 'mail.mime.decodefilename' by default. Will be available with versions 1.6.14 / 2.0.x .

That's great! How do I use that in this test? I want to set that option (mail.mime.decodefilename) for the IMAP connection in this test and then it should pass (or fail which would mean there's an issue in Greenmail).

P.S. I rebased on master.

boris-petrov avatar Feb 27 '23 09:02 boris-petrov

@boris-petrov , you can set the properties for the whole test via

    @Rule
    public GreenMailRule greenMail = new GreenMailRule(new ServerSetup[]{
        ServerSetupTest.SMTP.mailSessionProperty("mail.mime.encodefilename", "true"),
        ServerSetupTest.IMAP.mailSessionProperty("mail.mime.decodefilename", "true")
    });

... or in the test itself via

    public void testAttachmentWithUTF8NameAndGreenMailApi() throws MessagingException, IOException {
        greenMail.setUser("to@localhost", "pwd");
        final Session imapSession =  greenMail.getImap().createSession();
        imapSession.getProperties().setProperty("mail.mime.decodefilename","true"); // For reading
        final IMAPStore store = (IMAPStore) imapSession.getStore();
....
                    GreenMailUtil.sendAttachmentEmail(
                        "to@localhost", "from@localhost", "subject", "body",
                        new byte[]{0, 1, 2}, "image/gif", fileName,
                        "testimage_description",
                        greenMail.getSmtp().getServerSetup()
                            .mailSessionProperty("mail.mime.encodefilename", "true")); // For sending
....

marcelmay avatar Feb 27 '23 21:02 marcelmay

@marcelmay thanks for the help and sorry for the delay! I pushed another commit that does what you suggest - it sets mail.mime.decodefilename. So I would expect the value that I read back to be decoded but it isn't - that is, the test still fails. Please take a look when you have the time. Thanks!

boris-petrov avatar Mar 07 '23 08:03 boris-petrov

@boris-petrov ,

my fault as I assumed it is a session property.

This property is actually a JavaMail system property, not a JavaMail session property:

  System.setProperty("mail.mime.decodefilename", "true");

marcelmay avatar Mar 07 '23 22:03 marcelmay

OK, like that? Check the last commit. The test still doesn't work unfortunately. Am I missing something else?

boris-petrov avatar Mar 08 '23 08:03 boris-petrov