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

问题描述 投票:2回答:1

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

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

我找到了this documentation如何使用带有XML主体的EWS请求来完成它。但是,我的所有系统都依赖于EWS Managed API,我找不到执行此操作的等效方法。

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

c# vb.net exchange-server exchangewebservices msg
1个回答
3
投票

Microsoft文档链接here

您可以使用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);
}
© www.soinside.com 2019 - 2024. All rights reserved.