How to change the Sent date time format (C# .NET)

I use the following code to convert the msg file to Tiff image<span style=“font-size:10.0pt;
font-family:“Tahoma”,“sans-serif”;mso-fareast-font-family:“Times New Roman”;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”> and I want to change the format of the Sent date time from <span style=“font-size:10.0pt;
font-family:“Tahoma”,“sans-serif”;mso-fareast-font-family:“Times New Roman”;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>Sent: Fri, 29 Apr 2016 08:34:14 -0500 to Sent: Fri 29-Apr-2016
8:34 AM

VB Code:
Dim _msg As MailMessage = MailMessage.Load(FilNam_In, New MsgLoadOptions())
Dim msgStream As MemoryStream = Nothing
Try
msgStream = New MemoryStream()
_msg.Save(msgStream, Aspose.Email.Mail.SaveOptions.DefaultMhtml)
'msgStream.Position = 0
Dim msgDocument As Aspose.Words.Document = New Aspose.Words.Document(msgStream)
For Each section As Aspose.Words.Section In msgDocument
section.PageSetup.PaperSize = Aspose.Words.PaperSize.Letter
section.PageSetup.BottomMargin = 20
section.PageSetup.TopMargin = 20
section.PageSetup.LeftMargin = 20
section.PageSetup.RightMargin = 20
Next section
For Each table As Aspose.Words.Tables.Table In msgDocument.GetChildNodes(Aspose.Words.NodeType.Table, True)
table.AllowAutoFit = True
table.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto
Next table
Dim _saveoptions As Aspose.Words.Saving.ImageSaveOptions = New Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Tiff)
_saveoptions.ImageColorMode = Aspose.Words.Saving.ImageColorMode.None
_saveoptions.PixelFormat = Aspose.Words.Saving.ImagePixelFormat.Format16BppRgb555
_saveoptions.SaveFormat = Aspose.Words.SaveFormat.Tiff
_saveoptions.TempFolder = Path.GetTempPath
_saveoptions.TiffCompression = Aspose.Words.Saving.TiffCompression.Lzw
_saveoptions.Resolution = 300.0
_saveoptions.UseHighQualityRendering = True
_saveoptions.ColorMode = Aspose.Words.Saving.ColorMode.Normal
_saveoptions.DmlRenderingMode = Aspose.Words.Saving.DmlRenderingMode.DrawingML 'Aspose.Words.Saving.DmlRenderingMode.Fallback
_saveoptions.DmlEffectsRenderingMode = Aspose.Words.Saving.DmlEffectsRenderingMode.Fine
_saveoptions.PrettyFormat = False

msgDocument.Save(FilNam_Out, _saveoptions) 'Aspose.Words.SaveFormat.Tiff)

Hi Lisa,


Thank you for writing to Aspose Support team.

You need to use MhtMessageFormatter for formatting of date field as per your requirements. Please have a look at the following code sample for your reference and let us know if

MailMessage msg = MailMessage.Load(“snapshot.msg”, new MsgLoadOptions());

MhtMessageFormatter mhtlFormat = new MhtMessageFormatter();
mhtlFormat.DateTimeFormat = “dd/MMM/yyyy H:mm tt”;
mhtlFormat.Format(msg);
msg.Save(“snapshot_out.mhtml”, Aspose.Email.Mail.SaveOptions.DefaultMhtml);

I tested the code above and it nearly works.
The code produced double headers, but I found the code to hide extra print header.
But, the tiff image has the sent line with the following date. “2/12/2015 10:36” and does not have AM/PM.

My new code:
Dim _msg As MailMessage = MailMessage.Load(FilNam_In, New MsgLoadOptions())
Dim mhtlFormat As New MhtMessageFormatter()
mhtlFormat.DateTimeFormat = “mm/dd/yyyy H:mm tt”
mhtlFormat.Format(_msg)
Dim msgStream As MemoryStream = Nothing
Try
msgStream = New MemoryStream()
Dim options As New MhtSaveOptions()
options.MhtFormatOptions = MhtFormatOptions.HideExtraPrintHeader
options.MailMessageSaveType = MailMessageSaveType.MHtmlFormat
_msg.Save(msgStream, options)

Dim msgDocument As Aspose.Words.Document = New Aspose.Words.Document(msgStream)
options = Nothing : msgStream.Dispose() : mhtlFormat = Nothing


Whats wrong?

Hi Liza,


I have tested the following sample code and found no issue as PM is rendered in the output tiff file. You may please give a try to the following sample code using latest library and share the feedback.



MailMessage msg = MailMessage.Load(“snapshot.msg”, new MsgLoadOptions());


var mhtlFormat = new MhtMessageFormatter();

mhtlFormat.DateTimeFormat = “mm/dd/yyyy H:mm tt”;

mhtlFormat.Format(msg);

var msgStream = new MemoryStream();

var options = new MhtSaveOptions();

options.MhtFormatOptions = MhtFormatOptions.HideExtraPrintHeader;

options.MailMessageSaveType = MailMessageSaveType.MHtmlFormat;

msg.Save(“output.mht”, options);

msg.Save(msgStream, options);


var msgDocument = new Aspose.Words.Document(msgStream);

msgDocument.Save(“output.tiff”,SaveFormat.Tiff);

With the new code added, it is still not working.
By the way, the code given in the previous reply “<span style=“font-size:12.0pt;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:
EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>mhtlFormat.DateTimeFormat
= “mm/dd/yyyy H:mm tt”;”<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val=“–”/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

<![endif]–>
at least for vb.net the line should have been
<span style=“font-size:12.0pt;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:
EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>mhtlFormat.DateTimeFormat
= “MM/dd/yyyy H:mm tt”;



<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val=“–”/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

<![endif]–>
I have included a zip file that contains a text file with my complete procedure, and 2 screen captures. 1 of the mht file and 1 of the tiff image.

I am using the latest aspose dlls as of Feb 13 2017

Help

Hi,


We used the DateTimeFormat as ddd dd-MMM-yyyy H:mm tt and it worked well at our end. Please have a look at the attached files and following code sample for your kind reference and let us know your feedback.

Sample Code:

MailMessage msg = MailMessage.Load(“806082\About Aspose.msg”, new MsgLoadOptions());

MhtMessageFormatter mhtlFormat = new MhtMessageFormatter();
mhtlFormat.DateTimeFormat = “ddd dd-MMM-yyyy H:mm tt”;
mhtlFormat.Format(msg);
MhtSaveOptions saveOptions = new MhtSaveOptions();
saveOptions.MhtFormatOptions = MhtFormatOptions.HideExtraPrintHeader;
saveOptions.MailMessageSaveType = MailMessageSaveType.MHtmlFormat;
msg.Save(“806082\About Aspose_out.mhtml”, saveOptions);

Document doc = new Document(“806082\About Aspose_out.mhtml”);
doc.Save(“806082\out.pdf”, Aspose.Words.SaveFormat.Pdf);
doc.Save(“806082\out.tiff”, Aspose.Words.SaveFormat.Tiff);

Applied the change. Still not correct. see attached jpg

I tracked down the problem. For future reference, this is NOT your problem.

It was a problem on my development machine. In the control panel->Region and Language, my AM and PM was clear.

See attached.

Thanks for your help and patience

Hi,


We were about to ask you about the same as that could be the only reason for the difference in output for the same code. Please feel free to write to us if you have any further query in this regard.

Hi

I am using Aspose Email .Net 19.4.0.0

I do not see the MhtMessageFormatter. I am in need of changing the format of my email Sent date. Has this been replaced or am I missing something?

Thanks,
Charles

@ccuster68,

Can you please provide the sample code where you are trying to apply formatting so that we may investigate that further on our end.

@ccuster68,

public static class Aspose.Email.Mail.MthTemplateName - Defines well known names of headers for Mhtml formatting.

        string file = Path.Combine(TestUtil.GetTestPath(), "Test email.msg");

        MailMessage eml = MailMessage.Load(file, new MsgLoadOptions());

        MhtSaveOptions mhtSaveOptions= new MhtSaveOptions();

        mhtSaveOptions.FormatTemplates[MthTemplateName.DateTime] = "HH:mm:ss";

        mhtSaveOptions.FormatTemplates[MthTemplateName.From] = @"<span class='headerLineTitle'>From:</span><span class='headerLineText'>{0}/span><br/>";                         

        mhtSaveOptions.FormatTemplates[MthTemplateName.Sent] = @"<span class='headerLineTitle'>Sent:</span><span class='headerLineText'>{0}</span><br/>";

        mhtSaveOptions.FormatTemplates[MthTemplateName.To] = @"<span class='headerLineTitle'>To:</span><span class='headerLineText'>{0}</span><br/>";

        mhtSaveOptions.FormatTemplates[MthTemplateName.Cc] = @"<span class='headerLineTitle'>Cc:</span><span class='headerLineText'>{0}</span><br/>";

        eml.Save(ms, saveOptions);
1 Like

Thanks @mudassir.fayyaz That is the info I needed.

@ccuster68m,

I am pleased to hear that shared information served your purpose.