这个
TraceEnabled
这一部分让我注意到了
MessageDisposition=SaveOnly
是在xml中设置的。我查了一下,觉得我只想要SendOnly。
经过多次搜索,我终于来到这里:
https://github.com/OfficeDev/ews-managed-api/blob/master/Core/ServiceObjects/Items/EmailMessage.cs
这表明:
public void Send()
{
this.InternalSend(null, MessageDisposition.SendOnly);
}
看起来这正是我想要的。。。但是:
private void InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition)
{
this.ThrowIfThisIsAttachment();
if (this.IsNew)
{
if ((this.Attachments.Count == 0) || (messageDisposition == MessageDisposition.SaveOnly))
{
this.InternalCreate(
parentFolderId,
messageDisposition,
null);
}
else
{
// If the message has attachments, save as a draft (and add attachments) before sending.
this.InternalCreate(
null, // null means use the Drafts folder in the mailbox of the authenticated user.
MessageDisposition.SaveOnly,
null);
this.Service.SendItem(this, parentFolderId);
}
}
...
这两条评论是最有启发性的部分。附件将保存到运行流程的用户的草稿文件夹中。
为了避免这种情况,我们必须在调用Send时保存消息。让我们确保它保存在我们知道存在的邮箱中。因此,我们删除模拟,添加一个步骤来保存它,并修改From字段。然后我们可以安全地发送消息,它将从草稿文件夹中删除自己。
var service = new ExchangeService
{
TraceEnabled = true
};
service.AutodiscoverUrl(Resources.EmailUsername, RedirectionUrlValidationCallback);
var email = new EmailMessage(service);
if (!string.IsNullOrWhiteSpace(recipients))
{
foreach (var recipient in recipients.Split(','))
{
email.ToRecipients.Add(recipient.Trim());
}
}
email.Subject = subject;
email.Body = new MessageBody(BodyType.HTML, body);
if (attachmentName != null && attachment != null)
{
email.Attachments.AddFileAttachment(attachmentName, attachment);
}
var folderId = new FolderId(WellKnownFolderName.SentItems, Resources.EmailUsername);
email.Save(folderId);
email.From = Resources.EmailUsername;
email.Send();