'Class module
Sub Class_Globals
Dim MailSender As SMTP
Dim SendStatus As String = "Idle"
Dim StatusTimer As Timer
End Sub
Public Sub Initialize(SendServer As String, SendPort As Int, SendUser As String, _
SendPass As String, SendEvent As String)
MailSender.Initialize(SendServer, SendPort, SendUser, SendPass, "Mail")
StatusTimer.Initialize("StatusTimer", 25000)
StatusTimer.Enabled = False
End Sub
Sub SendMail(MailToAddress As List, MailCCAddress As List, MailBCCAddress As List, _
MailSubject As String, MailBody As String, _
MailAttachmentDirectory As String, MailAttachmentFile As String) _
As Boolean
If (MailToAddress.Size = 0) _
AND (MailCCAddress.Size = 0) _
AND (MailBCCAddress.Size = 0) _
Then Return False
If MailToAddress.Size <> 0 Then
For i = 0 To MailToAddress.Size - 1
MailSender.To.Add(MailToAddress.Get(i))
Next
End If
If MailCCAddress.Size <> 0 Then
For i = 0 To MailCCAddress.Size - 1
MailSender.CC.Add(MailCCAddress.Get(i))
Next
End If
If MailBCCAddress.Size <> 0 Then
For i = 0 To MailBCCAddress.Size - 1
MailSender.BCC.Add(MailCCAddress.Get(i))
Next
End If
MailSender.Subject = MailSubject
MailSender.Body = MailBody
If File.exists(MailAttachmentDirectory, MailAttachmentFile) Then
MailSender.AddAttachment(MailAttachmentDirectory, MailAttachmentFile)
End If
MailSender.Send
SendStatus = "Sent"
Log("Wait = " & WaitForStatus(2500))
StatusTimer.Enabled = True
Return True
End Sub
Sub WaitForStatus(MaxMilliSeconds As Long) As Long '4/5 seconds max?
Dim StartTime As Long = DateTime.now
Do While DateTime.Now < StartTime + MaxMilliSeconds
If SendStatus <> "Sent" Then Return DateTime.now - StartTime
DoEvents
Loop
Return MaxMilliSeconds
End Sub
Sub Mail_MessageSent(Success As Boolean)
If Success Then
SendStatus = "OK"
Log("mail sent ok")
Else
SendStatus = "Failed"
Log("mail send failed")
End If
StatusTimer.Enabled = False
End Sub
Sub CheckStatus() As String
Return SendStatus
End Sub
Sub StatusTimer_Tick
SendStatus = "TimeOut"
StatusTimer.Enabled = False
End Sub