How to use the Microsoft Outlook Object Library to send a message that has attachments by using Visual C#


This article describes how to use the Microsoft Outlook 2002 Object Library or the Microsoft Office Outlook 2003 Object Library to send a message (that is, a MailItem object) that contains attachments by using Microsoft Visual C#.

More Information

To use the Microsoft Outlook Object Library to send a message that contains attachments, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C# Projects.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  4. Under Templates, click Console Application.

    By default, Class1.cs is created.

    Note In Microsoft Visual Studio 2005, Program.cs is created by default.
  5. Add a reference to the Microsoft Outlook 2002 Object Library or to the Microsoft Office Outlook 2003 Object Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, click the Microsoft Outlook Object Library that you want to use, and then click Select.

      Note In Visual Studio 2005, you do not have to click Select.
    3. In the Add References dialog box, click OK.
    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  6. In the Class1.cs code window, replace the code with the following:
    using System;
    //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
    //using Outlook = Microsoft.Office.Interop.Outlook;

    namespace Outlook_SendMailItem
    public class Class1
    public static int Main(string[]args)
    // Create the Outlook application by using inline initialization.
    Outlook.Application oApp = new Outlook.Application();

    //Create the new message by using the simplest approach.
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

    //Add a recipient.
    // TODO: Change the following recipient where appropriate.
    Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("e-mail address");

    //Set the basic properties.
    oMsg.Subject = "This is the subject of the test message";
    oMsg.Body = "This is the text in the message.";

    //Add an attachment.
    // TODO: change file path where appropriate
    String sSource = "C:\\setupxlg.txt";
    String sDisplayName = "MyFirstAttachment";
    int iPosition = (int)oMsg.Body.Length + 1;
    int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
    Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);

    // If you want to, display the message.
    // oMsg.Display(true); //modal

    //Send the message.

    //Explicitly release objects.
    oRecip = null;
    oAttach = null;
    oMsg = null;
    oApp = null;

    // Simple error handler.
    catch (Exception e)
    Console.WriteLine("{0} Exception caught: ", e);

    //Default return value.
    return 0;


  7. Search for TODO in the code, and then modify the code for your environment.
  8. Press F5 to build and to run the program.
  9. Make sure that the e-mail message has been sent and received.
Note The security features in Outlook will cause one or more prompts to occur before the message is sent. For more information, see the "References" section.


For more information, visit the following MSDN Web site:
For more information, click the following article number to view the article in the Microsoft Knowledge Base:

290500 Description of the developer-related e-mail security features in Outlook 2002


Article ID: 310263 - Last Review: Mar 23, 2009 - Revision: 1