Dela via


Så här använder du Microsoft Outlook-objektbiblioteket för att hämta ett meddelande med hjälp av Visual C# från Inkorgen

Introduktion

I den här artikeln beskrivs hur du använder Microsoft Outlook 2002-objektbiblioteket eller Microsoft Office Outlook 2003-objektbiblioteket för att hämta ett meddelande från Inkorgen med hjälp av Microsoft Visual C#.

Mer information

Följ dessa steg om du vill använda Outlook 2002-objektbiblioteket eller Outlook 2003-objektbiblioteket för att hämta ett meddelande från Inkorgen med hjälp av Visual C#:

  1. Skapa ett nytt konsolprogramprojekt i Microsoft Visual Studio .NET eller Visual Studio 2005:

    1. På Arkiv-menyn pekar du på Nytt och väljer sedan Projekt.

    2. Under Projekttyper väljer du Visual C#-projekt.

      Kommentar

      I Visual Studio 2005 väljer du Visual C#.

    3. Under Mallar väljer du Konsolprogram.

    4. Välj OK. Som standard skapas en fil med namnet Class1.cs .

      Kommentar

      I Visual Studio 2005 skapas Program.cs som standard.

  2. Lägg till en referens till antingen Outlook 2002-objektbiblioteket eller Outlook 2003-objektbiblioteket. För att göra detta följer du stegen nedan:

    1. På Projekt-menyn väljer du Lägg till referens.

    2. Välj fliken COM .

    3. På fliken COM väljer du Microsoft Outlook 11.0-objektbibliotek om du använder Outlook 2003 eller väljer Microsoft Outlook 10.0-objektbibliotek om du använder Outlook 2002.

    4. Välj Välj.

    5. I dialogrutan Lägg till referenser väljer du OK.

      Kommentar

      Om du får ett meddelande om att generera omslutningar för de bibliotek som du har valt väljer du Ja.

  3. I Class1.cs-kodfönstret ersätter du all befintlig kod med följande kod:

    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("mapi");
    
    // 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("profilename",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("Attachments: " + 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() + "-" + 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() + "-" + 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("{0} Exception caught: ", e);
    }
    
    // Return value.
    return 0;
    
    }
    }
    }
    
  4. I den här koden gör du alla nödvändiga ändringar där du ser "ATT GÖRA"-kommentarerna.

  5. Tryck på F5 för att skapa och kör sedan programmet.

Referenser

Mer information finns i Microsoft Office Development med Visual Studio.