How to change attachment of Outlook .msg file with Apache POIFS/HSMF - java

I am working on some project that requires changing attachments of an Outlook .msg file.
I chosen to use Apache POI with its low-level POIFS API.
I am able to substitute binary data of an attachments by re-writing PR_ATTACH_DATA_OBJ ByteChunk, as well as few other Chunks related to filename, extension, displayName.
However, when I open the resulting .msg file in outlook my attachments viewed as no-extension file but content is exact what I have pushed to it:
There are two more fields that I can't find possible data to write as well as there is no description for them in MAPIProperty.Java POI dictionary:
Can somebody advice me how I can make attachment substitution possible to success? What kind of data I have to put into these two fields in order to fix preview? Will this help?
I am looking for a free solution. I already tried Aspose.Email and this works brilliant but it is not currently an option.

Embedded message attachment (stored in PR_ATTACH_DATA_OBJ property on an attachment) is not a file.
Does Outlook correctly open the attachment? Make sure the attachment type remains the same.

Related

How to detect an HTML file that had extension changed to Excel .xls

I have a java app that processes excel files from emails in an automated fashion (.xls, xlsx etc). I've noticed that some files are not native files. Opening in Excel will give a warning that the file is corrupt/badly formated. Opening in notepad++ clearly shows HTML
Unfortunately I can't just manually handle these files so I need a way to automatically spot them.
I noticed that when I use java.io.fiile object then with org.apache.tika.Tika I can detect the the type. So with the file object I can find out the extension, and with tika.detect() i can find that the format is called "text/html". (Not sure if this is the best way, but it seems to work with my singular example)
So I can then find these kinds of files using:
File file = getTheFileObject();
if ( tika.detect(file).equalsIgnoreCase("text/html") && file.getName().contains(".xls") ) { ... do what I want with the corrupt file... }
My problem comes when doing something similar with email attachments. To get the file from emails I'm using the com.microsoft.ews-java-api 2.0 and from this I can get a FileAttachment object which represents the file.
But when I attempt to use tika.detect() on this (same corrupt file) i get a different format output "application/octet-stream" instead of "text/html". Or get "application/vnd.ms-excel" using the FileAttachments own methods
How can I spot these corrupt files if I can't spot the html formated xls files?
FileAttachment attachment = getFileAttachment();
attachment.getContentType() //application/vnd.ms-excel
tika.detect(attachment.getContentStream()) //application/octet-stream
How would I spot an html file that has .xls file extension from the emails ews FileAttachment object? Will tika still help?

How to create a pdf file on the fly and make it an email attachment?

The tutorials I found for sending an email with attachment are based on an already created file. But my need is to create a pdf file on the fly and attach it to an email. How to do that ?
Most PDF generators support writing to an OutputStream. One solution is to use a FileOutputStream, save the PDF to disk, create the mail (using the tutorials you have), send the mail, delete the file.
The other option is to use ByteArrayOutputStream (use the one from commons-lang3, since the one in the Java runtime is very slow for large files). Use that to generate the "file". When the PDF is created, use getByteArray() and put that into a ByteArrayInputStream which you should be able to use as a mail attachment.

Java code to convert google drive spreadsheet to excel

Need Java Code to convert Google Drive Spreadsheet to Excel. Later I want to send the converted file as mail attachment.
I am using this code to retrieve the file metadata -
services.files().get(fileObj.getId()).executeAsInputStream();
I have a code to send the mail, but the problem is if I use the above code, the attachment is like a link to Google Drive document.
The File object has exportLinks property that
"Links for exporting Google Docs to specific formats."
exportLinks.(key) can be used to get the link for a specific format. Replace the key with the mimeType you want the file exported to (if I remember correctly)
You can find more info about the exportLinks keys (and Document Downloading in general) here

How to make client download xml file from server?

My server is implemented with few servlets when each one is responsible for different task.
I need to make client to download a specified xml file from server when a SAVE button in html page pressed.
I've read that the best way is to host file on server and just let client download, but I don't know how to implement it.
Any example will be highly appreciated. :)
p.s.
I'm using JAVA.
Do these steps:
Set proper MIME-type for your file. If you do not want (there's no suitable MIME-type) to set MIME-type specific to your file type, then set it to application/octet-stream
Set content length to the response
Set content disposition
Open output binary stream, then read your file and write its contents to this output stream.
That's it.
Here is the sample code

finding file extension by java code

Suppose users can upload files, I want to find the extension of the uploaded file.
Even if the user has renamed the extension of the file, I want to find the real extension of that file by it's header or bytecode..etc.
Please help me with a solution.
Note: Not just the extension by substring() or getContentType() but the real file extension,
say for example(in windows), its a .doc file and user renames it to .jpg and uploads it.
Its possible in php, but I don't know how to do that in java! but it can be done.
Thank you.
Apache Commons: FileUpload is a good place to start. further, you could look at the link i referenced: JSP: Get MIME Type on File Upload for hints on how to do this.
As the user states in their question, the thought is if i rename a .png to a .jpg this will fool the getContentType() into thinking it's now a .jpg file. A quick search on google provided the following answer result: Get the mime type from a file that lists 3 very good options:
Apache Tika
The Apache Tika™ toolkit detects and extracts metadata and structured text content from various documents using existing parser libraries
JMimeMagic
jMimeMagic is a Java library for determining the MIME type of files or streams
A java library that claims to help you with this, is: mime-util
Enable Java programs to detect MIME types based on file extensions, magic data and content sniffing. Supports detection from java.io.File, java.io.InputStream, java.net.URL and byte arrays.
A quick search brought this post up: http://fredeaker.blogspot.com/2006/12/file-type-mime-detection.html
The post lists several "magic" libraries that can detect the file type based on its contents.

Categories

Resources