In Java, how to launch the mail client along with the given file as its attachment - particularly using the method Desktop.getDesktop().mail(URI)
I am using Windows 7 and want to launch MS Outlook.
It is a good question.
Indeed the URI that sent as a parameter to method desktop.mail(URI) allows setting to, cc, bcc, subject, body and does not allow setting attachments. (see http://www.ietf.org/rfc/rfc2368.txt)
However attachments are actually specially formatted fragments of email body. Please read this for more details: http://techhelp.santovec.us/decode.htm.
This means that you can encode your binary attachment using Base64 and create email body that already contains the attachment of any generic file. I personally have not tried this but I believe it must work. Good luck.
As far as I know, it is unfortunatly not possible to specify any attachment using Desktop.mail(URI).
I've tried AlexR suggestion. It doesn't work if the file is too big because of the restriction of the number of characters in the URI.
However, it is still possible using JMAPI, though it only works on x86 platforms.
The ultimate way to make it work is using the JavaMail API, but it forces you to create your own GUI and to set the SMTP server configuration.. which is not pretty user-friendly.
If anyone as other suggestions, i'd be glad to know them.
Related
I want to open a new message, in the system default mail client, and include an attachment, from Java.
I tried Desktop.mail(URI mailtoURI) but I do not know how to specify the attachment.
Then, I tried JavaMail. It is working, but I am not able to open the default mail client
How to call the default mail client from within Java, and specify an attachment?
It doesn't seem to be possible to do this neatly, in a cross-platform way.
See Start Mail-Client with Attachment? for an explanation of how to do it in Windows
There is no agreed standard for adding attachments to messages, therefore every mail client handles it differently. This means it is not possible to write a catch-all method to do this for every possible mail client.
I don't think you can. The Desktop.mail launches mailto handler.
The mailto scheme handler only supports to, cc, subject and body.
Since the mailto scheme only supports mime type of text/plain, it does not support any attachments.
See https://www.rfc-editor.org/rfc/rfc2368
I have a program that must 'prepare' emails for me. When the email is prepared, it should show in outlook (or an other email client - default user mail client).
I have used the Desktop.getDesktop().mail approach, but I'm very limited with the options. (I cannot set the high importance). An other option I've tried is JavaMail. But here the mail will be send instead of opened in the email client. (same for Apache POI).
An other option I've considered is to write a .msg/.eml file and open it, but this must be done manually (via java I get a 'file not found' error or an error regarding privileges).
Does anyone know other options or other approaches I might have missed / overlooked?
You can make use of moyosoft's connector to access outlook functionality from java. Please refer to below url's for more details on this.
http://www.moyosoft.com/joc/
http://www.moyosoft.com/joc/getstarted/
I've been searching for the best way to send an e-mail with an attachment by my java application. I want to use this as an users bug report with logger files. The recipient should be my own e-mail address. I'd prefer the use of an e-mail client.
I tried the following:
Send an e-mail with user authentification like this. I don't want to use this, because the user would need to reveal his e-mail account and password. Furthermore, I'd have to set the properties for every e-mail adress, which is impossible.
Send an e-mail directly to my own e-mail address like this in Listing 16.16 (didnt found an english example). The problem is every e-mail server is using POP authentification nowadays, that means the recipient e-mail server won't accept my e-mail.
Using the mailto URL syntax like this. Doesn't work aswell, because the attachment function isn't working properly in every e-mail client. Best solution so far is to brief the user to add the attachment by himself, after I would put it to his desktop. Or upload the data and add a link to the e-mail body.
The last way I've found is this one. As you could assume this won't work either, because the localhost needs to be connected to the internet and capable enough to send an e-mail.
Hopefully I explained my problem well enough. Is there a different way to send bug reports?
The generally accepted way to get around the problems you describe is to keep all the email logic server side, and then have your application call a web service with the appropriate parameters. It's pretty easy to knock a PHP script / servlet up that will do the job and then send the results on via email, put them in a mysql database or so on.
However, if you can't / don't want to / won't keep this server side, I'd recommend using JavaMail to create a MimeMessage, then using writeTo() to write this to an EML file.
You can then do the usual:
Desktop.getDesktop().open(emlFile);
...which will open the EML file with the default application for handing those files, which is almost always the mail client. Still not foolproof, but if you're determined on sending the email from the client directly I think that's as good as you'll get.
I am trying to send an email with Java, I am using apache commons email library.
I cannot achieve to send an email with a body as HTML and an attachment as PDF (or any file type)
If I use EmailAttachment() and add it to an HtmlEmail object, my mail looks like with two attachment. First one is for HTML, second is for PDF.
Is there any way to do that?
Thank you very much!
It sounds like relatively normal behaviour for a message that's being sent as both text and HTML, and/or a mail client (at the receiving end) that prefers text emails. I suspect that this is due to the behaviour of the client, which you won't be able to change (but on the plus side all HTML emails would appear like this).
The thing is, an HTML email (with a textual component) really is a multipart message, with the HTML content as one of the "extra" parts. All you're actually sending in the email from the server side is a bunch of text, and it's up to the receiving mail client to decide how to display it. In that respect, it is not wrong for the client to display your HTML as an attachment - just like it is not wrong for a smart client to infer that the HTML isn't a "real" attachment and activate some kind of toggle between text and HTML (rather than displaying it as an attachment).
If you're convinced that the client would normally treat HTML in this smart way, then:
You'll have to mention which client you're using to check, because this isn't really an issue with the sending per se; and
You might want to take a look at the raw source of email that "works", and your email that doesn't, in order to determine what the critical differences are that trigger the different rendering modes. Depending on the client software, this could be just about anything - but I'd pay particular attention to part MIME types and charsets.
I tried apache commons mail v1.2 instead of 1.1.
It works!?
Andrej, by the way many thanks for your kindly help.
How does one parse the mail files generated on a Linux system using Java? I mean, I want to be able to extract the From, To, Timestamp and Subject of the various emails inside the file. Any suggestions?
javax.mail.internet.MimeMessage.parse(InputStream)
it's protected but you can subclass it to use the method. However, the file format is quite simple, if you just want some headers, why not parse it on your own?
Those files belong to the Mail Transfer Agent and maybe the user's mail client. Other programs should tread very softly or better yet keep out altogether. Or is your program a mail client?
The "clean" way to do this would be to open up an SMTP or IMAP connection to the mail server / MTA and ask it for pieces of mail on behalf of your user, using his credentials that he gives you.
There's a Java mail API for this that knows how to do this well: http://java.sun.com/products/javamail/ .