This example demonstrates the standard open-edit-save pipeline with Email documents, using different options on every step.
Introduction
There is a group of Email file formats, which usually are intended for storing individual mail letters, or contact data, personal information, calendar and so on. There are plenty of them, because almost every email program uses its own set of such formats.
From its emergence the GroupDocs.Editor for Java had no support for editing the such documents in Email formats. But starting from the version 22.9 we finally released this possibility! And this article explains in detail how to edit different Email files, because due to their nature their editing differs from editing common text documents.
Loading
Loding of the Email documents is usual and the same as for other formats. Like with text, XPS and e-Book formats, and unlike the PDF and Office formats, there is no loading options for the Email documents — just specify a path to the file or a byte stream with the document content in the constructor of the com.groupdocs.editor.Editor class and that’s all.
Code example below demonstrates loading a single EML file to the Editor instance through the file path and byte stream.
StringemlFilename="C:\Attachments.eml";InputStreamemlStream=newFileInputStream(newFile(emlFilename));EditoreditorFromPath=newEditor(emlFilename))//from the path
EditoreditorFromStream=newEditor(emlStream)//from the stream
{//Here two Editor instances can separately work with one file
}
Editing
Like for all other formats, there is a special EmailEditOptions class for adjusting editing of the Email documents. Instance of this class should be specified in the Editor.edit(IEditOptions editOptions) method. And, like for all other formats, it is possible to use a parameterless overload of the Editor.edit() — in this case, if Email document was loaded to the Editor instance, the GroupDocs.Editor will automatically apply the default EmailEditOptions.
EmailEditOptions class has only one member — a MailMessageOutput property of a MailMessageOutput type. MailMessageOutput is a flagged enum that controls, which parts of the mail message should be delivered to the output EditableDocument and then to the emitted HTML. The MailMessageOutput enum has both atomic values that are responsible for a single part of the mail message, as well as combined values, like Common and All. It is allowed to use not only these combined values, but any user-defined combination of atomic values is acceptable. For example, it is possible to create a combination Subject | Date | To | Cc | Bcc | From, which processes only a metadata, but not the body and attachments list.
It is important to mention that currently the com.groupdocs.editor.Editor cannot process the content of the attachments, it can only display a list of attachment names. So the MailMessageOutput.Attachments flag, when specified, emits a list of attachment names, if they are present in the mail message.
Code example below shows adjusting of the MailMessageOutput and generating two different EditableDocument for editing from a single input Email document.
//0. Prepare a sample file
StringemlFilename="C:\Attachments.eml";InputStreamemlInputPath=System.IO.Path.Combine(Common.TestHelper.EmailFolder,emlFilename);//1. Load to the Editor class
Editoreditor=newEditor(emlInputPath);//2. Create 1st edit options with only metadata
EmailEditOptionseditOptions=newEmailEditOptions(MailMessageOutput.Subject|MailMessageOutput.From|MailMessageOutput.To|MailMessageOutput.Cc|MailMessageOutput.Bcc|MailMessageOutput.Date);//2. Create 2nd edit options with only subject, body and attachments list
EmailEditOptionseditOptionsBody=newEmailEditOptions(MailMessageOutput.Subject|MailMessageOutput.Body|MailMessageOutput.Attachments);//3. Generate two different EditableDocument with these options from single input "Attachments.eml"
EditableDocumentonlyMetadata=editor.edit(editOptionsMetadata);EditableDocumentonlyBody=editor.edit(editOptionsBody);//4. Then emit HTML/CSS and send to the WYSIWYG-editor on the client-side
Saving
Saving the edited Email documents in general follows the same principles, like for other document formats, but with several distinctions. As usual, after obtaining an edited HTML-content from the client-side WYSIWYG-editor an instance of the EditableDocument should be created, and then need to pass it to the com.groupdocs.editor.Editor.save() method.
Like for all other formats, during saving, along with the EditableDocument and a path or a stream, an inheritor of the ISaveOptions interface should be specified, and for email documents it is a EmailSaveOptions class. Like the previously described EmailEditOptions, this class has only one member — a MailMessageOutput property of a MailMessageOutput type. The only distinction between this MailMessageOutput property in EmailEditOptions and EmailSaveOptions is that in the first case the MailMessageOutput property controls, which parts of the mail message will be processed while generating EditableDocument from the input Email file, while in the second case the MailMessageOutput property controls, which parts of the mail message will be processed while generating output Email file from EditableDocument, obtained after editing. So it is possible, for example, send to the WYSIWYG-editor a full mail message with 100% all content, but generate an output file only with a Subject and Body content, for example.
Email format family consists of many different formats, but the EmailSaveOptions does not allow to specify the format for the output document — it automatically be the same, as the format of the original Email document, loaded into the com.groupdocs.editor.Editor class. So, for example, if you loaded the MSG file, then the com.groupdocs.editor.Editor.save() method will also generate the MSG file, and no matter which file extension you specify.
Code example below shows full load-edit-save pipeline for the Email documents. It loads a document, makes it editable with MailMessageOutput.All combined flag, and then generates to output Email documents: first one is saved to the file with MailMessageOutput.Common, while second is saved to the stream with customly created combined flag.
//0. Prepare a sample file
StringmsgInputPath="C:\ComplexExample.msg";//1. Load to the Editor class
EditormsgEditor=newEditor(msgInputPath);//2. Create edit options with all content
EmailEditOptionseditOptions=newEmailEditOptions(MailMessageOutput.All);//3. Generate an editable document
EditableDocumentoriginalDoc=msgEditor.edit(editOptions);//4. Emit HTML from EditableDocument, send it to the client-side, edit it there in WYSIWYG-editor (omitted here)
StringsavedHtmlContent=originalDoc.getEmbeddedHtml();//5. Obtain edited content from the client-side and generate a new EditableDocument from it (omitted here)
EditableDocumenteditedDoc=EditableDocument.fromMarkup(savedHtmlContent,null);//6. Create 1st save options
EmailSaveOptionssaveOptions1=newEmailSaveOptions(MailMessageOutput.Common);//7. Create 2nd save options
EmailSaveOptionssaveOptions2=newEmailSaveOptions(MailMessageOutput.Body|MailMessageOutput.Attachments);//8. Generate and save 1st output MSG to the file
StringoutputMsgPath="C:\OutputFile.msg";msgEditor.save(editedDoc,outputMsgPath,saveOptions1);//9. Generate and save 2nd output MSG to the stream
StreamoutputMsgStream="C:\OutputStream.msg";msgEditor.save(editedDoc,outputMsgStream,saveOptions2);//10. Dispose all resources
outputMsgStream.dispose();editedDoc.dispose();originalDoc.dispose();msgEditor.dispose();
Obtaining Email document info
Article Extracting document metainfo describes the getDocumentInfo() method, that allows to detect the document format and extract its metadata without editing it. Actually, after adding Email support to the GroupDocs.Editor, this mechanism also works with Email documents.