Office 365 使用 OutlookServicesClient 发送带附件的电子邮件

Office 365 send email with attachment using OutlookServicesClient

我正在尝试代表我的 LOB 应用程序的用户发送一些电子邮件。我正在使用 Office 365 连接服务 API,以便我可以使用 OAuth 进行身份验证。我的代码将发送电子邮件,但没有显示附件。这是我的代码的一个孤立示例:

    static async void SendUsingOutlookClient(CommunicationRow me, OutlookServicesClient outlook)
    {
        var m = new Message();
        m.From = ToRecipient(me.From);
        m.Body = new ItemBody { Content = me.Body };
        if (me.IsBodyHtml)
            m.Body.ContentType = BodyType.HTML;
        else
            m.Body.ContentType = BodyType.Text;
        m.Subject = me.Subject;
        m.CcRecipients.Add(me.Cc);
        m.BccRecipients.Add(me.Bcc);
        m.ToRecipients.Add(me.To);
        foreach (var attach in me.Attachments)
        {
            var file = attach.File;
            var fileversion = file.GetVersion(attach.Version);
            string fullpath = LibraryServiceImpl.GetFullNameInArchive(fileversion);
            var mattach = new FileAttachment { Name = file.Name, ContentId = attach.ContentId, ContentLocation = fullpath, ContentType = GraphicUtils.DetermineMime(Path.GetExtension(fullpath)) };
            if (file.Name.MissingText())
                mattach.Name = attach.ContentId + fileversion.FileExtension;
            m.Attachments.Add(mattach);
        }
        await outlook.Me.SendMailAsync(m, true);
    }

我正在使用的 OutlookServicesClient 是在这里找到的 https://visualstudiogallery.msdn.microsoft.com/a15b85e6-69a7-4fdf-adda-a38066bb5155

我自己刚刚尝试过,看起来问题是 OutlookServicesClient 在您发送时不包含附件数据。如果你使用Fiddler,你可以自己看到这个。

我会让负责这个图书馆的人知道这件事。同时,您可以先将邮件保存为草稿,然后更新附件,然后发送,使其正常工作。类似于:

// Save to Drafts folder
await outlook.Me.AddMessageAsync(m);

foreach (var attach in me.Attachments)
{
    var file = attach.File;
    var fileversion = file.GetVersion(attach.Version);
    string fullpath = LibraryServiceImpl.GetFullNameInArchive(fileversion);
    var mattach = new FileAttachment { Name = file.Name, ContentId = attach.ContentId, ContentLocation = fullpath, ContentType = GraphicUtils.DetermineMime(Path.GetExtension(fullpath)) };
    if (file.Name.MissingText())
        mattach.Name = attach.ContentId + fileversion.FileExtension;
    m.Attachments.Add(mattach);
}

// Update with attachments
await m.UpdateAsync();
// Send the message
await m.SendAsync();

我已经弄明白了,看来在 Office 365 API 中发送电子邮件附件的唯一方法是先将电子邮件保存为草稿,添加附件,然后发送。这是我修改后的 SendUsingOutlookClient 方法

    static async void SendUsingOutlookClient(CommunicationRow me, OutlookServicesClient outlook)
    {
        var m = new Message();
        m.From = ToRecipient(me.From);
        m.Body = new ItemBody { Content = me.Body };
        if (me.IsBodyHtml)
            m.Body.ContentType = BodyType.HTML;
        else
            m.Body.ContentType = BodyType.Text;
        m.Subject = me.Subject;
        m.CcRecipients.Add(me.Cc);
        m.BccRecipients.Add(me.Bcc);
        m.ToRecipients.Add(me.To);
        outlook.Me.Messages.AddMessageAsync(m).Wait();
        foreach (var attach in me.Attachments)
        {
            var file = attach.File;
            var fileversion = file.GetVersion(attach.Version);
            string fullpath = LibraryServiceImpl.GetFullNameInArchive(fileversion);
            var mattach = new FileAttachment { Name = file.Name, ContentId = attach.ContentId, ContentLocation = fullpath, ContentType = GraphicUtils.DetermineMime(Path.GetExtension(fullpath)) };
            if (file.Name.MissingText())
                mattach.Name = attach.ContentId + fileversion.FileExtension;
            m.Attachments.Add(mattach);
            await m.UpdateAsync();
        }
        await m.SendAsync();
    }