There seem to be many questions regarding email attachments but I can't find anything similar to my problem so just wondering if anyone can help?
With the code below I've had to add the delay 'WaitForStatus' otherwise the attachment is sometimes sent and sometime not. Can anyone help with why that should be necessary?
Also WaitForStatus always returns the maximum value. Is it blocking the 'Sub Mail_MessageSent(Success As Boolean)'. I assumed that the 'DoEvents' would allow that to progress
Thanks
With the code below I've had to add the delay 'WaitForStatus' otherwise the attachment is sometimes sent and sometime not. Can anyone help with why that should be necessary?
Also WaitForStatus always returns the maximum value. Is it blocking the 'Sub Mail_MessageSent(Success As Boolean)'. I assumed that the 'DoEvents' would allow that to progress
Thanks
B4X:
'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