【问题标题】:Upload .msg file to Exchange Server using EWS Managed API使用 EWS 托管 API 将 .msg 文件上传到 Exchange Server
【发布时间】:2019-02-12 01:56:16
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# vb.net exchange-server exchangewebservices msg


    【解决方案1】:

    微软文档链接here

    您可以使用 UploadItems EWS 操作将项目作为数据流上传。项目的这种数据流表示必须来自 ExportItems 操作调用的结果。因为 EWS Managed API 没有实现 UploadItems 操作,如果您使用 EWS Managed 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-04
      • 2011-08-04
      • 1970-01-01
      • 2011-06-12
      • 2011-09-11
      • 1970-01-01
      • 2016-04-18
      • 2015-03-18
      • 2017-02-13
      相关资源
      最近更新 更多