Microsoft KB Archive/310263

From BetaArchive Wiki
Knowledge Base


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

Article ID: 310263

Article Last Modified on 6/29/2007



APPLIES TO

  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Visual C# 2005



This article was previously published under Q310263

SUMMARY

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)
            {
                try
                {
                    // 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");
                    oRecip.Resolve();
    
                    //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.
                    oMsg.Save();
                    oMsg.Send();
    
                    //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.

REFERENCES

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



Additional query words: csharp oom OL2002 OL2000

Keywords: kbhowto kboutlookobj kbinterop kbcode KB310263