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

时间:2015-01-19 18:39:30

标签: c# api office365

我试图代表我的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

2 个答案:

答案 0 :(得分:4)

我自己尝试了这个问题,看起来问题是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();

答案 1 :(得分:0)

我能够解决这个问题,似乎在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();
    }