Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active October 17, 2021 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-cloud/bb26f9aff1fa8ea51447a2c7f3cdfda3 to your computer and use it in GitHub Desktop.
Save aspose-cloud/bb26f9aff1fa8ea51447a2c7f3cdfda3 to your computer and use it in GitHub Desktop.
This gist contains code snippets related to conversion of EML files to MHT format using Aspose.Email Cloud SDK for .NET
This gist contains code snippets related to conversion of EML files to MHT format using Aspose.Email Cloud SDK for .NET
// For complete examples and data files, please go to
https://github.com/aspose-email-cloud/aspose-email-cloud-dotnet
// Get client credentials from https://dashboard.aspose.cloud/
string clientSecret = "d757548a9f2558c39c2feebdf85b4c44";
string clientID = "4db2f826-bf9c-42e7-8b2a-8cbca2d15553";
// create an instance of EmailCloud Api
var emailApi = new EmailCloud(clientSecret, clientID);
// source EML file name
string name = "sample.eml";
// name of resultant MHT file
string resultantFile = "converted.mht";
// format of input email file
string fromFormat = "eml";
// format of resultant file
string toFormat = "Mhtml";
try
{
// read the sourec EML to stream object
using (var file = System.IO.File.OpenRead(@"C:\Users\shahbnay\Downloads\" + name))
{
// create EmailConvert request instance passing input, output formats and file stream
var convertRequest = new EmailConvertRequest(fromFormat, toFormat, file);
// Perform the conversion operation
var response = emailApi.Email.Convert(convertRequest);
// print success message if conversion is successful
if (response != null && response.Equals("OK"))
{
// print the success message
Console.WriteLine("Successfully converted EML to MHT !");
Console.ReadKey();
}
// save the resultant file stream to local drive
saveToDisk(response, @"C:\Users\shahbnay\Downloads\" + resultantFile);
}
}catch (Exception ex)
{
// print the exception in console
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
// custom method to save the output to system drive
static void saveToDisk(Stream responseStream, String resultantFile)
{
var fileStream = File.Create(resultantFile);
responseStream.Seek(0, SeekOrigin.Begin);
responseStream.CopyTo(fileStream);
fileStream.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment