bbottema/simple-java-mail

Attachment (file)names with special characters should not be encoded

barkep opened this issue · 5 comments

When adding an attachment:
Testowy_załącznik_ąśćńółęĄŻŹĆŃÓŁĘ.txt
to an email and positive delivery, the recipient receives the attachment with the changed name:
=UTF-8_Q_Testowy=5Fza=C5=82=C4=85cznik=5F= =UTF-8_Q=C4=85=C5=9B=C4=87=C5=84=C3=B3=C5=82=C4=99=C4=84=C5=BB_= =UTF-8_Q=C5=B9=C4=86=C5=83=C3=93=C5=81=C4=98.txt_=.txt

After a small investigation, I find that the reason for this is using
MimeUtility.encodeText (name, UTF_8.name (), "B")
for the name of the attachment. I suggest using one of two solutions:

  • adding a parameter that allows you to skip this action for attachments
  • skipping this action for attachments

This is valid encoding according to the RFC, but the client seems to parse it incorrectly. In what email client does the recipient view this result?

The attachment has an incorrect name directly, for example gmail.com.
The characters in the file name are important.

Sample code reproducing the error.

        String host = "smtp.gmail.com";
        int port = 587;
        String name = "";
        String password = "";
        TransportStrategy transportStrategy = TransportStrategy.SMTP_TLS;

        String path = "E://Testowy_załącznik_ąśćńółęĄŻŹĆŃÓŁĘ.txt";
        File file = new File(path);
        DataSource ds = new FileDataSource(file);

        Email email = EmailBuilder
                .startingBlank()
                .from("")
                .to("")
                .withSubject("test")
                .withAttachment("Testowy_załącznik_ąśćńółęĄŻŹĆŃÓŁĘ.txt", ds)
                .buildEmail();
        Mailer mailer = MailerBuilder
                .withSMTPServer(host, port, name, password)
                .withTransportStrategy(transportStrategy)
                .buildMailer();

        mailer.sendMail(email);

Also see: https://javaee.github.io/javamail/FAQ#encodefilename

I'm going to turn off encoding of filenames, while making sure file names are tested for injection attacks.

Fix released in 6.3.2

@bbottema
Hi!
Thanks for this fix, was waiting for this update.
I have a related question regarding EmailConverter.
I wanted to check in test attachment file names, and was using method mimeMessageToEmail.
It was not working, I checked the call chain, and it seems that method buildEmailFromMimeMessage takes ContentId as attachment file name instead of actual not-encoded filename.
Is that a bug, or maybe I am not using EmailConverter correctly?