Avatar of chrisbray
chrisbray
Flag for United Kingdom of Great Britain and Northern Ireland asked on

How can I add Outlook attachments in C# using Late Binding?

I need to send emails using Outlook, and I have to use late binding because users have various different versions of Outlook from XP through to 2010.  I cannot use MAPI because I have to be able to send HTML emails.

I have got it all working EXCEPT for adding attachments which fails because retrieving the Attachments property always returns null...  Presumably I am looking for the wrong properly or some other failing on my part!

The code so far is shown below, suggestions would be welcomed.

Chris Bray

public static void SendOutlookEmail(string fromAddress, string toAddress, string ccList, string bccList, string subject, string body, string attachmentList)
        {
            object objOutlookApplication;
            object objOutlookItem;
            Type objClassType;

            try
            {

                objClassType = Type.GetTypeFromProgID("Outlook.Application");

                if (objClassType != null)
                {

                    objOutlookApplication = Activator.CreateInstance(objClassType);
                    objOutlookItem = objOutlookApplication.GetType().InvokeMember("CreateItem", BindingFlags.InvokeMethod, null, objOutlookApplication, new object[] { 0 });

                    // Get all To addresses
                    string[] toAddresses;

                    if (toAddress.Length > 0)
                    {
                        toAddresses = toAddress.Split(';');
                        objOutlookItem.GetType().InvokeMember("To", BindingFlags.SetProperty, null, objOutlookItem, toAddresses);
                    }

                    // Get all CC addresses
                    string[] ccAddresses;
                    if (ccList.Length > 0)
                    {
                        ccAddresses = ccList.Split(';');
                        objOutlookItem.GetType().InvokeMember("CC", BindingFlags.SetProperty, null, objOutlookItem, ccAddresses);
                    }

                    // Get all BCC addresses
                    string[] bccAddresses;
                    if (bccList.Length > 0)
                    {
                        bccAddresses = bccList.Split(';');
                        objOutlookItem.GetType().InvokeMember("BCC", BindingFlags.SetProperty, null, objOutlookItem, bccAddresses);
                    }

                    // Subject
                    if (subject.Length > 0)
                    {
                        objOutlookItem.GetType().InvokeMember("Subject", BindingFlags.SetProperty, null, objOutlookItem, new string[] { subject });
                    }

                    // Body
                    if (body.Length > 0)
                    {
                        objOutlookItem.GetType().InvokeMember("HTMLBody", BindingFlags.SetProperty, null, objOutlookItem, new string[] { body });
                    }

                    // Attachments
                    // split out attachmentList
                    if (attachmentList.Length > 0)
                    {
                        object objAttachments = objOutlookItem.GetType().GetProperty("Attachments", BindingFlags.GetProperty); // Always returns null

                        string[] fileAttachments = attachmentList.Split(';');
                        foreach (string attachment in fileAttachments)
                        {
                            objAttachments.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objAttachments, new object[] { attachment, Type.Missing, Type.Missing, Type.Missing }); // Fails due to null objAttachments
                        }
                    }

                    
                    // Display the dialog
                    objOutlookItem.GetType().InvokeMember("Display", BindingFlags.InvokeMethod, null, objOutlookItem, new object[] { true });

                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            { 
                objOutlookApplication = null;
                objOutlookItem = null;
                objClassType = null;            
            }
        }

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
chrisbray

8/22/2022 - Mon
starlite551

chrisbray

ASKER
Hi starlite551:

Why have you sent me that link?  As far as I can tell it has no relevance whatsoever to the question asked - for a start it is talking about wrting add-ins, and secondly it is using early binding when my question relates to late binding!

Chris Bray
ASKER CERTIFIED SOLUTION
Frank Helk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
chrisbray

ASKER
Hi frankhelk,

Thank you for that - bang on the money!  The answer is that you invoke the member Attachments rather than try to access the property.  For those who may find this thread when searching, and in case the link supplied gets broken, the resulting working code is shown below.

Chris Bray

                    // Attachments
                    // split out attachmentList
                    if (attachmentList.Length > 0)
                    {
                        objAttachments = objOutlookItem.GetType().InvokeMember("Attachments", BindingFlags.GetProperty, null , objOutlookItem, new object[] { Missing.Value });

                        fileAttachments = attachmentList.Split(';');
                        foreach (string attachment in fileAttachments)
                        {
                            objAttachments.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objAttachments, new object[] { attachment, Type.Missing, Type.Missing, Type.Missing });
                        }
                    }
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck