Slik bruker du Microsoft Outlook-objektbiblioteket til å sende en HTML-formatert melding ved hjelp av Visual C#

INNLEDNING

Denne artikkelen beskriver hvordan du bruker Microsoft Outlook 2002-objektbiblioteket eller Microsoft Office Outlook 2003-objektbiblioteket til å sende en HTML-formatert melding ved hjelp av Microsoft Visual C#.

Mer informasjon

Hvis du vil bruke objektbiblioteket i Outlook 2002 eller objektbiblioteket i Outlook 2003 til å sende en HTML-formatert melding ved hjelp av Microsoft Visual C#, følger du disse trinnene:

  1. Opprett et nytt konsollprogramprosjekt i Microsoft Visual Studio .NET eller i Microsoft Visual Studio 2005:

    1. Pek på Ny på Fil-menyen, og klikk deretter Prosjekt.

    2. Klikk på Visual C# Prosjekter under Prosjekttyper.

      Obs! I Microsoft Visual C# 2005 klikker du Visual C# under Prosjekttyper.

    3. Klikk Konsollprogram under Maler.

    4. Klikk på OK. Som standard opprettes en fil med navnet Class1.cs.

      Obs! I Microsoft Visual C# 2005 opprettes Program.cs som standard.

  2. Legg til en referanse til objektbiblioteket i Outlook 2002 eller objektbiblioteket i Outlook 2003. Gjør dette ved å følge disse trinnene:

    1. Klikk Legg til referanse på Prosjekt-menyen.

    2. Klikk COM-fanen .

    3. Klikk Microsoft Outlook 11.0-objektbibliotek på COM-fanen hvis du bruker Outlook 2003, eller klikk Microsoft Outlook 10.0-objektbibliotek hvis du bruker Outlook 2002.

    4. Klikk Velg.

      I Visual C# 2005 trenger du ikke å klikke på Velg.

    5. Klikk OK i dialogboksen Legg til referanser.

      Obs! Hvis du mottar en melding om å generere innpakninger for bibliotekene du valgte, klikker du på Ja.

  3. Erstatt all eksisterende kode med følgende kode i Class1.cs kodevinduet:

    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 SendHTMLMail
    {
       public class Class1
       {
          public static int Main(string[] args)
          {
             try
             {
                // Create the Outlook application.
                Outlook.Application  oApp = new Outlook.Application();
    
                // Get the NameSpace and Logon information.
                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    
                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true); 
    
                // Alternate logon method that uses a specific profile.
                // TODO: If you use this logon method, 
                //  change the profile name to an appropriate value.
                //oNS.Logon("YourValidProfile", Missing.Value, false, true); 
    
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    
                // Set the subject.
                oMsg.Subject = "Send Using OOM in C#";
    
                // Set HTMLBody.
                String sHtml;
                sHtml = "<HTML>\n" + 
                   "<HEAD>\n" +
                   "<TITLE>Sample GIF</TITLE>\n" +
                   "</HEAD>\n" +
                   "<BODY><P>\n" + 
                   "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n" +
                   "</BODY>\n" + 
                   "</HTML>";
                oMsg.HTMLBody = sHtml;
    
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // TODO: Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("email address");
                oRecip.Resolve();
    
                // Send.
                oMsg.Send();
    
                // Log off.
                oNS.Logoff();
    
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;
             }
    
             // Simple error handling.
             catch (Exception e)
             {
                Console.WriteLine("{0} Exception caught.", e);
             }  
    
             // Default return value.
             return 0;
    
          }
       }
    }
    
  4. Gjør eventuelle nødvendige endringer i denne koden der du ser "TO DO"-kommentarene.

  5. Trykk F5 for å bygge, og kjør deretter programmet.

  6. Kontroller at e-postmeldingen er sendt og mottatt.

Kilder

Hvis du vil ha mer informasjon, kan du gå til følgende nettsted for Microsoft Developer Network (MSDN):

http://msdn2.microsoft.com/en-us/library/aa188489(Office.10).aspx Hvis du vil ha mer informasjon om sikkerhetsfunksjonene i Outlook 2002 for e-post og om hvordan disse funksjonene kan påvirke egendefinerte løsninger, klikker du følgende artikkelnummer for å vise artikkelen i Microsoft Knowledge Base:

290500 Beskrivelse av utviklerrelaterte sikkerhetsfunksjoner for e-post i Outlook 2002