CDO 클래스 사용 smtp 메일 보내기 > vb6.0/vba

본문 바로가기

vb6.0/vba

[vba] CDO 클래스 사용 smtp 메일 보내기

회원사진
하나를하더라도최선을
2022-03-17 17:31 4,542 0

본문



'아래코드는 이석재님께서 작성하신겁니다

'일전에는 위 코드가 먹혀서 이메일을 보냈는데요

'오늘 하니깐 에러가 발생을 합니다.

 

'네이버에서 smtp를 설정했구요

'보내기 에서 에러가 나는데 해결 방안이 없을까요?

 

 

 

Option Explicit

Private Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Private Const cdoSendUsingPort = 2
Private Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Private Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Private Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Private Const cdoSMTPAccountName = "http://schemas.microsoft.com/cdo/configuration/smtpaccountname"
Private Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Private Const cdoBasic = 1
Private Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Private Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"

Private Const strSMTP As String = "smtp.naver.com"
Private Const strID As String = "*******"
Private Const strPW As String = "********"

 

'## 실행테스트...
Sub Test()
    If CDOSendMail("foxmotor@nate.com", "1234@Hotmail.com", "테스트", "ㅎㅎㅎ", "C:\aaa.txt") Then
        MsgBox "메세지 전송 성공"
    End If
End Sub

 

'========================================================================================================
' Procedure : CDOSendMail
' DateTime  : 2007-10-05 14:19
' Author    : 이석재 (http://cafe.naver.com/xlsvba/380
)
' Param     : mailto - 수신자(s)
'             mailfrom - 송신자(s)
'             mailsubject- 제목(s)
'             mailbody - 내용(s)
'             mailattatch - 첨부파일(s)
'=========================================================================================================
Private Function CDOSendMail(mailto As String, _
                       mailfrom As String, _
                       mailsubject As String, _
                       mailbody As String, _
                       mailattatch As String) As Boolean
                       
  Dim oMsg   As Object
  Dim oConf  As Object
   
    On Error GoTo CDOSendMail_Error

    Set oMsg = CreateObject("CDO.Message")              '## CDO.Message 개체에 대한 참조를 작성하고 반환
    Set oConf = CreateObject("CDO.Configuration")       '## CDO.Configuration 개체에 대한 참조를 작성하고 반환
    
    With oConf.Fields
        
        oConf.Load -1
        
        .Refresh                                        '## oConf.Fields Option 초기화
        .Item(cdoSendUsingMethod) = 2                   '## oConf.Fields Option index : 3 (내부계정-1, 외부계정-2)
        .Item(cdoSMTPServer) = strSMTP            '## oConf.Fields Option index : 6 (smtp 계정주소)
        .Item(cdoSMTPConnectionTimeout) = 10            '## oConf.Fields Option index : 7 (Connection 대기시간제한)
        .Item(cdoSMTPAuthenticate) = cdoBasic           '## oConf.Fields Option index : 8 (보안설정 메일서버에 한해)
        .Item(cdoSendUserName) = strID                 '## oConf.Fields Option index : 9 (ID)
        .Item(cdoSendPassword) = strPW                  '## oConf.Fields Option index : 10 (PW)
        '.Item(cdoURLGetLatestVersion) = True           '## oConf.Fields Option index : 11
        .Update                                         '## oConf.Fields Option Update
    End With

    With oMsg
        Set .Configuration = oConf
        
        If InStr(1, mailto, "@", vbTextCompare) Then
            .To = mailto                                    '## 수신주소
            .BodyPart.Charset = "ks_c_5601-1987"            '## 한글설정
            .From = mailfrom                                '## 송신주소
            .Subject = mailsubject                          '## 제목
            .TextBody = mailbody                            '## 내용
            .AutoGenerateTextBody = False                   '## 바디설정:텍스트
            .AddAttachment mailattatch                    '## 첨부파일
'            .HTMLBodyPart.Charset = "ks_c_5601-1987"        '## 바디설정:HTML
            .send                                           '## 보내기

            CDOSendMail = True
        End If
    End With
        
CDOSendMail_Error:

    Call Err_Message("CDOSendMail", "modDGR")

    If Not oMsg Is Nothing Then
        Set oMsg = Nothing                                  '## 메모리제거
    End If
    
    If Not oMsg Is Nothing Then
        Set oConf = Nothing                                 '## 메모리제거
    End If

End Function

 

'================================================================================
' Procedure   : Err_Message
' DateTime    : 2007-06-26 17:05
' Author      : 서은아빠 (
http://cafe.naver.com/xlsvba)
' Purpose     : 오류메세지 처리(모듈위치, 프로시저(함수)명 리턴
' Param       : strFuncName - 해당 함수나 프로시저명
'             : strModuleName - 프로시저를 포함하고 있는 모듈명
'================================================================================

Private Sub Err_Message(ByVal strFuncName As String, ByVal strModuleName As String)
  
  If Err.Number <> 0 Then
      MsgBox "오류가 발생하였습니다." & vbCrLf & _
             "오류의 내용은 " & Err.Description & vbCrLf & _
             "오류의 위치는 Function(Procedure) : " & strFuncName & "Module : " & strModuleName, vbCritical
  End If
 
  On Error GoTo 0
 
End Sub

 

[채택답변] 참조Microsoft CDO for Windows 2000 Library
0
2010-10-06 오후 4:31:15
 질문자 인사 :고맙습니다.

한번 실험해보겠습니다.
  왜구랭번호: 306341  

    Dim iMsg As CDO.Message
    Dim iConf As CDO.Configuration
    Dim strbody As String

    Set iMsg = New CDO.Message
    Set iConf = New CDO.Configuration

        iConf.Load cdoDefaults     ' CDO Source Defaults
        With iConf.Fields
            .Item(cdoSMTPServer) = "" ' SMTP 서버
            .Item(cdoSendUsingMethod) = cdoSendUsingPort ' 보내는 방법을 어떤것을 사용할 건지 선택, 포트, 픽업
            .Item(cdoSMTPServerPort) = ' SMTP 서버 포트
            .Item(cdoSMTPUseSSL) = True ' smtp서버에서 ssl 사용 유무
            .Item(cdoSendUserName) = "" ' 계정 ID
            .Item(cdoSendPassword) = "" ' 계정 암호
            .Item(cdoSMTPAuthenticate) = 1
            .Update
        End With

    strbody = "Hi there"

    With iMsg
        Set .Configuration = iConf ' SMTP 서버를 설정한 개체를 할당한다.
        .To = "" ' 메일을 받을 사람의 메일 주소
        .From = "" ' 보내는 사람 메일 주소 즉 사용하는 SMTP에 있는 메일 계정
        .Subject = "New figures" ' 메일 제목
        .TextBody = strbody ' 메일 내용
        .Send ' 메일을 보낸다.
    End With
    Set iConf = Nothing
    Set iMsg = Nothing

 


출처: http://www.devpia.com/MAEUL/Contents/Detail.aspx?BoardID=47&MAEULNO=19&no=306337&ref=306337

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색