Office 365 send email with attachment using OutlookServicesClient

前端 未结 2 1277
我在风中等你
我在风中等你 2021-01-17 04:36

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 c

2条回答
  •  深忆病人
    2021-01-17 05:00

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

提交回复
热议问题