Partager via


Comment utiliser la bibliothèque d’objets Microsoft Outlook pour récupérer un message à l’aide de Visual C# dans la boîte de réception

Présentation

Cet article explique comment utiliser la bibliothèque d’objets Microsoft Outlook 2002 ou la bibliothèque d’objets Microsoft Office Outlook 2003 pour récupérer un message à partir de la boîte de réception à l’aide de Microsoft Visual C#.

Plus d’informations

Pour utiliser la bibliothèque d’objets Outlook 2002 ou la bibliothèque d’objets Outlook 2003 pour récupérer un message à partir de la boîte de réception à l’aide de Visual C#, procédez comme suit :

  1. Dans Microsoft Visual Studio .NET ou Visual Studio 2005, créez un projet d’application console :

    1. Dans le menu Fichier , pointez sur Nouveau, puis sélectionnez Projet.

    2. Sous Types de projets, sélectionnez Projets Visual C#.

      Remarque

      Dans Visual Studio 2005, sélectionnez Visual C#.

    3. Sous Modèles, sélectionnez Application console.

    4. Cliquez sur OK. Par défaut, un fichier nommé Class1.cs est créé.

      Remarque

      Dans Visual Studio 2005, Program.cs est créé par défaut.

  2. Ajoutez une référence à la bibliothèque d’objets Outlook 2002 ou à la bibliothèque d’objets Outlook 2003. Pour ce faire, procédez comme suit :

    1. Dans le menu Projet, sélectionnez Ajouter une référence.

    2. Sélectionnez l’onglet COM .

    3. Sous l’onglet COM , sélectionnez Microsoft Outlook 11.0 Object Library si vous utilisez Outlook 2003, ou sélectionnez Microsoft Outlook 10.0 Object Library si vous utilisez Outlook 2002.

    4. Sélectionnez Sélectionner.

    5. Dans la boîte de dialogue Ajouter des références , sélectionnez OK.

      Remarque

      Si vous recevez un message pour générer des wrappers pour les bibliothèques que vous avez sélectionnées, sélectionnez Oui.

  3. Dans la fenêtre de code Class1.cs, remplacez tout le code existant par le code suivant :

    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. Dans ce code, apportez les modifications nécessaires lorsque vous voyez les commentaires « TO DO ».

  5. Appuyez sur F5 pour générer, puis exécutez le programme.

Références

Pour plus d’informations, consultez Développement Microsoft Office avec Visual Studio.