Attachement's name encoding fails

柔情痞子 提交于 2021-02-07 04:00:47

问题


I try to send an email with an attachment (A pdf file), but the receiver receives a file with a different name and without the .pdf ending. The name of the file is in Greek..

try {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress("from@mail.com"));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail));
    message.setSubject(title,"utf-8");

    // Create the message part
    BodyPart messageBodyPart = new MimeBodyPart();

    // Now set the actual message
    messageBodyPart.setText("This is message body");

    // Create a multipar message
    Multipart multipart = new MimeMultipart();

    // Set text message part
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();

    String filename = "file.pdf";
    String f = name + " Πρόγραμμα Ιανουάριος 2016.pdf";  // the desired name of the file
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(MimeUtility.encodeText(f, "UTF-8", null));
    multipart.addBodyPart(messageBodyPart);

    // Send the complete message parts
    message.setContent(multipart);

    Transport.send(message);

    System.out.println("Mail " + mail +" sent");
} catch (MessagingException e) {
    throw new RuntimeException(e);
}

the name is a string variable and is getting a value previously. The strange is that if I have String f = name + " αααα.pdf" the receiver is getting a pdf succesfully with the name Ρουβάς αααα.pdf but if i have this String f = name + " Πρόγραμμα Ιανουάριος 2016.pdf"; he doesn't. He is getting sth like =_UTF-8_B_zpzOtc Dz4POsc67zrHPgiDOmc6xzr3Ov8 FzqzPgc65zr_Pgi___ ___filename_1=__5wZGY=_=

I added the message.writeTo(System.out); and I got:

MIME-Version: 1.0
Content-Type: multipart/mixed; 
    bou

    ndary="----=_Part_0_1825884453.1457025565509"

    ------=_Part_0_1825884453.1457025565509
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit

    This is message body
    ------=_Part_0_1825884453.1457025565509
    Content-Type: application/octet-stream; 
        name*0="=?UTF-8?B?zpzOtc+Dz4POsc67zrHPgiDOmc6xzr3Ov8+FzrHPgc6vzr/Pgi"; 
        name*1="Ay?=
     =?UTF-8?B?MDE2zpnOsc69zr/Phc6sz4HOuc6/z4IgMjAxNi5wZGY=?"; 
        name*2="="
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; 
        filename*0="=?UTF-8?B?zpzOtc+Dz4POsc67zrHPgiDOmc6xzr3Ov8+FzrHPgc6vzr/Pgi"; 
        filename*1="Ay?=
     =?UTF-8?B?MDE2zpnOsc69zr/Phc6sz4HOuc6/z4IgMjAxNi5wZGY=?"; 
        filename*2="="

with props.setProperty("mail.mime.encodeparameters", "false"); or true

MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_0_797681969.1457074816557"

------=_Part_0_797681969.1457074816557
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is message body
------=_Part_0_797681969.1457074816557
Content-Type: application/octet-stream; name="?????????? 2016.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; 
    filename*=Cp1252''%3F%3F%3F%3F%3F%3F%3F%3F%3F%3F%202016.pdf

回答1:


Because you're encoding the filename yourself, you're using the non-standard MIME encoding format, as described in the JavaMail FAQ. That non-standard encoded text is then being split into multiple parameters using the standard RFC 2231 technique. It's this mix of non-standard and standard format that's probably causing the confusion for the mail reader.

Try letting JavaMail do the encoding for you by removing the call to MimeUtility.encodeText. If that doesn't work, set the System property mail.mime.encodeparameters to false to disable the RFC 2231 encoding.



来源:https://stackoverflow.com/questions/35673240/attachements-name-encoding-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!