We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Incorrect time in email body header

Hi,

Our customer has an Exchange 2010 and when attempting to create an email body with header of any email send or received on their exchange server, the date received is wrong. It shows the UTC time instead of Local time when generating the body with header. When downloading the message to msg and opening it in outlook it shows the correct date, but again when processing this msg to generate the email body the date received is UTC instead of Local. How can this be resolved?

I attached a sample .msg and resulting email body pdf.

The code I used:

private static void GenerateEmailBody()
{
string msgPath = @“C:\temp\1.msg”;

        var loadOptions = new MsgLoadOptions();
        var msg = MailMessage.Load(msgPath, loadOptions);

        using (var ms = new MemoryStream())
        {
            MhtSaveOptions saveOptions = new MhtSaveOptions();
            saveOptions.MhtFormatOptions = (true ? MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteBccEmailAddress | MhtFormatOptions.WriteCompleteCcEmailAddress
                | MhtFormatOptions.WriteCompleteEmailAddress | MhtFormatOptions.WriteCompleteFromEmailAddress | MhtFormatOptions.WriteCompleteToEmailAddress : MhtFormatOptions.None);
            saveOptions.CheckBodyContentEncoding = true;
            msg.Save(ms, saveOptions);
            GeneratePdfFromMhtmlStream(ms, Path.ChangeExtension(msgPath, ".pdf"));
        }
    }

    private static void GeneratePdfFromMhtmlStream(MemoryStream ms, string destFile)
    {
        //create an instance of LoadOptions and set the LoadFormat to Mhtml
        var loadOptions = new Aspose.Words.LoadOptions();
        loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;

        //create an instance of Document and load the MTHML from MemoryStream
        var doc = new Aspose.Words.Document(ms, loadOptions);
        doc.UpdateTableLayout();

        var pdfOptions = new Aspose.Words.Saving.PdfSaveOptions();

        pdfOptions.UseHighQualityRendering = true;            

        //Format the document - smaller margins
        foreach (Aspose.Words.Section section in doc)
        {
            section.PageSetup.LeftMargin = 20;
            section.PageSetup.RightMargin = 20;
            section.PageSetup.TopMargin = 20;
            section.PageSetup.BottomMargin = 20;
            section.PageSetup.HeaderDistance = 10;
            section.PageSetup.FooterDistance = 10;
        }

        //Prevent Tables from being cut off if the are too wide to fit to the page 
        foreach (Aspose.Words.Tables.Table tableNode in doc.GetChildNodes(Aspose.Words.NodeType.Table, true))
        {
            tableNode.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPercent(100);
        }

        //Prevent embedded images from being cut off if the are too wide to fit to the page
        foreach (Aspose.Words.Drawing.Shape shape in doc.GetChildNodes(Aspose.Words.NodeType.Shape, true))
        {
            if (shape.ShapeType == Aspose.Words.Drawing.ShapeType.Image)
            {
                if (doc.Sections[0].PageSetup.PageWidth < shape.Width)
                {

                    double scale = (doc.Sections[0].PageSetup.PageWidth - 40) / shape.Width;
                    shape.Width = doc.Sections[0].PageSetup.PageWidth - 40;
                    shape.Height = shape.Height * scale;
                }
            }
        }

        doc.Save(Path.ChangeExtension(destFile, ".pdf"), pdfOptions);            
    }
}<a class="attachment" href="/uploads/discourse_instance3/6078">1.pdf</a> (83.1 KB)

sample.zip (83.7 KB)

@ikke1234,

Thank you for contacting Aspose support team.

Please set the TimeZoneOffset property of the message as shown in the following sample code and share the feedback.

string msgPath = "./1.msg";

var loadOptions = new MsgLoadOptions();
var msg = MailMessage.Load(msgPath, loadOptions);

using (var ms = new MemoryStream())
{
    MhtSaveOptions saveOptions = new MhtSaveOptions();
    saveOptions.MhtFormatOptions = (true ? MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteBccEmailAddress | MhtFormatOptions.WriteCompleteCcEmailAddress
        | MhtFormatOptions.WriteCompleteEmailAddress | MhtFormatOptions.WriteCompleteFromEmailAddress | MhtFormatOptions.WriteCompleteToEmailAddress : MhtFormatOptions.None);
    saveOptions.CheckBodyContentEncoding = true;

    //ADD FOLLOWING LINE OF CODE
    msg.TimeZoneOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);

    msg.Save(ms, saveOptions);
    GeneratePdfFromMhtmlStream(ms, Path.ChangeExtension(msgPath, ".pdf"));
}

Hi Kashif,

That solves the issue

Thanks

@ikke1234,

Thank you for the feedback and feel free to write us back if you have any other query in this regard.