troubleshooting Question

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

Avatar of chrisbray
chrisbrayFlag for United Kingdom of Great Britain and Northern Ireland asked on
.NET ProgrammingC#
4 Comments1 Solution2118 ViewsLast Modified:
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;


                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)
                objOutlookApplication = null;
                objOutlookItem = null;
                objClassType = null;            
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros