gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:32:34 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

Microsoft 文档链接 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);
}

关于c# - 使用 EWS 托管 API 将 .msg 文件上传到 Exchange Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52210229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com