使用 EWS Managed API 将 .msg 文件上传到 Exchange Server

Upload .msg file to Exchange Server using EWS Managed API

我找到了几个从 MS Exchange 服务器下载电子邮件并将其保存到文件的示例。

我需要相反的东西。我需要从“.msg”文件在服务器的特定文件夹中创建一封电子邮件。

我找到了 this documentation on how to do it using an EWS request with a XML body. However, all my system relies on EWS Managed API,但找不到执行此操作的等效方法。

我怎样才能执行我需要的操作?我可以通过 Microsoft.Exchange.WebServices.Data.ExchangeService 对象传递自定义请求吗?

Microsoft 文档 link here.

You can use the UploadItems EWS operation to upload an item as a data stream. This data stream representation of an item has to come from the results of an ExportItems operation call. Because the EWS Managed API does not implement the UploadItems operation, if you use the EWS Managed API, you'll need to write a routine to send the web requests.

您可以将您的 .msg 文件转换为 .eml 并使用以下代码添加您的消息。

private static void UploadMIMEEmail(ExchangeService service)
{
    EmailMessage email = new EmailMessage(service);

    string emlFileName = @"C:\import\email.eml";
    using (FileStream fs = new FileStream(emlFileName, FileMode.Open, FileAccess.Read))
    {
        byte[] bytes = new byte[fs.Length];
        int numBytesToRead = (int)fs.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            int n = fs.Read(bytes, numBytesRead, numBytesToRead);
            if (n == 0)
                break;
            numBytesRead += n;
            numBytesToRead -= n;
        }
        // Set the contents of the .eml file to the MimeContent property.
        email.MimeContent = new MimeContent("UTF-8", bytes);
    }

    // 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);
    email.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
    // This results in a CreateItem call to EWS. The email will be saved in the Inbox folder.
    email.Save(WellKnownFolderName.Inbox);
}