When my attached files have long file names, the filename in the email appears as "mime" instead of the actual file name. Is there a limit to how long the attachment's file name can be? Or is this issue caused by something else?
Here's the code:
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();
There's no limit, but long file names will be encoded differently and if you have an old mailer it may not understand that encoding, substituting some general name instead. You can set the System property mail.mime.encodeparameters
to false to disable this encoding.