SendMail in Console App Failing

To begin, let me please say that I have researched this issue, and as you will see, believe that I have coded everything correctly based on posts from other very knowledgeable users. That said, my hair is kept short to keep me from pulling it out.

The problem. I have a console app that runs as a Windows service. It has been running for quite some time, as has another service that uses the same code. This service, among other things, sends status emails when it is processing data transactions. I have been adding capabilities recently, and suddenly last week it was no longer able to send any outgoing emails. None. Nada. Zip. I made no changes to the routine that sends these emails, so this was particularly troubling.

After much hair pulling I decided to drop the routine that sends the emails into a windowed app to test it, and behold, it worked! I then dropped the exact same code into a console app and it did not work!

For this testing I am using hMail server on the same desktop as the IDE, as I like the logging info and do not have to worry about firewall issues. The message for failures is: The read operation failed. Bytes transferred: 0 Remote IP: 127.0.0.1

Now for the meat. Below is the routine that sends the email. In the console app, the Run event calls SendEmail with a subject and message. That’s it. If I don’t have the For-Next loop right after send message, the email fails. As you can see, the While loop polling the socket is present, as per other postings, and always has been. It does not, however, seem to be working.

Will someone please tell me what I am doing wrong, and if you can, why this routine (minus the For-Next loop) has been working for weeks without a hitch? I have now burned a day on a time sensitive project that really isn’t about this email. I have only so much hair to pull.

Thanks,
Matthew

[code]’ Send an email as requested

Dim i as Integer

dim dtmNow as new Date

Dim msgMail as EmailMessage

Dim sckSMTP as SMTPSecureSocket

’ Create an instance:
sckSMTP = new SMTPSecureSocket

sckSMTP.Address = “127.0.0.1”
sckSMTP.port = 25
sckSMTP.Username = “^^^^^^^^^^^^^^”
sckSMTP.Password = “**************”

’ Create a new message
msgMail = New EmailMessage

’ Sender and Subject
msgMail.fromAddress= “sender@email.net
msgMail.subject= strSubject

’ The message
msgMail.bodyPlainText = strMessage

’ Add Recipient(s)
msgMail.AddRecipient “recipient@email.com

’ Add it to list of messages
sckSMTP.messages.append msgMail

’ Send message
sckSMTP.SendMail

’ This should NOT be necessary
for i = 1 to 100
app.DoEvents
sckSMTP.poll

next

’ Wait for the sending to be done if you’re going to send another
while sckSMTP.BytesLeftToSend > 0
sckSMTP.poll
wend[/code]

Are you running this on Windows 10 per chance?

No. Win 8. I also tried on a Win 10 box on another network just to be certain, but the results were the same.

From what your describing sounds like Microsoft’s new policies of blocking command line apps from having network access unless they are signed. I’ve run into that with some utilities we had written to send emails. Are you using the latest Xojo or an older release?

Latest version (2018r2). Very interesting. I’ll do some digging on that. Thanks for the tip!

[code]Dim sckSMTP as SMTPSecureSocket

’ Create an instance:
sckSMTP = new SMTPSecureSocket

sckSMTP.Address = “127.0.0.1”
sckSMTP.port = 25
sckSMTP.Username = “^^^^^^^^^^^^^^”
sckSMTP.Password = “**************”[/code]
You’re not setting the Secure property to True or the ConnectionType property.

Secure email is typically not sent over port 25. Maybe you were using SMTPSocket before?

No. I’ve been using SMTPSecureSocket for months, since my client switched to an external email provider that requires it. I have never set Secure or ConnectionType, but will try it.

Thanks!

It’s also possible that something has changed on the provider’s side. You should check with them.

Well, I found that I could send if I set Secure = False and looped about 100 times with DoEvents in the loop.

I am monitoring via my local hMail server, so the external provider is out of the loop on this one.

With further testing, I see that 5 times in the loop is too few, but 50 is sufficient to get all of the data out of the send buffer. However, when I run this change in my service, 100 loops with DoEvents takes over 10 seconds! I have no idea what it is doing in that interval, as I have break points set after the SendEmail call and I am not hitting them until after the send is complete.

I am not seeing this on my web apps or console helpers on Windows 10, server 2012R2 or server 2016.

But I do use an event driven model for my apps.