Convert MSG files to EML format issue

We’re currently using the Aspose library (version 1.17.158) to convert MSG files to EML format, but we’ve encountered an issue during the conversion process.

Details:
Aspose Version: 1.17.158
Operation: MSG → EML conversion
Error: Object reference not set to an instance of an object
Code:

var mapi = MapiMessage.Load(“E:\zeva\Test 2.msg”);
var ms = new MemoryStream();
mapi.Save(ms, SaveOptions.DefaultEml);

Could you please help us identify the cause and provide guidance on how to resolve it?

Thank you for your assistance.

Sample file attached.
test.zip (5.8 KB)

@bahakh

The error “Object reference not set to an instance of an object” typically indicates that there is a null reference in your code. This can occur if the MapiMessage.Load method fails to load the MSG file correctly, possibly due to an invalid file path or a corrupted file.

Here are several steps to troubleshoot and resolve the issue:

  1. Check File Path: Ensure that the file path “E:\zeva\Test 2.msg” is correct and that the file exists at that location.

  2. File Integrity: Verify that the MSG file is not corrupted. Try opening it in an email client to ensure it can be read properly.

  3. Update Aspose Library: The version you are using (1.17.158) is quite old. Consider updating to the latest version of the Aspose.Email library, as there may have been bug fixes and improvements since your version.

  4. Error Handling: Implement error handling to catch exceptions and provide more information about what might be going wrong. For example:

    try
    {
        var mapi = MapiMessage.Load("E:\\zeva\\Test 2.msg");
        using (var ms = new MemoryStream())
        {
            mapi.Save(ms, SaveOptions.DefaultEml);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
    
  5. Documentation: Refer to the Aspose.Email documentation for any specific requirements or limitations regarding MSG to EML conversion.

If the issue persists after these checks, consider reaching out to the Aspose support forum for more targeted assistance.

I apologize, the correct version is: 25.9.0

We are aware of the solutions you listed. But can we have a human look into this.

Hello @bahakh,

thank you for the report and for attaching the sample.

We were able to reproduce the exception with your sample file, and we’ve opened an internal ticket to fix the issue in the next release.

Issue ID(s): EMAILNET-41649

In the sample you sent there is a MAPI named property with the name id “content-type” whose string value is null. During the MSG → EML conversion code path the lib attempts to read that named property as if it contained a valid string.

As a temporary workaround you can remove the problematic named property before saving to EML. The workaround removes that specific named property when its string value is null. Removing a null content-type property is safe: a null content-type is unusable for EML.

var mapi = MapiMessage.Load(@"test.msg");
            
if (mapi.NamedProperties != null)
{
    foreach (var namedProperty in mapi.NamedProperties.Values)
    {
        if (((MapiNamedProperty)namedProperty).NameId == "content-type" && namedProperty.GetString() == null)
        {
            mapi.Properties.Remove(namedProperty.Tag);
        }
    }
}
            
var ms = new MemoryStream();
mapi.Save(ms, SaveOptions.DefaultEml);