以编程方式在 Exchange Online 收件箱中创建邮件

Create mail in Exchange Online inbox programatically

自从几天以来就一直面临着关于 EWS 的问题。所以我的情况是;

我要以编程方式同步 GMAIL 和 EXCHANGE ONLINE。这就是我所做的;

Now the problem is here, how can I create an email in Inbox which looks like incoming mail from the sender;

这是我完成的代码;

_service = new ExchangeService(ExchangeVersion.Exchange2013);
        _service.TraceEnabled = true;
        _service.Credentials = new WebCredentials("admin@xyz.onmicrosoft.com", "password");
        _service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
        _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "xyz@xyz.onmicrosoft.com");

        EmailMessage message = new EmailMessage(_service);
        Random r = new Random();
        message.Subject = "Email Message";
        message.From = new EmailAddress("xyz@gmail.com");
        message.Sender = new EmailAddress("xyz@gmail.com");
        message.Body = new MessageBody(BodyType.HTML, "<HTML><body><h1>This is a voice mail.</h1></BODY></HTML>");
        message.ToRecipients.Add(new EmailAddress(""));
        message.Save(WellKnownFolderName.Inbox);

这样它在收件箱中创建了一封电子邮件,但它显示为草稿邮件。我不想要它,我希望它看起来像 RECEIVED mail.

我做错了什么吗?

在保存消息之前,您需要设置几个属性

// Set a delivery time
ExtendedPropertyDefinition PidTagMessageDeliveryTime =
    new ExtendedPropertyDefinition(0x0E06, MapiPropertyType.SystemTime);
DateTime deliveryTime = DateTime.Now; // Or whatever deliver time you want
message.SetExtendedProperty(PidTagMessageDeliveryTime, deliveryTime);

// Indicate that this email is not a draft. Otherwise, the email will appear as a 
// draft to clients.
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
message.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);

保存项目后无法设置这些属性,因此在第一次 Save 调用之前进行设置很重要。