• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Attache dynamic file to Jakarta Commons Email

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be a properly defined MIME type. For Excel files, that is application/vnd.ms-excel
 
reply
    Bookmark Topic Watch Topic
  • New Topic