Monday, June 21, 2010

Send Email from C# using Outlook's COM late binding

The following sample code shows how to send emails from Outlook and Exchange using C#. This code works with any version of Outlook because it uses Late Binding to automate Outlook. Parts of the code where taken from other websites.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;

namespace LateBindingTest
{
    class OutlookEmailerLateBinding
    {

        private object oApp;
        private object oNameSpace;
        private object oOutboxFolder;

        public OutlookEmailerLateBinding()
        {
            Type outlook_app_type;
            object[] parameter = new object[1];
            //Get the excel object
            outlook_app_type = Type.GetTypeFromProgID("Outlook.Application");
            //Create instance of excel
            oApp = Activator.CreateInstance(outlook_app_type);
            //Set the parameter which u want to set
            parameter[0] = "MAPI";
            //Set the Visible property
            oNameSpace = outlook_app_type.InvokeMember("GetNamespace",
BindingFlags.InvokeMethod, null, oApp, parameter);

            var Logon_parameter = new object[4] { null, null, true, true };
            oNameSpace.GetType().InvokeMember("Logon",
BindingFlags.InvokeMethod, null, oNameSpace, Logon_parameter);

            var GetDefaultFolder_parameter = new object[1] { 6 };
            oOutboxFolder =
oNameSpace.GetType().InvokeMember("GetDefaultFolder",
BindingFlags.InvokeMethod, null, oNameSpace,
GetDefaultFolder_parameter);

            Console.WriteLine("Press enter to exit");
        }

        static void Main(string[] args)
        {
            //Variable
            try
            {
                OutlookEmailerLateBinding app = new OutlookEmailerLateBinding();
                app.SendOutlookEmail("yourname@gmail.com", "Test 1 2
3", "Test message. Testing.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Stack {0} ", e.Message);
            }
            
            Console.ReadLine();
        }

        public void SendOutlookEmail(string toValue, string
subjectValue, string bodyValue)
        {
            var CreateItem_parameter = new object[1] { 0 };
            object oMailItem =
oApp.GetType().InvokeMember("CreateItem", BindingFlags.InvokeMethod,
null, oApp, CreateItem_parameter);

            var mail_item_type = oMailItem.GetType();
            mail_item_type.InvokeMember("To",
                BindingFlags.SetProperty, null, oMailItem, new
object[] { toValue });
            mail_item_type.InvokeMember("Subject",
                BindingFlags.SetProperty, null, oMailItem, new
object[] { subjectValue });
            mail_item_type.InvokeMember("Body",
                BindingFlags.SetProperty, null, oMailItem, new
object[] { bodyValue });
            mail_item_type.InvokeMember("Send",
                BindingFlags.InvokeMethod, null, oMailItem, null);

        }

    }
}

5 comments:

Anonymous said...

How to send email to more than one receivers? When I use this code, I got the error message saying: "Outlook does not recognize one or more names. "

Juan Pablo said...

I think you have to use the Add of the Recipients property in the MailItem object. You can google those and just use the same convention I used in the post to call late binding objects. Another option you can try is the dynamic keyword in c#4.0 with it you can do late binding but without all the reflection cluttering. You can google the dynamic keyword in c# to learn how it works. Thanks!

happy said...

code work...but I am getting a security popup , try this in a asp.net webapp....how to bypass the security popup???worthe ricatin

Juan Pablo said...

@happy : I don't think there is a way of bypassing that. You mentioned asp.net, are you using this in a server? Microsoft and most people don't recommend using Office Automation (which COM late binding is) for web or other server side applications. Automation is intended for only desktop apps.

Anonymous said...

Thank you Juan, this worked well