Freigeben über


Verwenden der Microsoft Outlook-Objektbibliothek zum Abrufen einer Nachricht mithilfe von Visual C# aus dem Posteingang

Einführung

In diesem Artikel wird beschrieben, wie Sie die Microsoft Outlook 2002-Objektbibliothek oder die Microsoft Office Outlook 2003-Objektbibliothek verwenden, um eine Nachricht mithilfe von Microsoft Visual C# aus dem Posteingang abzurufen.

Weitere Informationen

Führen Sie die folgenden Schritte aus, um die Outlook 2002-Objektbibliothek oder die Outlook 2003-Objektbibliothek zum Abrufen einer Nachricht aus dem Posteingang mithilfe von Visual C# zu verwenden:

  1. Erstellen Sie in Microsoft Visual Studio .NET oder Visual Studio 2005 ein neues Konsolenanwendungsprojekt:

    1. Zeigen Sie im Menü Datei auf Neu, und klicken Sie dann auf Projekt.

    2. Wählen Sie unter "Projekttypen" die Option "Visual C#-Projekte" aus.

      Notiz

      Wählen Sie in Visual Studio 2005 Visual C# aus.

    3. Wählen Sie unter "Vorlagen" die Option "Konsolenanwendung" aus.

    4. Wählen Sie OK aus. Standardmäßig wird eine Datei mit dem Namen Class1.cs erstellt.

      Notiz

      In Visual Studio 2005 wird Program.cs standardmäßig erstellt.

  2. Fügen Sie einen Verweis auf die Outlook 2002-Objektbibliothek oder die Outlook 2003-Objektbibliothek hinzu. Gehen Sie dazu wie folgt vor:

    1. Wählen Sie im Menü Projekt die Option Verweis hinzufügen aus.

    2. Wählen Sie die REGISTERKARTE COM aus.

    3. Wählen Sie auf der Registerkarte COM microsoft Outlook 11.0-Objektbibliothek aus, wenn Sie Outlook 2003 verwenden, oder wählen Sie Microsoft Outlook 10.0-Objektbibliothek aus, wenn Sie Outlook 2002 verwenden.

    4. Klicken Sie auf Auswählen.

    5. Wählen Sie im Dialogfeld "Verweise hinzufügen" "OK" aus.

      Notiz

      Wenn Sie eine Nachricht erhalten, um Wrapper für die ausgewählten Bibliotheken zu generieren, wählen Sie "Ja" aus.

  3. Ersetzen Sie im codefenster Class1.cs den gesamten vorhandenen Code durch den folgenden 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("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. Machen Sie in diesem Code alle erforderlichen Änderungen dort, wo die Kommentare "TO DO" stehen.

  5. Drücken Sie F5, um das Programm zu erstellen und dann auszuführen.

Referenzen

Weitere Informationen finden Sie unter Microsoft Office Development with Visual Studio.