0
當我附加的文件具有長文件名時,電子郵件中的文件名顯示爲「mime」而不是實際的文件名。附件的文件名可以有多長時間有限制?還是這個問題是由別的東西引起的?附件文件名在電子郵件中顯示爲「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();