EWS将已发送的项目保存在WellKnownFolderName.SentItems中

时间:2018-09-25 06:33:44

标签: c# exchangewebservices send mailmessage

我正尝试使用EmailMessage和用户 A 发送EWS,并将发送的项目保存在用户 B的SentItems文件夹中。基本上可以。我遇到的唯一问题是,该项目保存为草稿而不是已发送项目。

代码:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2, timeZone)
{
    Url = new Uri(uri),
    Credentials = new WebCredentials(new NetworkCredential(user, password, domain)),
    UseDefaultCredentials = false,
};

EmailMessage message = new EmailMessage(service)
{
    Subject = subject,
    Body = new MessageBody(BodyType.HTML, fullBody)
};
message.From = email;
message.ToRecipients.Add(email);

FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);

我尝试过的事情:

// Simply sends the message
message.Send();

// Sends the item but is it not saved in the sentItems of email-account
FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);
message.SendAndSaveCopy(folderId);

// Sends the item, saves the item in the right folder, but it is saved as a draft, not as a sent item
FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);
message.Send();
message.Move(folderId);

我想念什么或做错什么了?

This家伙告诉我们先保存然后再保存usind folderId,但是在这种情况下,我会遇到以下错误:

This operation can't be performed because this service object already has an ID. To update this service object, use the Update() method instead.

1 个答案:

答案 0 :(得分:0)

经过一些测试,我发现正确的步骤如下:

FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);
message.SendAndSaveCopy(folderId);

但是,必须存在一个错误,因为该项始终保存在用于对ExchangeService进行身份验证的用户的sendItems文件夹中,而忽略了作为参数传递的folderId
解决方法(解决方法)是按照Microsoft Documentation的说明模拟用户。这里是注册代码:

service.AutodiscoverUrl(email);
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email);

它起作用了...我想知道其他人是否能够以适当的方式来安排它。