Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send an email attachment using the designated client, programmatically from Java

Tags:

I'd like to encourage our users of our RCP application to send the problem details to our support department. To this end, I've added a "Contact support" widget to our standard error dialogue.

I've managed to use URI headers to send a stacktrace using Java 6's JDIC call: Desktop.getDesktop().mail(java.net.URI). This will fire up the user's mail client, ready for them to add their comments, and hit send.

I like firing up the email client, because it's what the user is used to, it tells support a whole lot about the user (sigs, contact details etc) and I don't really want to ship with Java Mail.

What I'd like to do is attach the log file and the stacktrace as a file, so there is no maximum length requirement, and the user sees a nice clean looking email, and the support department has a lot more information to work with.

Can I do this with the approach I'm taking? Or is there a better way?

Edit: I'm in an OSGi context, so bundling JDIC would be necessary. If possible, I'd like to ship with as few dependencies as possible, and bundling up the JDIC for multiple platforms does not sound fun, especially for such a small feature.

JavaMail may be suitable, but for the fact that this will be on desktops of our corporate clients. The setup/discovery of configuration would have to be transparent, automatic and reliable. Regarding JavaMail, configuration seems to be manual only. Is this the case?

The answer I like most is using the Desktop.open() for an *.eml file. Unfortunately Outlook Express (rather than Outlook) opens eml files. I have no idea if this is usual or default to have Windows configured for to open EML files like this. Is this usual? Or is there another text based format that a) is easy to generate, b) opens by default in the same email client as users would be using already?

like image 678
jamesh Avatar asked Sep 17 '08 10:09

jamesh


2 Answers

You could save a temporary .eml file, and Desktop.getDesktop().open(emlFile)
Edit: As you point out, this will unfortunately open outlook express instead of outlook.
However if you have Windows Live Mail installed, it will use that.

like image 56
Stephen Denne Avatar answered Sep 30 '22 17:09

Stephen Denne


If you're using JDK 6 (you really should), the Desktop API is now part of the JRE. See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/ for more information.

like image 43
Asgeir S. Nilsen Avatar answered Sep 30 '22 15:09

Asgeir S. Nilsen