Freigeben über


Verwenden der Cdosys.dll-Bibliothek zum Senden einer E-Mail-Nachricht mit Anlagen

Ursprüngliche KB-Nummer: 310212

Zusammenfassung

In diesem Artikel wird beschrieben, wie Sie die CdO (Collaboration Data Objects) für Windows 2000-Bibliothek (Cdosys.dll) verwenden, um eine E-Mail-Nachricht mit Anlagen zu senden. Sie können Text oder HTML oder eine Webseite im Textkörper der E-Mail-Nachricht mithilfe des lokalen SMTP-Servers oder mithilfe eines Smarthostservers in Microsoft Visual C# senden.

Hinweis

Die Cdosys.dll-Bibliothek wird auch als CDOSYS bezeichnet.

Weitere Informationen

Führen Sie die folgenden Schritte aus, um CDOSYS wie im Abschnitt "Zusammenfassung" beschrieben zu verwenden:

  1. Starten Sie Microsoft Visual Studio.

  2. Wählen Sie im Menü Datei-Neuenaus, und wählen Sie dann Projectaus.

  3. Wählen Sie unter ProjekttypenVisual C#-aus, und wählen Sie dann Konsolenanwendung unter Vorlagenaus. Standardmäßig wird Program.cs erstellt.

    Hinweis

    In Microsoft Visual C# .NET 2003 wird Visual C#- in Visual C#-Projektegeändert. Standardmäßig wird Class1.cs erstellt.

  4. Fügen Sie einen Verweis auf die Microsoft CDO für Windows 2000 Libraryhinzu. Gehen Sie dazu wie folgt vor:

    1. Wählen Sie im Menü ProjectReferenz hinzufügenaus.

    2. Suchen Sie auf der Registerkarte COM- nach Microsoft CDO für Windows 2000 Library.

      Hinweis

      Wählen Sie in Visual C# .NET 2003 Wählen Sieaus.

    3. Wählen Sie OK im Dialogfeld Hinzufügen von Verweisen aus, um Ihre Auswahl zu akzeptieren.

      Wenn Sie ein Dialogfeld zum Generieren von Wrappern für die ausgewählten Bibliotheken erhalten, wählen Sie Jaaus.

  5. Ersetzen Sie im Codefenster den gesamten Code durch den folgenden Code:

    namespace CdoSys {
        using System;
        class Class1 {
            static void Main (string[] args) {
                try {
                    CDO.Message oMsg = new CDO.Message ();
                    CDO.IConfiguration iConfg;
    
                    iConfg = oMsg.Configuration;
    
                    ADODB.Fields oFields;
                    oFields = iConfg.Fields;
    
                    // Set configuration.
                    ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
    
                    //TODO: To send by using the smart host, uncomment the following lines:
                    //oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
                    //oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
                    //oField.Value = "smarthost";
    
                    // TODO: To send by using local SMTP service.
                    //oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                    //oField.Value = 1;
    
                    oFields.Update ();
    
                    // Set common properties from message.
    
                    //TODO: To send text body, uncomment the following line:
                    //oMsg.TextBody = "Hello, how are you doing?";
    
                    //TODO: To send HTML body, uncomment the following lines:
                    //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>\n" +
                    //"</BODY>\n" +
                    //"</HTML>";
                    //oMsg.HTMLBody = sHtml;
    
                    //TOTO: To send WEb page in an e-mail, uncomment the following lines and make changes in TODO section.
                    //TODO: Replace with your preferred Web page
                    //oMsg.CreateMHTMLBody("http://www.microsoft.com",
                    //CDO.CdoMHTMLFlags.cdoSuppressNone,
                    //"", "");
                    oMsg.Subject = "Test SMTP";
    
                    //TODO: Change the To and From address to reflect your information.
                    oMsg.From = "someone@example.com";
                    oMsg.To = "someone@example.com";
                    //ADD attachment.
                    //TODO: Change the path to the file that you want to attach.
                    oMsg.AddAttachment ("C:\\Hello.txt", "", "");
                    oMsg.AddAttachment ("C:\\Test.doc", "", "");
                    oMsg.Send ();
                } catch (Exception e) {
                    Console.WriteLine ("{0} Exception caught.", e);
                }
                return;
            }
        }
    }
    
  6. Wenn TODO im Code angezeigt wird, ändern Sie den Code wie angegeben.

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

  8. Vergewissern Sie sich, dass die E-Mail-Nachricht sowohl gesendet als auch empfangen wurde.

Referenzen

Weitere Informationen zur Microsoft Office-Entwicklung mit Visual Studio finden Sie unter Microsoft Office Development with Visual Studio.