MapiMessage Properties

Hi team ,
we are trying to get all Mapi Message properties from MSG file ( subject , from , to , cc , body , date , …)
and save it on an xml document using this code :

//- Load the .MSG File
MapiMessage message = MapiMessage.FromFile(EmailFilePath);

//- Create an XML Document
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(“”);

//- Encode the XML Document in ASCII with his declaration
XmlDeclaration xmlDec;
xmlDec = xmlDocument.CreateXmlDeclaration(“1.0”, “UTF-8”, “yes”);

//- Create the “Properties” element in the XML Document
XmlElement xPropertiesElement = xmlDocument.CreateElement(“Properties”);
xmlDocument.DocumentElement.AppendChild(xPropertiesElement);

MapiPropertyCollection MapiProp = message.NamedProperties;

foreach (MapiNamedProperty namedProperty in MapiProp.Values)
{

string propertyValue = namedProperty.ToString();

XmlElement xElement = xmlDocument.CreateElement(“Property”);
xPropertiesElement.AppendChild(xElement);

XmlAttribute xmlAttributeName = xmlDocument.CreateAttribute(“Name”);
xmlAttributeName.Value = (namedProperty.Name);
xElement.Attributes.Append(xmlAttributeName);

XmlAttribute xmlAttributeValue = xmlDocument.CreateAttribute(“Value”);
xmlAttributeValue.Value = (propertyValue);
xElement.Attributes.Append(xmlAttributeValue);

}

but we don’t get the desired result:
we get something like this :

< Properties>
< Property Name=“__substg1.0_8000001F” Value=“exemple@gmail.com” />
< Property Name=“” Value=“False” />
< Property Name=“” Value=“0” />

< /Properties>
< /Email>
Is there a problem with our code ? or another way to do it ?
we have tried to do it with a similar code and it worked :
MapiPropertyCollection properties = mapiMsg.NamedProperties;

  foreach (MapiProperty mapiProp in properties.Values)
    {    
            XmlElement xSubjectElement = xmlDocument.CreateElement("Property");
            xPropertiesElement.AppendChild(xSubjectElement);

            XmlAttribute xmlAttributeSubjectN = xmlDocument.CreateAttribute("Name");
            xmlAttributeSubjectN.Value = ("Subject");
            xSubjectElement.Attributes.Append(xmlAttributeSubjectN);

            XmlAttribute xmlAttributeSubjectV = xmlDocument.CreateAttribute("Value");
            xmlAttributeSubjectV.Value = (mapiMsg.Subject);
            xSubjectElement.Attributes.Append(xmlAttributeSubjectV);
           // Same code for all properties which is not an optimized code

}

Could you please provide example code to get all mapi property at once

@ryuk,

Thank you for contacting Aspose support team.

Following sample code can be used to get all the properties like subject, body, from, to, cc and bcc etc. You may please use this code to retrieve the property value and save it in your required file format. It is not necessary that all the properties contain the entry in named property collection also. Please use MapiMessage.Properties to get all the properties.

MapiPropertyCollection properties = message.Properties;
foreach (MapiProperty mapiProp in properties.Values)
{
    MapiPropertyType type = (MapiPropertyType)mapiProp.DataType;
    if (type == MapiPropertyType.PT_UNICODE)
    {
        var mystring = Encoding.Unicode.GetString(mapiProp.Data);
        Console.WriteLine(mapiProp.PropertyTagName + ", Value = " + mystring);
    }
}

Thank you for your reply ,

I tried your sample code and it worked perfectly , thank you so much , but i still can’t get some properties like Sent time or Delivery Time,
on the same project i want to save the MSG file on EML but i’m getting “The given path’s format is not supported” error only for files that contain email conversations ,
code :
message.Save(FileAtt + “\” +SavedFileName+".eml", Aspose.Email.SaveOptions.DefaultEml);
Could you help me !

@ryuk,

Following sample code can be used to display the date and time properties.

I have checked with my own sample MSG files which are converted fine to EML using above sample code. Your sample MSG file is required to observe the issue here and provide assistance accordingly. Please share the sample MSG file with us for testing.

MapiPropertyCollection properties = message.Properties;
foreach (MapiProperty mapiProp in properties.Values)
{
    MapiPropertyType type = (MapiPropertyType)mapiProp.DataType;
    if (type == MapiPropertyType.PT_UNICODE)
    {
        var mystring = Encoding.Unicode.GetString(mapiProp.Data);
        Console.WriteLine(mapiProp.PropertyTagName + ", Value = " + mystring);
    }
    else if(type == MapiPropertyType.PT_SYSTIME)
    {
        Console.WriteLine(type + " ," + mapiProp.PropertyTagName + ", Value = " + DateTime.FromFileTime(BitConverter.ToInt64(mapiProp.Data,0)));
    }
}

Thank you for the sample code i will try it ,
infact when it’s a simple mail it works for me too but when it’s an email conversation it give me an error

Summary

MSG.zip

@ryuk,

Thank you for providing the sample message. I have tested it using following sample code with the latest version Aspose.Email for .NET 17.8.0. No issue is observed and MSG file is converted to EML successfully. Please give it a try and share the feedback.

MailMessage mail = MailMessage.Load(@"TR Fichiers import de masse documents ged.msg");
mail.Save(@"TR Fichiers import de masse documents ged.eml", SaveOptions.DefaultEml);

TR Fichiers import de masse documents ged.EML.zip (2.3 MB)

Thank you for replying ,
Infact , i’m loading the file as MapiMessage because i need it to get Mapi Properties ,
i tried to load the MSG file with this code :
var message= MapiMessage.FromMailMessage(Aspose.Email.MailMessage.Load(EmailFilePath));
but i’m still having the same issue with the EML conversion

MSG to EML conversion problem solved , but with your sample code for MapiProperties i’m getting special charactes like " &#x0 ; " or " & lt; "
Could you please help me with this issue ?

@ryuk,

I have checked the properties sample code with the sample message file provided by you. I am afraid that I could not find these special characters in the output. Please provide us the sample code along with the sample MSG file and snapshot showing these special characters. We will try to reproduce this issue and provide assistance accordingly.

Hi ,
Thank you for your help ,
i’m using this code :
MapiPropertyCollection properties = mapiMsg.Properties;

 foreach (MapiProperty mapiProp in properties.Values)
   {       
     MapiPropertyType type = (MapiPropertyType)mapiProp.DataType;
              
       if (type == MapiPropertyType.PT_UNICODE)
             {
               
              string mystring = Encoding.Unicode.GetString(mapiProp.Data);

                if(mapiProp.PropertyTagName!= "")
                {
                XmlElement xElement = xmlDocument.CreateElement("Property");
                xPropertiesElement.AppendChild(xElement);

                XmlAttribute xmlAttributeName = xmlDocument.CreateAttribute("Name");
                xmlAttributeName.Value = (mapiProp.PropertyTagName);
                xElement.Attributes.Append(xmlAttributeName);

                XmlAttribute xmlAttributeValue = xmlDocument.CreateAttribute("Value");
                xmlAttributeValue.Value = (mystring);
                xElement.Attributes.Append(xmlAttributeValue);
                } 
            }
   }

I’m using it with the same MSG file that i provide you , and i’m getting Something like this :
TR Fichiers import de masse documents ged.zip (10.0 KB)

@ryuk,

I have tested following sample code and observed no issue while writing data simply to a text file. You may please give a try to following sample code which generates correct file. There may be some issue with your code while writing to XML document, however that is beyond the scope of Aspose.Email. You may please adopt some other option to create XML file like creating XML file tags and values manually by writing text to file as mentioned in the above sample code. Output text file is attached here for your reference.

MapiMessage mapiMsg = MapiMessage.FromFile(@"TR Fichiers import de masse documents ged.msg");
MapiPropertyCollection properties = mapiMsg.Properties;
//XmlDocument xmlDocument = new XmlDocument();
System.IO.StreamWriter file = new System.IO.StreamWriter(@"output.txt");
foreach (MapiProperty mapiProp in properties.Values)
{
    MapiPropertyType type = (MapiPropertyType)mapiProp.DataType;

    if (type == MapiPropertyType.PT_UNICODE)
    {
        string mystring = Encoding.Unicode.GetString(mapiProp.Data);

        if (mapiProp.PropertyTagName != "")
        {
            file.WriteLine("Name = " + mapiProp.PropertyTagName + ", value = " + mystring);
        }
    }
}

output.zip (9.4 KB)