Late binding and the Attachments collection

N

NeilL

I have a C# application that generates email messages, lets the user
view/edit and (I assume) send the message.

What I do is late bind to outlook to generate the mail. Now, I need to add
an attachment to the mail item attachments collection and I'm stuck.

At the moment, the code does (without all the error checking)

Type objClassType;
objClassType = Type.GetTypeFromProgID("Outlook.Application");
objApp = Activator.CreateInstance(objClassType);
//* Create the mail item
object mailItem =
objApp.GetType().InvokeMember("CreateItem",BindingFlags.InvokeMethod , null,
objApp, new object[]{0});

//* Get the namespace, login and get to the outbox
object nameSpace = objApp.GetType().InvokeMember("GetNamespace",
BindingFlags.InvokeMethod, null, objApp, new object[] { "Mapi" });
nameSpace.GetType().InvokeMember("Logon", BindingFlags.InvokeMethod, null,
nameSpace, new object[] { null, null, true, true });
object outBox = nameSpace.GetType().InvokeMember("GetDefaultFolder",
BindingFlags.InvokeMethod, null, nameSpace, new object[] { 4 }); //* 4 =
olFolderOutbox
///* CODE LEFT OUT THAT ADDS THE TO, CC, BODY stuff

//* Add the subject
mailItem.GetType().InvokeMember("Subject", BindingFlags.SetProperty, null,
mailItem, new object[] { _subject });

//* Now, try to get the attachments property
Object attachments = mailItem.GetType().GetProperty("Attachments",
BindingFlags.GetProperty);
//* This always returns null so, how do I get to the collection so I can
invoke the Add member to add my file attachment.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top