simple-java-mail icon indicating copy to clipboard operation
simple-java-mail copied to clipboard

Encoding issues with non ascii attachement names.

Open sbellan opened this issue 5 years ago • 5 comments

Using Version 6.4.3

Couple of issues,

  1. When sending an email with non-ascii characters, like this,
    String attachementName = "⏳.txt";
    System.out.println(attachementName);
    Email email = EmailBuilder.startingBlank()
        .to("[email protected]")
        .from("[email protected]")
        .withReplyTo("[email protected]")
        .withSubject("hey")
        .withHTMLText("<img src='cid:wink1'><b>We should meet up!</b><img src='cid:wink2'>")
        .withPlainText("Please view this email in a modern email client!")
        .withAttachment(attachementName, IOUtils.toByteArray(attachmentStream), "text/plain")
        .buildEmail();


the content generated when seen using withTransportModeLoggingOnly looks like the following,

Content-Type: text/plain; filename="�.txt"; name="�.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="�.txt"
Content-ID: <=?UTF-8?B?4o+z?=>

The filename and name are not encoded correctly. https://tools.ietf.org/html/rfc1342, https://tools.ietf.org/html/rfc1341

  1. When parsing a email generated by a Outlook client which has the headers like the following,
...
--_004_9e40f9086684426f9ce55a9caffb3ab1Point72com_
Content-Type: text/html;
	name="=?Windows-1252?B?Q29uZmlndXJlX1NTT19mb3JfQWRtaW5fQ29uc29sZV9BY2Nlc3Nfll9T?=
 =?Windows-1252?Q?ilverfort.pdf.html?="
Content-Description: =?Windows-1252?B?Q29uZmlndXJlX1NTT19mb3JfQWRtaW5fQ29uc29sZV9BY2Nlc3Nfll9T?=
 =?Windows-1252?Q?ilverfort.pdf.html?=
Content-Disposition: attachment;
	filename="=?Windows-1252?B?Q29uZmlndXJlX1NTT19mb3JfQWRtaW5fQ29uc29sZV9BY2Nlc3Nfll9T?=
 =?Windows-1252?Q?ilverfort.pdf.html?="; size=274602;
	creation-date="Tue, 29 Sep 2020 14:18:08 GMT";
	modification-date="Tue, 29 Sep 2020 16:33:58 GMT"
Content-Transfer-Encoding: base64
...

with code like this,


    InputStream emailStream = new FileInputStream("somefile.eml");
    MimeMessage mm = new MimeMessage(null, emailStream);
    Email email = EmailBuilder.copying(mm).buildEmail();

It fails with,


org.simplejavamail.mailer.MailValidationException: Suspected of injection attack, field: email.header.Content-Description with suspicious value: =?Windows-1252?B?Q29uZmlndXJlX1NTT19mb3JfQWRtaW5fQ29uc29sZV9BY2Nlc3Nfll9T?=
 =?Windows-1252?Q?ilverfort.pdf.html?=

	at org.simplejavamail.mailer.MailerHelper.scanForInjectionAttack(MailerHelper.java:139)
	at org.simplejavamail.mailer.MailerHelper.validate(MailerHelper.java:97)
	at org.simplejavamail.mailer.internal.MailerImpl.validate(MailerImpl.java:361)
	at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:339)
	at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:330)

sbellan avatar Oct 01 '20 21:10 sbellan

I'm facing similar problmes. My filename is ignored, the recipient get's an attchament with =UTF-8BRWRlbmhhdXNlcl9LQjAwMDAwMDE2OQ=== =UTF-8BNF9CcmFuZHN0w6R0dGVyIEhhbnMucGRm= as filename?

Content-Type: application/pdf; name="=UTF-8BRWRlbmhhdXNlcl9LQjAwMDAwMDE2OQ=== =UTF-8BNF9CcmFuZHN0w6R0dGVyIEhhbnMucGRm=" Content-Description: =UTF-8BRWRlbmhhdXNlcl9LQjAwMDAwMDE2OQ=== =UTF-8BNF9CcmFuZHN0w6R0dGVyIEhhbnMucGRm= Content-Disposition: attachment; filename="=UTF-8BRWRlbmhhdXNlcl9LQjAwMDAwMDE2OQ=== =UTF-8BNF9CcmFuZHN0w6R0dGVyIEhhbnMucGRm="; size=38133; creation-date="Wed, 25 Nov 2020 20:08:11 GMT";

meeximum avatar Nov 25 '20 20:11 meeximum

@sbellan, could you please provide me with an example email? Even if you don't face this issue anymore or moved on to another library, I would like to analyse the problem and find a solution. Would be much appreciated!

bbottema avatar Feb 13 '21 14:02 bbottema

You can use this to fix it: .withAttachment(MimeUtility.encodeText(filename, "UTF-8", null), FileDataSource(pdf))

dschrul-cf avatar Aug 24 '21 06:08 dschrul-cf

Note that attachment name encoding was turned off deliberately due to #271. Not sure what the best solution is right now...

bbottema avatar Jan 02 '22 21:01 bbottema

What's the latest status here?

bbottema avatar Jul 29 '22 17:07 bbottema

Still waiting for an example. Will close this as non-actionable soon.

bbottema avatar Jan 24 '23 17:01 bbottema

Apologies for not following up. Attaching the email which is failing, (rename .txt to .eml)

vers_quoted_printable.txt

This is the code snippet,

    InputStream emailStream = getClass().getResourceAsStream("/vers_quoted_printable.eml");
    MimeMessage mm = new MimeMessage(null, emailStream);
    Email email = EmailBuilder.copying(mm).buildEmail();

    TransportStrategy.SMTP.setOpportunisticTLS(false);
    Mailer mailer = MailerBuilder
        .withSMTPServer("localhost", 25)
        .withTransportStrategy(TransportStrategy.SMTP)
        .withSessionTimeout(10 * 1000)
        .clearEmailAddressCriteria() // turns off email validation
        .withDebugLogging(true)
        .withTransportModeLoggingOnly(true)
        .buildMailer();

    mailer.sendMail(email);

sbellan avatar Jan 24 '23 18:01 sbellan

Fixed in release 7.8.3, please verify. It was a bit of a pain to figure this one out, thanks again for helping me analyse this!

bbottema avatar Feb 21 '23 00:02 bbottema

Great!!. The fix looks quite involved. Thank you.

sbellan avatar Feb 21 '23 03:02 sbellan