是否可以使用具有不同 sent/received 日期的 Microsoft Graph 创建消息?
Is it possible to create a message using Microsoft Graph with a different sent/received date?
我正在开发一个应用程序,该应用程序需要能够将消息插入具有特定日期的 O365 邮箱(类似于邮件迁移)。我创建了一个使用 IMAP 和 MailKit 的版本,这是在消息对象上设置日期 属性 的简单问题:
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress(NameGenerator.AnyName(), EmailAddressGenerator.AnyEmailAddress()));
message.To.Add(new MailboxAddress(m_O365UserID));
message.Subject = StringGenerator.AnyStringOfSizeAndCase(NumberGenerator.RandomNumberBetween(20, 100), CaseType.TitleCase);
BodyBuilder builder = new BodyBuilder
{
HtmlBody = LipsumGenerator.GenerateHtml(NumberGenerator.RandomNumberBetween(3, 10))
};
message.Body = builder.ToMessageBody();
DateTime t = DateTimeGenerator.AnyDateBetween(m_startDate, DateTime.Now);
t = t.Add(DateTimeGenerator.AnyTime());
DateTimeOffset dto = new DateTimeOffset(t);
message.Date = dto;
我过去也可以使用 EWS 做到这一点,但我需要设置一些扩展属性才能做到这一点,如下所示:
ExtendedPropertyDefinition deliveryTime = new Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0E06, MapiPropertyType.SystemTime);
ExtendedPropertyDefinition clientSubmitTime = new Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0039, MapiPropertyType.SystemTime);
ExtendedPropertyDefinition flags = new Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
EmailMessage m = new EmailMessage(m_exchangeService);
m.From = EmailAddressGenerator.AnyEmailAddress();
m.ToRecipients.Add(m_emailAddress);
DateTime t = DateTimeGenerator.AnyDateBetween(startDate, DateTime.Now);
t = t.Add(DateTimeGenerator.AnyTime());
m.SetExtendedProperty(deliveryTime, t);
m.SetExtendedProperty(clientSubmitTime, t);
m.SetExtendedProperty(flags, 1);
这两种方法都可以将消息回溯到我需要的任何时间点。在这种情况下,我只是用测试数据填充邮箱以验证 API 调用。尝试像这样在图表中做同样的事情:
Microsoft.Graph.Message message = new Microsoft.Graph.Message();
message.From = new Recipient { EmailAddress = new EmailAddress { Address = EmailAddressGenerator.AnyEmailAddress(), Name = NameGenerator.AnyName() } };
message.ToRecipients = new List<Recipient>();
message.ToRecipients.Append(new Recipient { EmailAddress = new EmailAddress { Address = m_O365UserID } });
message.Subject = StringGenerator.AnyStringOfSizeAndCase(NumberGenerator.RandomNumberBetween(20, 100), CaseType.TitleCase);
DateTime t = DateTimeGenerator.AnyDateBetween(m_startDate, DateTime.Now);
t = t.Add(DateTimeGenerator.AnyTime());
DateTimeOffset dto = new DateTimeOffset(t);
message.ReceivedDateTime = dto;
message.SentDateTime = dto;
message.CreatedDateTime = dto;
message.LastModifiedDateTime = dto;
为消息添加时间戳,截至提交时间。我想我需要在消息上设置与 EWS 相同的 Mapi 属性,但到目前为止我还没有找到这样做的方法。我查看了概述的扩展属性 here, which says they can be referenced by a type and MAPI property tag. This page 说可以在新对象上创建扩展属性:
To create one or more extended properties in a new resource instance, use the same REST request as creating the instance, and include the properties of the new resource instance and extended property in the request body.
我这样试过:
Dictionary<string, object> extendedProperties = new Dictionary<string, object>();
extendedProperties.Add("SystemTime 0x0E06",dto.DateTime);
extendedProperties.Add("SystemTime 0x0039", dto.DateTime);
extendedProperties.Add("Integer 0x3591",1);
message.Body = new ItemBody { ContentType = BodyType.Html, Content = LipsumGenerator.GenerateHtml(NumberGenerator.RandomNumberBetween(3, 10)), AdditionalData=extendedProperties};
抛出异常:
The property 'SystemTime 0x0E06' does not exist on type 'Microsoft.OutlookServices.ItemBody'. Make sure to only use property names that are defined by the type or mark the type as open type.
我也无法直接在 message.SingleValueExtendedProperties 上创建任何内容,如果我省略 SystemTime 并尝试将 属性 设置为十六进制值,则会发生同样的异常。我希望能够为此应用程序支持 Graph - 有没有任何人知道用自定义 send/receive 日期创建消息的方法?如果我能够在 EWS 中做到这一点,我希望较新的 API 应该能够做到同样的事情。
编辑
@Glen 的回答有效。我尝试了同样的事情,但仍然有我试图添加到消息正文中的 属性 的第二个定义,这实际上是导致异常的原因。要添加到他的答案中的唯一另一件事是 属性 的时间格式需要采用非常特定的格式,因此您可以将标准 DateTimeOffset 格式化为 mapi 兼容时间,如下所示:
string mapiTime = $"{dto.UtcDateTime.Year}-{dto.UtcDateTime.Month.ToString("D2")}-{dto.UtcDateTime.Day.ToString("D2")}T{dto.UtcDateTime.TimeOfDay.ToString()}.0{dto.Offset.Hours.ToString("D2")}:00";
message.SingleValueExtendedProperties = new MessageSingleValueExtendedPropertiesCollectionPage()
{
new SingleValueLegacyExtendedProperty {Id = "Integer 0x0E07",Value = "1" },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0039", Value = mapiTime },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0E06",Value = mapiTime }
};
你应该可以做类似的事情
Microsoft.Graph.Message message = new Microsoft.Graph.Message();
message.From = new Recipient { EmailAddress = new EmailAddress { Address = "blah@blah.com", Name = "blah" } };
message.ToRecipients = new List<Recipient>() { new Recipient { EmailAddress = new EmailAddress { Address = "ToBalh@blah.com" } } };
message.Subject = "Blah";
message.SingleValueExtendedProperties = new MessageSingleValueExtendedPropertiesCollectionPage()
{
new SingleValueLegacyExtendedProperty {Id = "Integer 0x0E07",Value = "1" },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0039", Value = "2020-05-12T10:10:47.2048+10:00" },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0E06",Value = "2020-05-12T10:10:47.2048+10:00" }
};
var SaveMessage = graphClient.Me.MailFolders["Inbox"].Messages.Request().AddAsync(message).Result;
适合我
我正在开发一个应用程序,该应用程序需要能够将消息插入具有特定日期的 O365 邮箱(类似于邮件迁移)。我创建了一个使用 IMAP 和 MailKit 的版本,这是在消息对象上设置日期 属性 的简单问题:
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress(NameGenerator.AnyName(), EmailAddressGenerator.AnyEmailAddress()));
message.To.Add(new MailboxAddress(m_O365UserID));
message.Subject = StringGenerator.AnyStringOfSizeAndCase(NumberGenerator.RandomNumberBetween(20, 100), CaseType.TitleCase);
BodyBuilder builder = new BodyBuilder
{
HtmlBody = LipsumGenerator.GenerateHtml(NumberGenerator.RandomNumberBetween(3, 10))
};
message.Body = builder.ToMessageBody();
DateTime t = DateTimeGenerator.AnyDateBetween(m_startDate, DateTime.Now);
t = t.Add(DateTimeGenerator.AnyTime());
DateTimeOffset dto = new DateTimeOffset(t);
message.Date = dto;
我过去也可以使用 EWS 做到这一点,但我需要设置一些扩展属性才能做到这一点,如下所示:
ExtendedPropertyDefinition deliveryTime = new Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0E06, MapiPropertyType.SystemTime);
ExtendedPropertyDefinition clientSubmitTime = new Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0039, MapiPropertyType.SystemTime);
ExtendedPropertyDefinition flags = new Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
EmailMessage m = new EmailMessage(m_exchangeService);
m.From = EmailAddressGenerator.AnyEmailAddress();
m.ToRecipients.Add(m_emailAddress);
DateTime t = DateTimeGenerator.AnyDateBetween(startDate, DateTime.Now);
t = t.Add(DateTimeGenerator.AnyTime());
m.SetExtendedProperty(deliveryTime, t);
m.SetExtendedProperty(clientSubmitTime, t);
m.SetExtendedProperty(flags, 1);
这两种方法都可以将消息回溯到我需要的任何时间点。在这种情况下,我只是用测试数据填充邮箱以验证 API 调用。尝试像这样在图表中做同样的事情:
Microsoft.Graph.Message message = new Microsoft.Graph.Message();
message.From = new Recipient { EmailAddress = new EmailAddress { Address = EmailAddressGenerator.AnyEmailAddress(), Name = NameGenerator.AnyName() } };
message.ToRecipients = new List<Recipient>();
message.ToRecipients.Append(new Recipient { EmailAddress = new EmailAddress { Address = m_O365UserID } });
message.Subject = StringGenerator.AnyStringOfSizeAndCase(NumberGenerator.RandomNumberBetween(20, 100), CaseType.TitleCase);
DateTime t = DateTimeGenerator.AnyDateBetween(m_startDate, DateTime.Now);
t = t.Add(DateTimeGenerator.AnyTime());
DateTimeOffset dto = new DateTimeOffset(t);
message.ReceivedDateTime = dto;
message.SentDateTime = dto;
message.CreatedDateTime = dto;
message.LastModifiedDateTime = dto;
为消息添加时间戳,截至提交时间。我想我需要在消息上设置与 EWS 相同的 Mapi 属性,但到目前为止我还没有找到这样做的方法。我查看了概述的扩展属性 here, which says they can be referenced by a type and MAPI property tag. This page 说可以在新对象上创建扩展属性:
To create one or more extended properties in a new resource instance, use the same REST request as creating the instance, and include the properties of the new resource instance and extended property in the request body.
我这样试过:
Dictionary<string, object> extendedProperties = new Dictionary<string, object>();
extendedProperties.Add("SystemTime 0x0E06",dto.DateTime);
extendedProperties.Add("SystemTime 0x0039", dto.DateTime);
extendedProperties.Add("Integer 0x3591",1);
message.Body = new ItemBody { ContentType = BodyType.Html, Content = LipsumGenerator.GenerateHtml(NumberGenerator.RandomNumberBetween(3, 10)), AdditionalData=extendedProperties};
抛出异常:
The property 'SystemTime 0x0E06' does not exist on type 'Microsoft.OutlookServices.ItemBody'. Make sure to only use property names that are defined by the type or mark the type as open type.
我也无法直接在 message.SingleValueExtendedProperties 上创建任何内容,如果我省略 SystemTime 并尝试将 属性 设置为十六进制值,则会发生同样的异常。我希望能够为此应用程序支持 Graph - 有没有任何人知道用自定义 send/receive 日期创建消息的方法?如果我能够在 EWS 中做到这一点,我希望较新的 API 应该能够做到同样的事情。
编辑 @Glen 的回答有效。我尝试了同样的事情,但仍然有我试图添加到消息正文中的 属性 的第二个定义,这实际上是导致异常的原因。要添加到他的答案中的唯一另一件事是 属性 的时间格式需要采用非常特定的格式,因此您可以将标准 DateTimeOffset 格式化为 mapi 兼容时间,如下所示:
string mapiTime = $"{dto.UtcDateTime.Year}-{dto.UtcDateTime.Month.ToString("D2")}-{dto.UtcDateTime.Day.ToString("D2")}T{dto.UtcDateTime.TimeOfDay.ToString()}.0{dto.Offset.Hours.ToString("D2")}:00";
message.SingleValueExtendedProperties = new MessageSingleValueExtendedPropertiesCollectionPage()
{
new SingleValueLegacyExtendedProperty {Id = "Integer 0x0E07",Value = "1" },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0039", Value = mapiTime },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0E06",Value = mapiTime }
};
你应该可以做类似的事情
Microsoft.Graph.Message message = new Microsoft.Graph.Message();
message.From = new Recipient { EmailAddress = new EmailAddress { Address = "blah@blah.com", Name = "blah" } };
message.ToRecipients = new List<Recipient>() { new Recipient { EmailAddress = new EmailAddress { Address = "ToBalh@blah.com" } } };
message.Subject = "Blah";
message.SingleValueExtendedProperties = new MessageSingleValueExtendedPropertiesCollectionPage()
{
new SingleValueLegacyExtendedProperty {Id = "Integer 0x0E07",Value = "1" },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0039", Value = "2020-05-12T10:10:47.2048+10:00" },
new SingleValueLegacyExtendedProperty {Id = "SystemTime 0x0E06",Value = "2020-05-12T10:10:47.2048+10:00" }
};
var SaveMessage = graphClient.Me.MailFolders["Inbox"].Messages.Request().AddAsync(message).Result;
适合我