SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast gecastill's Avatar
    Join Date
    May 2005
    Location
    Virginia US
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to email an Apache POI workbook?

    The POI project consists of APIs for manipulating various file formats based upon Microsoft's OLE 2 Compound Document format, and Office OpenXML format, using pure Java. In short, you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. POI is your Java Excel solution (for Excel 97-2007). However, we have a complete API for porting other OLE 2 Compound Document formats and welcome others to participate.

    http://poi.apache.org/

    I am able to manipulate a spreadsheet using Apache POI, I need to email the spreadsheet to certain recipients.

    How do I email an HSSFWorkbook java object?

    To send an email without the spreadsheet I am doing the following:
    try {
    String[] cmdarray = new String[4];
    String emailBody = msg;

    cmdarray[0] = "mail";
    cmdarray[1] = "-s";
    cmdarray[2] = subject;
    cmdarray[3] = toList;

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(cmdarray);
    BufferedOutputStream out = new BufferedOutputStream(p.getOutputStream());
    out.write(emailBody.getBytes());
    out.write("\n\n".getBytes()); //Closes the email editor and sends the mail.
    out.close();
    } catch(Throwable th) {
    th.printStackTrace();
    }

    Please advise, thanks.
    Best Regards,
    Gibran E Castillo

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    As you are using the 'mail' program on your environment, instead of Java, there's no way for us to help you.

    You might consider looking into JavaMail, which can easily handle attachments.

  3. #3
    SitePoint Enthusiast gecastill's Avatar
    Join Date
    May 2005
    Location
    Virginia US
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using javax.mail which is JavaMail.

    How do I attach the HSSFWorkbook java object into an email using JavaMail?

    Please advise, thanks.
    Best Regards,
    Gibran E Castillo

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using JavaMail attchments
    http://java.sun.com/developer/online...ailAttachments

    I have used Jscape, which provides me the secure connection

    http://www.jscape.com/articles/sendi...sing_java.html

  5. #5
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to consider using the Apache Commons email library.

    http://commons.apache.org/email/

    I've had to add attachments through the JavaMail API before and it really wasn't fun.

    I've never used the Commons email library, but I bet it is easier then the base JavaMail API.

    Best of Luck.
    baby steps... baby steps...

  6. #6
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another thing...

    If you are sending your message in a rich text formate (like HTML), you might want to consider sending your message in both html and plain text. There are still a substantial number of plain text only email clients out there.

    Best of Luck.
    baby steps... baby steps...

  7. #7
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Apache Commons Email FTW! I thought they gave up on that, so I didn't recommend it.

    Very easy to use.

  8. #8
    SitePoint Enthusiast gecastill's Avatar
    Join Date
    May 2005
    Location
    Virginia US
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sumit1001,

    The JavaMail attachments link is useful http://java.sun.com/developer/online...ingAttachments

    However, I am having problems with j2ee.jar classes javax.activation.DataHandler, javax.activation.DataSource, and javax.activation.FileDataSource. I am not sure if I should be using java.sql.DataSource or javax.activation.DataSource.

    Currently, I am getting the following Exception in thread "main"
    java.lang.NoClassDefFoundError: com/sun/activation/registries/MailcapFile
    at javax.activation.MailcapCommandMap.<init>(MailcapCommandMap.java:98)
    at javax.activation.CommandMap.getDefaultCommandMap(CommandMap.java:44)
    at javax.activation.DataHandler.getCommandMap(DataHandler.java:136)
    at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:568)
    at javax.activation.DataHandler.getContent(DataHandler.java:501)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:952)
    at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:1617)
    at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1598)
    at javax.mail.Transport.send(Transport.java:79)


    Do you know what is causing javax.mail.Transport.send() to throw the exception java.lang.NoClassDefFoundError: com/sun/activation/registries/MailcapFile

    Please advise, thanks.
    Best Regards,
    Gibran E Castillo

  9. #9
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, there is some strange jar requirement for JavaMail.

    http://java.sun.com/javase/technolog...ads/index.html

    I don't know why the activation.jar doesn't ship with JavaMail, but it just doesn't.

    Download the activation.jar from the above link and place it into your classpath and everything should be ok...

    Best of Luck.
    baby steps... baby steps...

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup adding activation.jar would do the work

  11. #11
    SitePoint Enthusiast gecastill's Avatar
    Join Date
    May 2005
    Location
    Virginia US
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added activation.jar and mail.jar to my runtime classpath and everything is working like it should.

    Thank you all for your help.

    Cheers!!
    Best Regards,
    Gibran E Castillo


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •