冒充没有邮箱时如何发送带附件的邮件

How to send an email with an attachment when you are impersonating and have no mailbox

我有一项服务 运行 作为机器上的计划作业。它 运行 在没有自己的邮箱的服务帐户下。我们希望它从团队的共享收件箱发送电子邮件。
在这里使用模拟是我尝试过的。

var service = new ExchangeService
{
    TraceEnabled = true,
    ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, Resources.EmailUsername)
};

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);                        
}

email.Send();

它失败了,我得到一个例外:

When making a request as an account that does not have a mailbox, you must specify the mailbox primary smtp address for any distinguished folder Ids.

TraceEnabled 部分让我注意到 MessageDisposition=SaveOnly 设置在 xml 中。我查找了 MessageDisposition 并认为我想要 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();