How to Convert EML and MSG Email Files to HTML in .NET
Converting email files to HTML format is essential for web-based applications, email archiving systems, and modern document management solutions. HTML provides a versatile format that enables easy viewing, styling, and integration of email content into web-based systems. The Aspose.Email LowCode Converter simplifies this process, allowing developers to transform EML and MSG files into HTML with minimal code complexity.
Prerequisites
Before implementing email-to-HTML conversion, ensure your development environment includes:
- Visual Studio 2019 or later (or any compatible .NET IDE)
- .NET 6.0 SDK or higher
- Aspose.Email NuGet package
- Basic knowledge of C# and file handling
Step 1: Install Aspose.Email via NuGet
Install the Aspose.Email package using one of these methods:
Package Manager Console:
Install-Package Aspose.Email
Package Manager UI:
- Right-click your project in Solution Explorer
- Select “Manage NuGet Packages”
- Search for “Aspose.Email”
- Click “Install”
PackageReference in .csproj:
<PackageReference Include="Aspose.Email" Version="24.3.0" />
Step 2: Write the Conversion Code
Here’s a complete example demonstrating single file conversion from EML/MSG to HTML:
using Aspose.Email.LowCode;
using System;
using System.IO;
using System.Threading.Tasks;
namespace EmailToHtmlConverter
{
class Program
{
static async Task Main(string[] args)
{
try
{
// Define input and output paths
string inputFilePath = @"C:\Emails\sample.eml";
string outputDirectory = @"C:\ConvertedEmails";
// Create output directory if it doesn't exist
Directory.CreateDirectory(outputDirectory);
// Create input stream from the email file
using var inputStream = File.OpenRead(inputFilePath);
// Set up the output handler to save converted files
var outputHandler = new FolderOutputHandler(outputDirectory);
// Get the filename for processing
string fileName = Path.GetFileName(inputFilePath);
// Convert the email file to HTML
await Converter.ConvertToHtml(inputStream, fileName, outputHandler);
Console.WriteLine($"Successfully converted {fileName} to HTML format");
Console.WriteLine($"Output saved to: {outputDirectory}");
}
catch (Exception ex)
{
Console.WriteLine($"Conversion failed: {ex.Message}");
}
}
}
}
Code Breakdown:
- Input Stream Creation:
File.OpenRead()
creates a stream from the source email file - Output Handler Setup:
FolderOutputHandler
manages where the converted HTML files are saved - Conversion Execution:
Converter.ConvertToHtml()
performs the actual transformation - Resource Management:
using
statements ensure proper disposal of file streams
Step 3: Handle Multiple Files (Batch Conversion)
For processing multiple email files simultaneously, implement batch conversion:
using Aspose.Email.LowCode;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
public class BatchEmailConverter
{
public static async Task ConvertMultipleEmailsToHtml()
{
try
{
// Define directories
string inputDirectory = @"C:\EmailFiles";
string outputDirectory = @"C:\ConvertedEmails";
// Create output directory
Directory.CreateDirectory(outputDirectory);
// Get all EML and MSG files from input directory
var emailFiles = Directory.GetFiles(inputDirectory, "*.*")
.Where(file => file.EndsWith(".eml", StringComparison.OrdinalIgnoreCase) ||
file.EndsWith(".msg", StringComparison.OrdinalIgnoreCase))
.ToArray();
Console.WriteLine($"Found {emailFiles.Length} email files to convert");
// Set up output handler
var outputHandler = new FolderOutputHandler(outputDirectory);
// Process each file
var conversionTasks = emailFiles.Select(async filePath =>
{
try
{
using var inputStream = File.OpenRead(filePath);
string fileName = Path.GetFileName(filePath);
await Converter.ConvertToHtml(inputStream, fileName, outputHandler);
Console.WriteLine($"✓ Converted: {fileName}");
}
catch (Exception ex)
{
Console.WriteLine($"✗ Failed to convert {Path.GetFileName(filePath)}: {ex.Message}");
}
});
// Execute all conversions concurrently
await Task.WhenAll(conversionTasks);
Console.WriteLine("Batch conversion completed!");
}
catch (Exception ex)
{
Console.WriteLine($"Batch conversion failed: {ex.Message}");
}
}
}
Advanced Topics
Custom Output Naming
Create a custom output handler to control file naming:
public class TimestampOutputHandler : IOutputHandler
{
private readonly string _basePath;
public TimestampOutputHandler(string basePath)
{
_basePath = basePath;
Directory.CreateDirectory(_basePath);
}
public async Task AddOutputStream(string name, Func<Stream, Task> writeAction)
{
// Add timestamp prefix to the filename
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var nameWithoutExtension = Path.GetFileNameWithoutExtension(name);
var extension = ".html"; // Force HTML extension
var newFileName = $"{timestamp}_{nameWithoutExtension}{extension}";
var outputPath = Path.Combine(_basePath, newFileName);
using var fileStream = File.Create(outputPath);
await writeAction(fileStream);
Console.WriteLine($"Created: {newFileName}");
}
public void AddOutputStream(string name, Action<Stream> writeAction)
{
// Synchronous version
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var nameWithoutExtension = Path.GetFileNameWithoutExtension(name);
var extension = ".html";
var newFileName = $"{timestamp}_{nameWithoutExtension}{extension}";
var outputPath = Path.Combine(_basePath, newFileName);
using var fileStream = File.Create(outputPath);
writeAction(fileStream);
}
}
// Usage example:
var customHandler = new TimestampOutputHandler(@"C:\ConvertedEmails");
await Converter.ConvertToHtml(inputStream, fileName, customHandler);
Comprehensive Error Handling
Implement robust error handling for production environments:
public class RobustEmailConverter
{
public static async Task<ConversionResult> ConvertEmailToHtml(string inputPath, string outputDirectory)
{
var result = new ConversionResult();
try
{
// Validate input file exists
if (!File.Exists(inputPath))
{
throw new FileNotFoundException($"Input file not found: {inputPath}");
}
// Validate file extension
var extension = Path.GetExtension(inputPath).ToLower();
if (extension != ".eml" && extension != ".msg")
{
throw new ArgumentException($"Unsupported file format: {extension}");
}
// Create output directory
Directory.CreateDirectory(outputDirectory);
// Perform conversion
using var inputStream = File.OpenRead(inputPath);
var outputHandler = new FolderOutputHandler(outputDirectory);
string fileName = Path.GetFileName(inputPath);
await Converter.ConvertToHtml(inputStream, fileName, outputHandler);
result.Success = true;
result.OutputPath = Path.Combine(outputDirectory, Path.ChangeExtension(fileName, ".html"));
result.Message = "Conversion completed successfully";
}
catch (FileNotFoundException ex)
{
result.Success = false;
result.ErrorType = "FileNotFound";
result.Message = ex.Message;
}
catch (UnauthorizedAccessException ex)
{
result.Success = false;
result.ErrorType = "AccessDenied";
result.Message = $"Access denied: {ex.Message}";
}
catch (ArgumentException ex)
{
result.Success = false;
result.ErrorType = "InvalidArgument";
result.Message = ex.Message;
}
catch (Exception ex)
{
result.Success = false;
result.ErrorType = "UnknownError";
result.Message = $"Unexpected error: {ex.Message}";
}
return result;
}
}
public class ConversionResult
{
public bool Success { get; set; }
public string Message { get; set; }
public string ErrorType { get; set; }
public string OutputPath { get; set; }
}
Conclusion
The Aspose.Email LowCode Converter provides a streamlined solution for converting EML and MSG files to HTML format. Key benefits include:
- Simplified API: Minimal code required for complex conversions
- Batch Processing: Handle multiple files efficiently with concurrent operations
- Flexible Output: Custom handlers for specialized naming and storage requirements
- Robust Error Handling: Comprehensive exception management for production use
- Asynchronous Support: Non-blocking operations for better application performance
This approach enables developers to integrate email-to-HTML conversion seamlessly into web applications, document management systems, and email archiving solutions. The HTML output maintains the original email formatting while providing compatibility with modern web standards and responsive design frameworks.