使用 PropertyAccessor.SetProperty 方法设置 SentOn Outlook MailItem 属性 时遇到问题

Troubles setting SentOn Outlook MailItem property with PropertyAccessor.SetProperty method

目前我正在努力将 System.Net.Mail.MailMessage object 转换为 Microsoft.Office.Interop.Outlook.MailItem。 一切似乎都工作正常,但我需要帮助通过 PropertyAccessor.SetProperty() 方法设置 SentOn MailItem 属性。

我在 MailMessage header 中读取了发送日期信息,returns 一个字符串 object,然后我将它转换为 DateTime,最后我使用 SetProperty( ) 方法。

这是我的代码:

MailMessage mMessage= MailMessageMimeParser.ParseMessage(emlFilePath);
eMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
//  here I set 'Subject', 'To', 'CC', 'BCC' etc. properties...
// then try to set the 'SentOn' property
string sentOnString = mMessage.Headers["Date"]; // Wed, 27 May 2015 10:54:39 +0200
DateTime sentOnDateTime = DateTime.SpecifyKind(DateTime.Parse(sentOnString), DateTimeKind.Local); // 5/27/2015 10:54:39 AM
string PR_CLIENT_SUBMIT_TIME = "http://schemas.microsoft.com/mapi/proptag/0x00390040";
eMail.PropertyAccessor.SetProperty(PR_CLIENT_SUBMIT_TIME, sentOnDateTime);
eMail.Save(); // here the SentOn property is  5/27/2017 12:54:39 PM
DateTime date = (DateTime)eMail.PropertyAccessor.GetProperty(PR_CLIENT_SUBMIT_TIME); // 5/27/2015 10:54:39 AM
...
return eMail; // here the SentOn property is  5/27/2017 12:54:39 PM

正如我的代码注释所建议的那样,当我使用 PropertyAccessor.GetProperty() 方法,但是如果我尝试从 eMail.SentOn 属性 获取它,那么我会得到一个错误的日期值 (5/27/2017 12:54:39 PM).

我也曾尝试使用此指令 DateTime sentOnDateTime = DateTime.Parse("Wed, 27 May 2015 10:54:39"); 创建 sentOnDateTime DateTime,但结果没有改变。

你有什么建议?有什么提示吗?谢谢。

尝试使用Utc or Unspecified value for the second parameter of the SpecifyKind方法。

  DateTime sentOnDateTime = DateTime.SpecifyKind(DateTime.Parse(sentOnString), DateTimeKind.Utc);

使用 DateTimeKind.Local 并不重要 - 日期时间值中没有任何内容本身就是本地或 UTC。 SetProperty 看到的只是一个 8 字节的浮点值,用于抑制 COM 中的 DateTime。

MAPI 在 UTC 时区中存储大多数 PT_SYSTIME 属性,这就是您需要传递给 SetProperty 的内容。 SentOn 属性 在您阅读时将 UTC 转换为本地时间。

附带说明一下,更大的问题是 Sent 属性 - OOM 不会让您设置它,因此您需要创建一个 post 项目而不是邮件项,然后将 MessageClass 改回 "IPM.Note" 并删除 PR_ICON_INDEX.

如果使用Redemption is an option (I am its author), it will let you import MIME files using RDIOMail.Import方法:

  set Session = CreateObject("Redemption.RDOSession")
  Session.MAPIOBJECT = Application.Session.MAPIOBJECT
  set Msg = Session.GetDefaultFolder(olFolderInbox).Items.Add
  Msg.Sent = true
  Msg.Import "C:\temp\test.eml", 1024
  Msg.Save