使用PropertyAccessor.SetProperty方法设置SentOn Outlook MailItem属性的麻烦

时间:2015-05-28 10:05:29

标签: c# email datetime outlook

目前我正在努力转换System.Net.Mail.MailMessageMicrosoft.Office.Interop.Outlook.MailItem对象。 一切似乎都运行良好,但我需要帮助通过SentOn方法设置PropertyAccessor.SetProperty() MailItem属性。

我在MailMessage标题内读取了发送日期信息,它返回一个字符串对象,然后我将其转换为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()方法获取时,正确/良好的日期值(2015年5月27日10:54:39 AM)似乎存储在eMail项目中,但是如果我试图从eMail.SentOn属性获取它,那么我得到一个错误的日期值(5/27/2017 12:54:39 PM)。

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

你有什么建议?任何提示?谢谢。

2 个答案:

答案 0 :(得分:1)

如果使用DateTimeKind.Local并不重要 - 日期时间值中没有任何内容使其本身或UTC。所有SetProperty看到的是一个8字节的浮点值,在COM中压制DateTime。

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

作为旁注,更大的问题是Sent属性--OOM不会让你设置它,所以你需要创建一个post项而不是mail项,然后将MessageClass改回" IPM 。注意"并删除PR_ICON_INDEX。

如果使用Redemption是一个选项,它将允许您使用RDIOMail导入MIME文件。导入方法:

  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

答案 1 :(得分:0)

尝试将UtcUnspecified值用于SpecifyKind方法的第二个参数。

$