• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1380
  • Last Modified:

Attaching Excel File To Email Results In Empty, Corrupt Attachment

Hello friends -

I've got an application running under Tomcat 5 / jdk1.5 that needs to send me emails with attachments from time to time.   Unfortunately, I cannot get the component to actually attach me an excel file; though for some reason, it does work in jdk1.6.  [Can't get jdk1.6 in my target environment in a meaningful timeframe].

Anyways, I have modified mailcap properties at run time to include the values that I needed in order to overcome the dreaded 'no DHC for handler application/octet-stream' exception, but after that, I do get an email with an attachment, but the attachment is blank (64B) and corrupt (Excel tries to launch, but complains about a bogus file).  This can't be difficult, and yet I'm struggling.  Here is my code:

You can see below my attempts to toggle back and forth between application/octet-stream and application/application/vnd.ms-excel.  No effect.  Am I using the wrong handler?  

                  logger.error("Our inbound attachment is of size: "+ attachment.length);
                  MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
                  mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
                  mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
                  mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
                  mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
                  mc.addMailcap("application/vnd.ms-excel;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
                  mc.addMailcap("application/octet-stream;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
                  mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");

                  // create a message
                  MimeMessage msg = new MimeMessage(session);
                  msg.setFrom(new InternetAddress(sender));
                  InternetAddress[] address = new InternetAddress[toAddress.length];
                  for (int cntr = 0; cntr < toAddress.length; cntr++)
                        address[cntr] = new InternetAddress(toAddress[cntr]);
                  msg.setRecipients(Message.RecipientType.TO, address);

                  // create and fill the first message part
                  MimeBodyPart mbp1 = new MimeBodyPart();

                  // create the second message part
                  MimeBodyPart mbp2 = new MimeBodyPart();

                  // attach the file to the message
                  //mbp2.setDataHandler(new DataHandler(attachment,"application/octet-stream"));
                  mbp2.setDataHandler(new DataHandler(attachment,"application/vnd.ms-excel"));                  

                  // create the Multipart and add its parts to it
                  Multipart mp = new MimeMultipart();

                  // add the Multipart to the message

                  // set the Date: header
                  msg.setSentDate(new Date());

                  // send the message


Any insight is greatly appreciated. Someone please save my sanity.

  • 2
1 Solution
passionlessDroneAuthor Commented:
Hello friends -

Problem solved.  Moved to a FileDataSource and avoided mime type nonsense all together.  Just in case this helps anyone else.  Source of problem is still unknown, but work around is in place!

passionlessDroneAuthor Commented:
Solved the problem with additional hacking.  Always the way!
I had the impression that JavaMail maybe up to some version had some bug which resulted
in producing corrupt attachments when attaching non-text files of substantial size. Perhaps
that was the reason for your problem

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now