• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attache dynamic file to Jakarta Commons Email

 
Maksim Ustinov
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I'm wrong, but can't you use MultiPartEmail.attach in combination with a ByteArrayDataSource?
 
Maksim Ustinov
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be a properly defined MIME type. For Excel files, that is application/vnd.ms-excel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic