Link to home
Start Free TrialLog in
Avatar of passionlessDrone
passionlessDrone

asked on

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("*********************************");
                  logger.error("Our inbound attachment is of size: "+ attachment.length);
                  logger.error("*********************************");
                  
                  
                  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");
                  CommandMap.setDefaultCommandMap(mc);

                  
                  
                  // 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);
                  msg.setSubject(subject);

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

                  // 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"));                  
                  mbp2.setFileName(attachmentName);

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

                  // add the Multipart to the message
                  msg.setContent(mp);

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

                  // send the message
                  Transport.send(msg);

*****************************************************

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

brian
ASKER CERTIFIED SOLUTION
Avatar of passionlessDrone
passionlessDrone

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of passionlessDrone
passionlessDrone

ASKER

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