Use attachments from calendar items - Outlook - C#

0

I'm trying to use the attachments included in calendar items pulled progmatically. I have a list of chosen calendar subject lines from a previous dialog box, and while the subject is transferring properly, the body isn't working well (another question altogether) but the attachments aren't working whatsoever. Here's my foreach loop where the attachments are being placed into an Attachments array for use later:

        string[] subjects = new string[dialog.chosen.Count];
        string[] bodies = new string[dialog.chosen.Count];
        Attachments[] attach = new Attachments[dialog.chosen.Count];
        foreach (Outlook.AppointmentItem appt in rangeAppts)
        {
            foreach (string text in dialog.chosen)
            {
                if (text == appt.Subject)
                {
                    subjects[i] = appt.Subject;
                    bodies[i] = Convert.ToString(appt.Body);
                    attach[i] = appt.Attachments;
                    i = i + 1;
                }
            }

        }

And then here's where I actually call the method:

            sendEmailTemplate(bodies[i], subject, to, "", attachment: attach[i]);

And then the method itself:

public void sendEmailTemplate(string body, string subject, string to, string cc , Attachments attachment = null)
{
    Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    oMailItem.HTMLBody = body;
    oMailItem.Subject = subject;
    try
    {
        oMailItem.Attachments.Add(attachment);
    }
    catch {}
    oMailItem.To = to;
    oMailItem.CC = cc;
    oMailItem.Display(false);
    oMailItem.Application.ActiveInspector().WindowState = Microsoft.Office.Interop.Outlook.OlWindowState.olNormalWindow;
}

I've tried several things, however when I actually go to send the e-mail, I end up getting:

Exception: Member not found. HRESULT: 0x80020003

And then I haven't been able to get anything else to work. The try/catch loop on the method is to prevent the above exception as I was getting that exception regardless of whether or not an attachment was present, and now attachments just aren't being added.

I'm using Interop that comes with Office along with C#. Winforms if that makes a difference.

c#
calendar
outlook
ms-office
office-interop
asked on Stack Overflow Oct 18, 2015 by Maelo

1 Answer

1

MailItem.Attachments takes either a string (fully qualified file name), or another Outlook item (MailItem, ContactItem, etc.).

You cannot pass Attachments object as an argument. If you need to copy the attachments, loop through all attachments in the Attachments collection, call Attachment.SaveAsFile for each attachment, pass the file name to MailItem.Attachments.Add, delete thee temporary file.


User contributions licensed under CC BY-SA 3.0