VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsEmailer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'=========================================
'=          Easy Emailer 2.4             =
'=---------------------------------------=
'=  Developed by P. G. B. Prasanna       =
'=  A Software Developer from Sri Lanka  =
'=  E-mail: pgbsoft@gmail.com            =
'=---------------------------------------=
'=        Copyright © 2015 PGBSoft       =
'=           All rights reserved         =
'=========================================

'This is the email sending class of Easy Emailer.

Option Explicit

Private mvarToAddress As String
Private mvarFromAddress As String
Private mvarSubject As String
Private mvarBody As String
Private mvarServiceTimeout As Long
Private mvarRequestTimeout As Long
Private mvarWbsr As WebBrowser
Private mvarProgressBar As ucCustomProgress

Public Property Set ProgressBar(ByVal vData As ucCustomProgress)
    Set mvarProgressBar = vData
End Property

Public Property Get ProgressBar() As ucCustomProgress
    Set ProgressBar = mvarProgressBar
End Property

Public Property Set wbsr(ByVal vData As WebBrowser)
    Set mvarWbsr = vData
End Property

Public Property Get wbsr() As WebBrowser
    Set wbsr = mvarWbsr
End Property

Public Property Let RequestTimeout(ByVal vData As Long)
    mvarRequestTimeout = vData
End Property

Public Property Get RequestTimeout() As Long
    RequestTimeout = mvarRequestTimeout
End Property

Public Property Let ServiceTimeout(ByVal vData As Long)
    mvarServiceTimeout = vData
End Property

Public Property Get ServiceTimeout() As Long
    ServiceTimeout = mvarServiceTimeout
End Property

Public Property Let Body(ByVal vData As String)
    mvarBody = vData
End Property

Public Property Get Body() As String
    Body = mvarBody
End Property

Public Property Let Subject(ByVal vData As String)
    mvarSubject = vData
End Property

Public Property Get Subject() As String
    Subject = mvarSubject
End Property

Public Property Let FromAddress(ByVal vData As String)
    mvarFromAddress = vData
End Property

Public Property Get FromAddress() As String
    FromAddress = mvarFromAddress
End Property

Public Property Let ToAddress(ByVal vData As String)
    mvarToAddress = vData
End Property

Public Property Get ToAddress() As String
    ToAddress = mvarToAddress
End Property

'Check the relevent service is properly loaded within a given Time Out value.
Private Function IsLoaded() As Boolean

    On Error Resume Next
    
    Dim strPageTxt As String
    Dim lngTryLimit As Long
    Dim lngTryCount As Long
    
    'Set timeout.
    lngTryLimit = ServiceTimeout
    
    'Load the service
    Call LoadSvc
    
    Do Until InStr(strPageTxt, strWL) > 0
        strPageTxt = wbsr.Document.Body.InnerText
        
        'If not busy
        If Not wbsr.Busy Then
            If InStr(strPageTxt, strWL) = 0 Then
                'Load the service
                Call LoadSvc
                DoEvents
            End If
        End If
        
        If CheckConnection = False Then
            IsLoaded = False
            Exit Function
        End If
       
        lngTryCount = lngTryCount + 1
        
        If lngTryCount >= lngTryLimit Then
            IsLoaded = False
            Exit Function
        End If
        DoEvents
    Loop
    
   IsLoaded = True
    
End Function

Private Sub LoadSvc()
    wbsr.Navigate strMURL
End Sub

'Function to send emails.
Public Function SendMail() As Integer
    
   wbsr.Silent = True
    
    If IsLoaded = False Then
        wbsr.Navigate ".."
        ProgressBar.ExecuteProgress 0, False
        SendMail = 7
        Exit Function
    End If
    
    On Error Resume Next

    Dim strPageTxt As String
    
    With wbsr.Document
        .All("femail").InnerText = FromAddress
        .All("temail").InnerText = ToAddress
        .All("sub").InnerText = Subject
        .All("bdy").InnerText = Body
        .All("uid").InnerText = GetUID
        .All("send").Click
        'DoEvents
    End With
    
    strPageTxt = ""
    
    Dim lngTryLimit As Long
    Dim lngTryCount As Long
    
    'Set timeout.
    lngTryLimit = RequestTimeout
    
    Do Until InStr(strPageTxt, "0") Or _
             InStr(strPageTxt, "1") Or _
             InStr(strPageTxt, "2") Or _
             InStr(strPageTxt, "3") Or _
             InStr(strPageTxt, "4") Or _
             InStr(strPageTxt, "5") Or _
             InStr(strPageTxt, "8")
        strPageTxt = wbsr.Document.Body.InnerText
        
        If CheckConnection = False Then
            wbsr.Navigate ".."
            ProgressBar.ExecuteProgress 0, False
            SendMail = 6
            Exit Function
        End If
        
        lngTryCount = lngTryCount + 1
        
        If lngTryCount >= lngTryLimit Then
            wbsr.Navigate ".."
            ProgressBar.ExecuteProgress 0, False
            SendMail = 5
            Exit Function
        End If
        
        DoEvents
    Loop
      
    wbsr.Navigate ".."
    ProgressBar.ExecuteProgress 0, False
    
    SendMail = val(strPageTxt)
    
End Function
