繁体   English   中英

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

[英]Upload .msg file to Exchange Server using EWS Managed API

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

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

我找到了有关如何使用带有XML正文的EWS请求来完成此文档的文档 但是,我所有的系统都依赖于EWS Managed API ,并且我找不到等效的方法来执行此操作。

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

Microsoft文档链接在这里

您可以使用UploadItems EWS操作将项目作为数据流上传。 项目的这种数据流表示必须来自ExportItems操作调用的结果 由于EWS托管API未实现UploadItems操作,因此,如果您使用EWS托管API,则需要编写例程以发送Web请求。

您可能可以将.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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM