Solved

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

Posted on 2010-09-13
4
1,871 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:chrisbray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Author Comment

by:chrisbray
ID: 33670020
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
0
 
LVL 14

Accepted Solution

by:
frankhelk earned 500 total points
ID: 33670311
Since I'm not really familiar with these Outlook objects, I'm just speculating on a more theoretical level ....

I think this this returns (null) because the email object has not yet got anything attached ?

I've found something you should have a look at:

Search for "GetTypeFromProgID" on that page, you should reach the 4th message form "Mai08" within the second code snippet.

Take a look at that snippet and the way the attachments are added there (precisely: Find the comment "// Anhänge" (german for "Attachments") and the lines below.

Hope it helps.
0
 
LVL 3

Author Comment

by:chrisbray
ID: 33670458
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 });
                        }
                    }
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question