Converting raw email bodies into comparable strings is a common need when building spam filters, ticketing systems, or automated reply generators. Aspose.Email Cloud SDK for Node.js provides a powerful API that makes compare text in emails in Node.JS straightforward in Node.js applications. In this guide you will learn how to set up the SDK, run a complete code sample that reads .EML files, perform batch similarity checks, fine‑tune thresholds, and invoke the same operation via REST cURL calls.
Compare Text in Emails in Node.JS in 5 Steps
This tutorial shows how to compare text in emails in Node.JS using the Aspose.Email Cloud SDK.
- Install the SDK and configure credentials: Add the package to your project and create a
Configurationobject with your client ID and secret.
const { EmailApi, Configuration } = require('asposeemailcloud');
const config = new Configuration({
clientId: process.env.ASPose_CLIENT_ID || 'YOUR_CLIENT_ID',
clientSecret: process.env.ASPose_CLIENT_SECRET || 'YOUR_CLIENT_SECRET'
});
const emailApi = new EmailApi(config);
- Load the source and target EML files: Use
fs.readFileSyncto read the raw bytes of each email.
const fs = require('fs');
function readEmailFile(filePath) {
return fs.readFileSync(filePath);
}
- Set comparison options: Create a
CompareOptionsinstance and configure flags such asignoreCase,ignoreWhitespace, andsimilarityThreshold.
const { CompareOptions } = require('asposeemailcloud');
const compareOpts = new CompareOptions();
compareOpts.ignoreCase = true;
compareOpts.ignoreWhitespace = true;
compareOpts.ignorePunctuation = false;
compareOpts.similarityThreshold = 0.8; // 80%
- Execute the compare operation: Call
emailApi.comparewith the prepared options. The response containssimilarity(0‑1) andisSimilar(boolean).
compareOpts.leftEmail = leftBytes;
compareOpts.rightEmail = rightBytes;
const result = await emailApi.compare(compareOpts);
console.log(`Similarity: ${result.similarity}, IsSimilar: ${result.isSimilar}`);
- Process batch results: Iterate over a folder of emails, compare each file against a reference email, and log the outcomes.
For more details on the API, refer to the Email API Reference.
Full Working Example for Email Text Comparison in Node.JS
The following code demonstrates how to compare text in emails in Node.JS with a single function that handles both single and batch comparisons.
// Compare Text in Emails using Aspose.Email Cloud SDK for Node.js
const fs = require('fs');
const path = require('path');
const { EmailApi, CompareOptions, Configuration } = require('asposeemailcloud');
// -----------------------------------------------------------------------------
// Configuration – replace with your Aspose Cloud credentials
// -----------------------------------------------------------------------------
const config = new Configuration({
clientId: process.env.ASPose_CLIENT_ID || 'YOUR_CLIENT_ID',
clientSecret: process.env.ASPose_CLIENT_SECRET || 'YOUR_CLIENT_SECRET'
});
const emailApi = new EmailApi(config);
// -----------------------------------------------------------------------------
// Helper: read an .eml file and return its raw bytes (Buffer)
// -----------------------------------------------------------------------------
function readEmailFile(filePath) {
return fs.readFileSync(filePath);
}
// -----------------------------------------------------------------------------
// Compare two email bodies and return similarity (0‑1)
// -----------------------------------------------------------------------------
async function compareEmailTexts(leftPath, rightPath, options) {
const leftBytes = readEmailFile(leftPath);
const rightBytes = readEmailFile(rightPath);
const compareOpts = new CompareOptions();
compareOpts.leftEmail = leftBytes;
compareOpts.rightEmail = rightBytes;
compareOpts.ignoreCase = options.ignoreCase ?? true;
compareOpts.ignoreWhitespace = options.ignoreWhitespace ?? true;
compareOpts.ignorePunctuation = options.ignorePunctuation ?? false;
compareOpts.similarityThreshold = options.similarityThreshold ?? 0.8; // default 80%
// API call – returns { similarity: number, isSimilar: boolean }
const result = await emailApi.compare(compareOpts);
return result;
}
// -----------------------------------------------------------------------------
// Batch processing – compare a reference email against many others
// -----------------------------------------------------------------------------
// Performance tip: limit parallelism to avoid overwhelming the service.
// Here we process 5 emails concurrently.
async function batchCompare(referenceEmailPath, emailsFolder, options) {
const files = fs.readdirSync(emailsFolder)
.filter(f => f.toLowerCase().endsWith('.eml'))
.map(f => path.join(emailsFolder, f));
const concurrency = 5;
let index = 0;
const results = [];
async function worker() {
while (true) {
const i = index++;
if (i >= files.length) break;
const targetPath = files[i];
try {
const res = await compareEmailTexts(referenceEmailPath, targetPath, options);
results.push({
target: path.basename(targetPath),
similarity: res.similarity,
isSimilar: res.isSimilar
});
console.log(`[${path.basename(targetPath)}] Similarity: ${res.similarity.toFixed(3)} – ${res.isSimilar ? 'Similar' : 'Different'}`);
} catch (err) {
console.error(`Error comparing ${targetPath}:`, err.message);
}
}
}
// launch workers
const workers = [];
for (let i = 0; i < concurrency; i++) workers.push(worker());
await Promise.all(workers);
return results;
}
// -----------------------------------------------------------------------------
// Main execution
// -----------------------------------------------------------------------------
(async () => {
try {
const referenceEmail = path.resolve(__dirname, 'reference.eml'); // reference email
const emailsDir = path.resolve(__dirname, 'emails'); // folder with .eml files to compare
const compareOptions = {
ignoreCase: true,
ignoreWhitespace: true,
ignorePunctuation: false,
similarityThreshold: 0.75 // 75% similarity required to be considered similar
};
console.log('Starting batch comparison...');
const batchResults = await batchCompare(referenceEmail, emailsDir, compareOptions);
// Example: summarize results
const similarCount = batchResults.filter(r => r.isSimilar).length;
console.log(`\nBatch completed. ${similarCount} out of ${batchResults.length} emails are similar to the reference.`);
} catch (err) {
console.error('Unexpected error:', err);
}
})();
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Compare Email Text via REST API Using cURL
The same comparison can be performed with direct REST calls. Below is a minimal workflow.
- Obtain an access token (replace placeholders with your credentials).
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
- Upload the left email (
left.eml).
curl -X PUT "https://api.aspose.cloud/v4.0/email/storage/file/left.eml" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @left.eml
- Upload the right email (
right.eml).
curl -X PUT "https://api.aspose.cloud/v4.0/email/storage/file/right.eml" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @right.eml
- Execute the compare operation with desired options.
curl -X POST "https://api.aspose.cloud/v4.0/email/compare" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"leftEmail": "left.eml",
"rightEmail": "right.eml",
"ignoreCase": true,
"ignoreWhitespace": true,
"ignorePunctuation": false,
"similarityThreshold": 0.75
}'
The response returns similarity and isSimilar. For more details, see the official API documentation.
Installing and Configuring Aspose.Email Cloud SDK for Node.JS
Install the library via npm and verify the download source.
npm install @asposecloud/aspose-email-cloud
You can also clone the source from the GitHub repository: Aspose.Email Cloud SDK for Node.js.
Make sure your runtime is Node.js 14 or newer and that you have a valid Aspose Cloud account with client credentials.
Configuring Comparison Options for Email Text Similarity
The SDK exposes several flags that influence the similarity algorithm:
- ignoreCase - When
true, the comparison ignores letter case. - ignoreWhitespace - Skips spaces, line breaks, and tabs.
- ignorePunctuation - Removes punctuation characters before comparison.
- similarityThreshold - A value between
0and1that defines the cut‑off for considering two emails similar.
You can adjust these options directly on the CompareOptions object as shown in the code snippets above. For a full list of parameters, refer to the Email API Reference.
Final Thoughts
Integrating email text similarity checks into a Node.js backend is now a few lines of code away thanks to the Aspose.Email Cloud SDK for Node.js. By configuring the comparison options you can tailor the algorithm to your specific use case whether you need strict matching or a more tolerant similarity score. Remember that production deployments require a proper license; you can view pricing details on the product page and obtain a temporary license from the temporary license page. With the SDK you can easily compare text in emails in Node.JS and scale the solution to handle large volumes.
Common Questions
-
What is the best way to handle different email encodings when using compare text in emails in Node.JS?
The SDK automatically detects UTF‑8, UTF‑16, and other common encodings. If you encounter an unsupported format, convert the message to UTF‑8 before passing it to thecomparemethod. See the official documentation for conversion helpers. -
Can I compare email bodies without attachments?
Yes. Thecompareoperation works on the raw email content only. Attachments are ignored unless you explicitly extract and compare them separately. -
How do I increase performance for large batches?
Use the worker‑pool pattern shown in the sample code and tune theconcurrencyvariable. Also consider uploading files to Aspose Cloud storage once and reusing the stored references for multiple comparisons. -
Is there a limit to the size of emails I can compare?
The cloud service accepts emails up to 10 MB each. Larger messages should be split or trimmed before comparison.