File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Attache dynamic file to Jakarta Commons Email Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Attache dynamic file to Jakarta Commons Email" Watch "Attache dynamic file to Jakarta Commons Email" New topic
Author

Attache dynamic file to Jakarta Commons Email

Maksim Ustinov
Greenhorn

Joined: Sep 15, 2008
Posts: 26
I have encountered a problem attaching dynamically generated Excel file to my email. I'm using Jakarta Commons Email I can easily attache file using just a path or Web URL but can't figure out how to attach InputStream or byte[]. Can someone help me with that.

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Correct me if I'm wrong, but can't you use MultiPartEmail.attach in combination with a ByteArrayDataSource?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maksim Ustinov
Greenhorn

Joined: Sep 15, 2008
Posts: 26
Yes you can, i guess. I can't find any example of how to use it. I don't see anything in their documentations.

WHere can i find it?

Plus, there are no examples how to send HTML email with dynamically generated attachment file
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Have you already read the user guide?

As for the attachment, if it needs to be a real attachment it's really simple:

You can replace the byte[] with an InputStream if needed.


If it needs to be an inlined (embedded) attachment (mostly used for images) inside the HtmlEmail, you can use the embed method as described in the user guide; it is overloaded to take a DataSource.
Maksim Ustinov
Greenhorn

Joined: Sep 15, 2008
Posts: 26
Thanks Rob for your help.
It work great, but I think I have small problem with Gmail.

First, when I open my email with Outlooks it looks fine. Body is on it's place and file is attached. Here is how it looks like


And here is gmail screenshot:



Is it gmail problem or i need to set up something in my code?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Some email clients, including web-based clients, cannot handle HTML or multi-part emails properly. Instead of displaying the HTML part, they try to search for a plain text part and display that. Everything else becomes an attachment.

You should always include a plain text part as well when sending HTML emails, because of these clients. That's why HtmlEmail has 3 methods for setting the body text - two for HTML and one for plain text.
Maksim Ustinov
Greenhorn

Joined: Sep 15, 2008
Posts: 26
One more question.
When I'm creating ByteArrayDataSource first I need to include byte[] file and then aType.
Can you explain me what aType stands for? As I figure out that the format should be "application/type". can I put anything there and specify format only when I'm attaching file (emali.attach(ds, "file.xls", "description"))

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

It should be a properly defined MIME type. For Excel files, that is application/vnd.ms-excel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attache dynamic file to Jakarta Commons Email