【问题标题】:Upload .msg file to Exchange Server using EWS Managed API使用 EWS 托管 API 将 .msg 文件上传到 Exchange Server
【发布时间】:2019-02-12 01:56:16
【问题描述】:
【问题讨论】:
标签:
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);
}