附件文件名显示为" mime"在电子邮件中

时间:2016-09-15 13:48:15

标签: javamail limit filenames email-attachments

当我的附件有长文件名时,电子邮件中的文件名显示为" mime"而不是实际的文件名。附件的文件名可以有多长时间?或者这个问题是由其他原因造成的?

以下是代码:

java.util.Properties properties=System.getProperties();
properties.put("mail.smtp.host",smtpHost);
Session session=Session.getDefaultInstance(properties, null);

MimeMessage message = new MimeMessage(session);
Address fromAddress=new InternetAddress(from);
message.setFrom(fromAddress);
Address[] toAddresses=InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO, toAddresses);
Address[] ccAddresses=InternetAddress.parse(cc);
message.setRecipients(Message.RecipientType.CC, ccAddresses);
Address[] bccAddresses=InternetAddress.parse(bcc);
message.setRecipients(Message.RecipientType.BCC, bccAddresses);
message.setSubject(subject);                       

//Start - Send HTML Message                    
MimeBodyPart mbpa2 = new MimeBodyPart();
mbpa2.setText(body);
mbpa2.addHeaderLine("Content-Type: text/html; charset=\"iso-8859-1\"");
mbpa2.addHeaderLine("Content-Transfer-Encoding: quoted-printable");                                                             
Multipart mp2 = new MimeMultipart("alternative");                    
mp2.addBodyPart(mbpa2);                    


// attach the files to the message
if ( attachments != null && attachments.length > 0 ) 
{
    for (String filename:attachments) 
    { 
        FileDataSource fds = new FileDataSource(filename);
        MimeBodyPart mbp3 = new MimeBodyPart();
        mbp3.setDataHandler(new DataHandler(fds));
        mbp3.setFileName(fds.getName());                            
        // attach the file to the message
        mp2.addBodyPart(mbp3);                                                        
    }
} 
// End of file attachment

message.setContent(mp2);                       
//End - Send HTML Message

Transport transport=session.getTransport("smtp");   
transport.connect(smtpHost, "", "");
transport.sendMessage(message, message.getAllRecipients());
transport.close();

1 个答案:

答案 0 :(得分:0)

没有限制,但是长文件名的编码方式会有所不同,如果你有一个旧邮件,它可能无法理解编码,而是用一些通用名称代替。您可以将系统属性mail.mime.encodeparameters设置为false以禁用此编码。