Error while converting .eml to msg

Hello all,

i posted about this problem a view weeks ago and got no solution until now.

We try to convert a eml-forrmatted mail from stream into a msg-forrmatted mail and save it to an other stream with Aspose.Network.

The code below works fine when running on Microsoft Windows XP and it fails when running on Microsoft Windows Server 2003 R2,

standard x64 edition, build number 3790, service pack 2.

We use Aspose.Network for .NET 2.0 version 4.6.0.1 (evaluation version with evaluation license) and .net framework v2.0.50727.

We try to convert the email format in two different ways and in both cases the code below fails at the underlined line with a null pointer

exception while the input parameters were valid object references.

This error occurs when executing the code in context of a web application within IIS 6 as well as when executing it in a Windows Forms application.

Anyone can help me?

Best Regards

Volker Roeser

public static Stream ConvertMailFormat(Stream pStream, MessageFormat pInputFormat, MessageFormat pOutputFormat)
{
    Stream stream = null;

    try
    {
        pStream.Position = 0;
        MailMessage mailMessage = MailMessage.Load(pStream, pInputFormat);
        stream = new MemoryStream();

        if (pInputFormat.ToString().Equals("Eml")
            && pOutputFormat.ToString().Equals("Msg"))
        {
            MapiMessage mapiMessage = MapiMessage.FromMailMessage(mailMessage);
            mapiMessage.Save(stream);
        }
        else if (pInputFormat.ToString().Equals("Msg")
            && pOutputFormat.ToString().Equals("Eml"))
        {
            mailMessage.Save(stream, pOutputFormat);
        }

        stream.Position = 0;
    }
    catch (Exception)
    {
        throw;
    }

    return stream;
}

public static Stream ConvertMailFormat(Stream pStream, MessageFormat pInputFormat, MessageFormat pOutputFormat)
{
    Stream stream = null;

    try
    {
        pStream.Position = 0;
        MailMessage mailMessage = MailMessage.Load(pStream, pInputFormat);

        stream = new MemoryStream();
        mailMessage.Save(stream, pOutputFormat);
        stream.Position = 0;
    }
    catch (Exception)
    {
        throw;
    }

    return stream;
}

Hi Volker,

Thanks for considering Aspose.

We just released Aspose.Network v 4.7 which contains fixes for eml to msg conversion in 64-bit Windows. Please download from http://www.aspose.com/community/files/51/file-format-components/aspose.network-for-.net/entry175281.aspx and try with the new dlls.

I just tested your sample code with it in 64-bit Windows and it worked fine. If you still have issues, please copy the exception message and stack trace here.

We still got a problem with conversion of mail messages. With the code below we try to convert a msg formatted mail to RFC 822 format and back to msg format. Now the code which works with Aspose.Network 4.6.0.1 doesn’t work any more with version 4.7.0.8. The code fails at the underlined line with the following error description. The error occurs with any msg formatted mail on 32 bit and 64 bit windows platforms.

Best Regards

Volker Roeser

bei System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
bei System.String.Substring(Int32 startIndex, Int32 length)
bei Aspose.Network.Outlook.MapiMessage.xbe8fa87f38748b7c(MapiAttachmentCollection xb8b986a36a564367, MapiPropertyStream xebd3f90832eab048, Attachment xdd361a2e023253e5, OutlookMessageFormat x5786461d089b10a0)
bei Aspose.Network.Outlook.MapiMessage.xaa71394a9a765151(MailMessage x1f25abf5fb75e795, MapiMessage xa773d6b986f6bdbb)
bei Aspose.Network.Outlook.MapiMessage.FromMailMessage(MailMessage message)
bei Aspose.Network.Mail.MailMessage.Save(Stream stream, MessageFormat format)
bei WindowsApplication2.Form1.ConvertEmailFormat(Stream pStream, MessageFormat pInputFormat, MessageFormat pOutputFormat)
private void button1_Click(object sender, EventArgs eventArgs)
{
    OpenFileDialog dlg = null;

    try
    {
        dlg = new OpenFileDialog();
        dlg.Multiselect = false;
        dlg.Filter = "Outlook-Nachrichtenformat (*.msg)|*.msg|RFC 822 Format (*.eml)|*.eml|"
            + "Alle Dateien (*.*)|*.*";

        if (dlg.ShowDialog() != DialogResult.OK)
            return;

        textBoxFilename.Text = dlg.FileName;

        MailMessage mailMessage = null;

        if (Path.GetExtension(dlg.FileName).ToLower().Equals(".msg"))
            mailMessage = MailMessage.Load(dlg.OpenFile(), MessageFormat.Msg);
        else
            mailMessage = MailMessage.Load(dlg.OpenFile());

        Stream stream = new MemoryStream();
        mailMessage.Save(stream);
        stream.Position = 0;
        //... Save the RFC 822 formatted mail to a stream wich will be saved into database

        //... load the stream from database and change the mail format back to msg format


        stream = ConvertEmailFormat(stream, MessageFormat.Eml, MessageFormat.Msg);

    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
}

private Stream ConvertEmailFormat(Stream pStream, MessageFormat pInputFormat, MessageFormat pOutputFormat)
{
    Stream stream = null;

    try
    {
        pStream.Position = 0;
        MailMessage mailMessage = MailMessage.Load(pStream, pInputFormat);

        stream = new MemoryStream();
        mailMessage.Save(stream, pOutputFormat);
        stream.Position = 0;
    }
    catch (Exception e)
    {
        throw new MailArchiveException(string.Format("Fehler beim Umwandeln der E-Mail in das"
            + "{0}-Dateiformat", pOutputFormat.ToString()), e);
    }

    return stream;
}

Hello,

Could you please provide the eml files that have this problem? We need to use them to locate the root of the crash.

Thank you very much.

As i wrot in my prior post: (…) we try to convert a msg formatted mail to RFC 822 format and back to msg format. (…). The error occurs with any msg formatted mail on 32 bit and 64 bit windows platforms.

If you run the code i provided with the previous post you should always run with any msg formatted mail in the error i described. (I attach a simple msg file which you can use for testing)

Best Regards

Volker Roeser

Die Länge darf nicht kleiner als 0 (null) sein. Parametername: length

bei System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
bei System.String.Substring(Int32 startIndex, Int32 length)
bei Aspose.Network.Outlook.MapiMessage.xbe8fa87f38748b7c(MapiAttachmentCollection xb8b986a36a564367, MapiPropertyStream xebd3f90832eab048, Attachment xdd361a2e023253e5, OutlookMessageFormat x5786461d089b10a0)
bei Aspose.Network.Outlook.MapiMessage.xaa71394a9a765151(MailMessage x1f25abf5fb75e795, MapiMessage xa773d6b986f6bdbb)
bei Aspose.Network.Outlook.MapiMessage.FromMailMessage(MailMessage message)
bei Aspose.Network.Mail.MailMessage.Save(Stream stream, MessageFormat format)
bei WindowsApplication2.Form1.ConvertEmailFormat(Stream pStream, MessageFormat pInputFormat, MessageFormat pOutputFormat)

Hello,

We have released a hotfix to resolve this problem. Please download it here:

http://www.aspose.com/community/files/51/file-format-components/aspose.network-for-.net/entry179958.aspx
Thanks