发送带附件的电子邮件

发送带附件的电子邮件

本文介绍了Office 365 使用 OutlookServicesClient 发送带附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send out some e-mails on behalf of users of my LOB application. I'm using the Office 365 connected service API so that I can authenticate using OAuth. My code will send the e-mail, but no attachments show up. Here is an isolated example of my code:

    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

The OutlookServicesClient I am using was found here https://visualstudiogallery.msdn.microsoft.com/a15b85e6-69a7-4fdf-adda-a38066bb5155

推荐答案

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

I just tried this myself, and it looks like the problem is that the OutlookServicesClient just doesn't include the attachment data when you do the send. You can see this yourself if you use Fiddler.

我会让负责这个图书馆的人知道这件事.同时,您可以先将邮件另存为草稿,然后使用附件进行更新,然后再发送,以使其工作.类似的东西:

I'll let the folks responsible for this library know about this. In the meantime, you can make it work by first saving the message as a draft, then updating with the attachments, then sending. Something like:

// 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 使用 OutlookServicesClient 发送带附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:38