0

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

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

Kleinux
  • 1,511
  • 10
  • 22
  • have you stepped thru the code.. ? on this line in the foreach loop what is the value of `me.Attachments` where are you declaring the attachment object..? perhaps this link may help http://stackoverflow.com/questions/1195111/c-sharp-mailto-with-attachment also this can easily be done using C# .net `System.Mail.net` – MethodMan Jan 19 '15 at 18:59
  • @MethodMan sending the e-mail isn't the problem and stepping through the code isn't going to tell me much. My e-mail arrives at the intended destination just without any attachments. Per my description of the problem I am trying to use OAuth to authenticate the sender. I know of no way to use that with the System.Mail.Net.SmtpClient. Credentials in that class require the password to be entered. – Kleinux Jan 19 '15 at 21:58
  • stepping thru the code will tell you a lot.. as I was asking earlier .. what is the value of the attachment prior to sending the email.. perhaps you are not getting the full filepath/name of the attachment for instance..also does this have to be an Asynchronous send.. help us help you.. thanks – MethodMan Jan 19 '15 at 22:05
  • @MethodMan Yes, when I step through having attached a pdf the properties of mattach seem reasonable and I have access to the file in fullpath. I added a link to the library being used here and I only have access to Async methods. – Kleinux Jan 19 '15 at 22:17

2 Answers2

4

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();
Jason Johnston
  • 17,194
  • 2
  • 20
  • 34
  • Thanks for posting this. I'd figured this out through some guessing, but you deserve the points. – Kleinux Jan 20 '15 at 17:41
0

I was able to figure this out and it seems that the only way to send attachments out in an e-mail in the Office 365 API is to first save the e-mail as a draft, add the attachments, and then send it. Here is my revised SendUsingOutlookClient method

    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();
    }
Kleinux
  • 1,511
  • 10
  • 22