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/vn d.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)Command Map.getDef aultComman dMap();
mc.addMailcap("text/html;; x-java-content-handler=com .sun.mail. handlers.t ext_html") ;
mc.addMailcap("text/xml;; x-java-content-handler=com .sun.mail. handlers.t ext_xml");
mc.addMailcap("text/plain; ; x-java-content-handler=com .sun.mail. handlers.t ext_plain" );
mc.addMailcap("multipart/* ;; x-java-content-handler=com .sun.mail. handlers.m ultipart_m ixed");
mc.addMailcap("application /vnd.ms-ex cel;; x-java-content-handler=com .sun.mail. handlers.m ultipart_m ixed");
mc.addMailcap("application /octet-str eam;; x-java-content-handler=com .sun.mail. handlers.m ultipart_m ixed");
mc.addMailcap("message/rfc 822;; x-java-content-handler=com .sun.mail. handlers.m essage_rfc 822");
CommandMap.setDefaultComma ndMap(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. RecipientT ype.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,"ap plication/ octet-stre am"));
mbp2.setDataHandler(new DataHandler(attachment,"ap plication/ vnd.ms-exc el"));
mbp2.setFileName(attachmen tName);
// 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
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/vn
**************************
logger.error("************
logger.error("Our inbound attachment is of size: "+ attachment.length);
logger.error("************
MailcapCommandMap mc = (MailcapCommandMap)Command
mc.addMailcap("text/html;;
mc.addMailcap("text/xml;; x-java-content-handler=com
mc.addMailcap("text/plain;
mc.addMailcap("multipart/*
mc.addMailcap("application
mc.addMailcap("application
mc.addMailcap("message/rfc
CommandMap.setDefaultComma
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(sender));
InternetAddress[] address = new InternetAddress[toAddress.
for (int cntr = 0; cntr < toAddress.length; cntr++)
{
address[cntr] = new InternetAddress(toAddress[
}
msg.setRecipients(Message.
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,"ap
mbp2.setDataHandler(new DataHandler(attachment,"ap
mbp2.setFileName(attachmen
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
in producing corrupt attachments when attaching non-text files of substantial size. Perhaps
that was the reason for your problem
ASKER