Real Software Forums

SMTPSocket from Windows Service
Page 1 of 1

Author:  Fugu [ Thu Aug 09, 2012 6:41 pm ]
Post subject:  SMTPSocket from Windows Service

I am having an issue get my windows service app to send emails. The service will check a storage location and determine the percentage utilized. Once a thresh hold has been meet it will email out an alert. The first part of the App works fine as I have it report out the percentage in the System.DebugLog and verified from the DebugViewer. But when it runs the EmailReport method I can see from the DebugViewer I get the following output when it tries to send the email "swi_lsp: non-browser app; disable" I am running this service app on a Window-7 64bit system.

I created a class called "nSMTPsocket1" as a SMTPSocket.

Sub EmailReport()
Dim FI2 As FolderItem
Dim TIS As TextInputStream
Dim mail As EmailMessage
Dim Q, LoopX As Integer
Dim Email, EmailAddr As String
Dim cArray(-1) As String
Dim SMTPsocket1 As New nSMTPsocket1

mail = New EmailMessage

SMTPsocket1.Address = ""
SMTPsocket1.Port = 25

FI2 = GetFolderItem("email.cfg")
TIS = TextInputStream.Open(FI2)

Q = 0

Do Until TIS.EOF
EmailAddr = TIS.ReadLine
If EmailAddr.Left(1) <> "#" Then
ReDim cArray(Q)
cArray(Q) = EmailAddr
Q = Q + 1
End If

Email = "Test from windows service app."

mail.subject="Storage Utilization Alert!!"
mail.BodyPlainText = Email
mail.headers.appendHeader "X-Mailer","REALbasic SMTP"

LoopX = 0
Do Until LoopX > UBound(cArray)
mail.AddRecipient cArray(LoopX)
LoopX = LoopX + 1

SMTPsocket1.messages.append mail


Author:  timhare [ Thu Aug 09, 2012 6:49 pm ]
Post subject:  Re: SMTPSocket from Windows Service

You're not giving the socket enough time to send anything. SendMail is an asynchronous process. It will return immediately, before the sending has already begun. Then you disconnect immediately. Also, Smtpsocket1 is a local variable, which will be destroyed as soon as the method finishes, and before the mail is sent. Make it a global property instead. You do not need to explicitly disconnect; the socket will disconnect after it sends the mail.

Author:  Fugu [ Fri Aug 10, 2012 5:44 pm ]
Post subject:  Re: SMTPSocket from Windows Service

Thank you very much for the info and advice.

After trouble shooting my issue further and stripping down the code I found this out.

Test application looks like this. Started from the "Empty Service.rbp" file.

System.DebugLog("Start of Run")

Dim TMR As new SrvcTimer
TMR.Mode = 2
TMR.Period = 30000

While True

Created new Class "SrvcTimer" set to Timer
System.DebugLog("Timer Action")

If I install this as a windows service and then start the service I get the following error that repeats.
[5500] Start of Run
[5500] Runtime Error
[5500] Please report what caused this error along with the information below.
[5500] ../../../Common/ThreadInvoker.cpp: 145
[5500] Failure Condition: IsRBThread() && CurrentThreadHandlesEvents()

If I just double click on my application and lauch it. It runs and from the DebugViewer I can see the DebugLog enteries as the timer cycles every 3 secs.
[5996] Start of Run
[5996] Timer Action
[5996] Timer Action
[5996] Timer Action
[5996] Timer Action

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group