Attaching Excel File To Email Results In Empty, Corrupt Attachment

Posted on 2011-05-11
Last Modified: 2012-05-11
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/  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/;; 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/"));                  

                  // 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.

Question by:passionlessDrone

    Accepted Solution

    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!


    Author Closing Comment

    Solved the problem with additional hacking.  Always the way!
    LVL 47

    Expert Comment

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now