Cdosys.dll 라이브러리를 사용하여 첨부 파일이 있는 전자 메일 메시지를 보내는 방법

원래 KB 번호: 310212

요약

이 문서에서는 Windows 2000 라이브러리(Cdosys.dll)용 CDO(공동 작업 데이터 개체)를 사용하여 첨부 파일이 있는 전자 메일 메시지를 보내는 방법을 설명합니다. 로컬 SMTP 서버를 사용하거나 Microsoft Visual C#의 스마트 호스트 서버를 사용하여 전자 메일 메시지 본문에 텍스트 또는 HTML 또는 웹 페이지를 보낼 수 있습니다.

참고

Cdosys.dll 라이브러리를 CDOSYS라고도 합니다.

추가 정보

요약 섹션에 설명된 대로 CDOSYS를 사용하려면 다음 단계를 수행합니다.

  1. Microsoft Visual Studio를 시작합니다.

  2. 파일 메뉴에서 새로 만들기를 선택한 다음 프로젝트를 선택합니다.

  3. 프로젝트 형식에서 Visual C#을 선택한 다음 템플릿 아래에서 콘솔 애플리케이션 선택합니다. 기본적으로 Program.cs 만들어집니다.

    참고

    Microsoft Visual C# .NET 2003에서 Visual C#Visual C# 프로젝트로 변경됩니다. 기본적으로 Class1.cs 만들어집니다.

  4. Windows 2000 라이브러리용 Microsoft CDO에 대한 참조를 추가합니다. 이렇게 하려면 다음과 같이 하십시오.

    1. 프로젝트 메뉴에서 참조 추가를 선택합니다.

    2. COM 탭에서 Windows 2000 라이브러리용 Microsoft CDO를 찾습니다.

      참고

      Visual C# .NET 2003에서 선택을 선택합니다.

    3. 선택을 수락하려면 참조 추가 대화 상자에서 확인을 선택합니다.

      선택한 라이브러리에 대한 래퍼를 생성하는 대화 상자가 표시되면 예를 선택합니다.

  5. 코드 창에서 모든 코드를 다음 코드로 바꿉 있습니다.

    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. TODO가 코드에 표시되는 경우 표시된 대로 코드를 수정합니다.

  7. 프로그램을 빌드하고 실행하려면 F5 키를 누릅니다.

  8. 전자 메일 메시지가 전송 및 수신되었는지 확인합니다.

참조

Visual Studio를 사용한 Microsoft Office 개발에 대한 자세한 내용은 Visual Studio 를 사용한 Microsoft Office 개발을 참조하세요.