Send HTML email with an pdf attachment by using Java - java

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.

Related

HTML table loses border when mail is forwarded

I am trying to include HTML tables with Outlook emails. It is able to render and provide the content when the mail is sent initially. But, the table loses it's border whenever the mail is forwarded. But, the structure is preserved.
Is there anyway to embed some css so that the content is rendered whenever the mail is forwarded ?
EDIT : I am using outlook to send the mails and the tables are already inline styled. Also the code is written for generation of email using apache velocity engine.
it's wired as each email client treats forwarded messages, so if gmail adds some classes to restyle the forwarded tables I don't think there's much you can do about that. I would suggest trying to use some divs inside table cells and give a border to those.
and there's a tool that will help you so much when writing email templates, called emogrefier. basically you write css and it automatically applies that style as inline. you can even use scss and set the exported file to be included as inline style.

Parse OLE-Object Email Attachments from Outlook (Java)

Situation:
The System is fetching Emails via standard methods (Pop3) from a Mailserver and sends them to the Archiving component as multi-part messages (*.eml files).
If the mail was sent from Outlook it may contain an OLE-Object for example a MS Word, MS Excel and so on. There are several ways to include such an Object, for example via Menu "Insert->Object"
Problem:
Our requirement is now to extract those OLE-Objects archive them as separate attachments. It would be best to do it in Java or other JVM Languages. Other Languages and Frameworks would be possible but they must be working on different platforms (Win, Linux, Unix)
The problem is we haven't found any library or functions in the libraries to do this.
First issue is, that the message the receiver gets depends on how outlook is configured:
It may send RTF messages: Then the receiver get's a message having an attachment "Untitled Attachment.bin"
It may send HTML messages: Then the receiver get's a message inlcuding an attachment "oledata.mso".
What we've tried so far:
We tried Apache POI, especially POIFS to load the file "oledata.mso" but it complained about that some header value is wrong:
Invalid header signature; read 0xD7EC9C7800013000, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
We found a website talking about the same issue. As far as we understood, the oledata.mso is an collection of Compound File Binary Files. Which should also be parsable with POI individually because the OpenMCDF is doing the same things as POI.
On this website they somehow separate the parts and parse them separatly. But we haven't found a similar function or any specification how this is done.
Can anybody please shed some light on this?

How to open default mail client (e.g outlook) with attachment in Java

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

Best way to send an E-Mail from a java Application

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.

Java: adding attachment to the mail client launched using getDesktop().mail(URI)

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.

Categories