How to Convert MSG Files to HTML using Node.js

Cloudmersive
2 min readNov 6, 2023

--

When we convert MSG files to HTML, we can guarantee they’re viewable on any web browser client, and we can ensure they’re editable without the help of a dedicated application (as would be the case with PDF).

With the below ready-to-run code examples, we can easily take advantage of free API to quickly convert our MSG files into HTML format. If our MSG files contain attachments, we can elect to include them or disinclude them by customizing the optional includeAttachments parameter. Similarly, we can choose if we want to include information outside of the body of our email (such as the email subject) in our HTML string — or return that content as properties in our API response instead — by setting the bodyOnly parameter.

Our first step is to install the SDK. We can either run the following command:

npm install cloudmersive-convert-api-client --save

Or, alternatively, we can add the following snippet to our package.json:

npm install cloudmersive-convert-api-client --save

Finally, we can include the below code examples in our file and authorize our request with a free-tier API key (we can get one by registering a free account on the Cloudmersive website):

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.

var opts = {
'bodyOnly': true, // Boolean | Optional; If true, the HTML string will only include the body of the MSG. Other information such as subject will still be given as properties in the response object. Default is false.
'includeAttachments': true // Boolean | Optional; If false, the response object will not include any attachment files from the input file. Default is true.
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentMsgToHtml(inputFile, opts, callback);

That’s all there is to it — no more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.