Microsoft KB Archive/310258

= How to use the Microsoft Outlook Object Library to retrieve a message from the Inbox by using Visual C# =

Article ID: 310258

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 Q310258



INTRODUCTION
This article describes how to use the Microsoft Outlook 2002 Object Library or the Microsoft Office Outlook 2003 Object Library to retrieve a message from the Inbox by using Microsoft Visual C#.



MORE INFORMATION
To use the Outlook 2002 Object Library or the Outlook 2003 Object Library to retrieve a message from the Inbox by using Visual C#, follow these steps:  In Microsoft Visual Studio .NET or Visual Studio 2005, create a new Console Application project:  On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects.

Note In Visual Studio 2005, click Visual C#. Under Templates, click Console Application. Click OK. By default, a file that is named Class1.cs is created.

Note In Visual Studio 2005, Program.cs is created by default.  Add a reference to either the Outlook 2002 Object Library or the Outlook 2003 Object Library. To do this, follow these steps:  On the Project menu, click Add Reference.</li> Click the COM tab.</li> On the COM tab, click Microsoft Outlook 11.0 Object Library if you are using Outlook 2003, or click Microsoft Outlook 10.0 Object Library if you are using Outlook 2002.</li> Click Select.</li> In the Add References dialog box, click OK.

Note If you receive a message to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the Class1.cs code window, replace all the existing code with the following code: using System; using System.Reflection; // to use Missing.Value

//TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line. //using Outlook = Microsoft.Office.Interop.Outlook;

namespace ConsoleApplication1 {   public class Class1 {       public static int Main(string[]args) {           try {               // Create the Outlook application. // in-line initialization Outlook.Application oApp = new Outlook.Application;

// Get the MAPI namespace. Outlook.NameSpace oNS = oApp.GetNamespace(&quot;mapi&quot;);

// Log on by using the default profile or existing session (no dialog box). oNS.Logon(Missing.Value,Missing.Value,false,true);

// Alternate logon method that uses a specific profile name. // TODO: If you use this logon method, specify the correct profile name // and comment the previous Logon line. //oNS.Logon(&quot;profilename&quot;,Missing.Value,false,true);

//Get the Inbox folder. Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

//Get the Items collection in the Inbox folder. Outlook.Items oItems = oInbox.Items;

// Get the first message. // Because the Items folder may contain different item types, // use explicit typecasting with the assignment. Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst;

//Output some common properties. Console.WriteLine(oMsg.Subject); Console.WriteLine(oMsg.SenderName); Console.WriteLine(oMsg.ReceivedTime); Console.WriteLine(oMsg.Body);

//Check for attachments. int AttachCnt = oMsg.Attachments.Count; Console.WriteLine(&quot;Attachments: &quot; + AttachCnt.ToString); //TO DO: If you use the Microsoft Outlook 10.0 Object Library, uncomment the following lines. /*if (AttachCnt > 0) {                   for (int i = 1; i <= AttachCnt; i++) Console.WriteLine(i.ToString + &quot;-&quot; + oMsg.Attachments.Item(i).DisplayName); }*/               //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following lines. /*if (AttachCnt > 0) {                   for (int i = 1; i <= AttachCnt; i++) Console.WriteLine(i.ToString + &quot;-&quot; + oMsg.Attachments[i].DisplayName); }*/

//Display the message. oMsg.Display(true); //modal

//Log off. oNS.Logoff;

//Explicitly release objects. oMsg = null; oItems = null; oInbox = null; oNS = null; oApp = null; }

//Error handler. catch (Exception e)           { Console.WriteLine(&quot;{0} Exception caught: &quot;, e); }

// Return value. return 0;

}   } } </li> In this code, make any necessary changes where you see the &quot;TO DO&quot; comments.</li> Press F5 to build and then run the program.</li></ol>

<div class="references_section">