SMTPSecureSocket in console application

I used this code in desktop application that works fine.

[code] Dim mail as New EmailMessage
Dim SMTPSecureSocket1 as New SMTPSecureSocket

SMTPSecureSocket1.address = “smtp.gmail.com
SMTPSecureSocket1.port = 587
SMTPSecureSocket1.Secure = True
SMTPSecureSocket1.ConnectionType = 3
SMTPSecureSocket1.Username = “sender@gmail.com
SMTPSecureSocket1.Password = “mypassword”
SMTPSecureSocket1.Connect

Dim Start as New Date

While 1=1
SMTPSecureSocket1.Connect
Dim Now as New Date
if Now.TotalSeconds - Start.TotalSeconds > 60 Then Exit
if SMTPSecureSocket1.IsConnected Then Exit
Wend

mail.fromAddress= “sender@domain.com
mail.subject= “test”
mail.AddRecipient “recipient@domain.com
mail.bodyPlainText = “Test email”

SMTPSecureSocket1.DeleteAllMessages
SMTPSecureSocket1.Messages.Append mail
SMTPSecureSocket1.SendMail[/code]

The same code in Run.Event of a console application doesn’t send the email.

What’s wrong?

Pietro

If you are using that code as-is, the mostly likely reason is that the Console app is quitting before the message is sent. Console apps exit when the end of the Run event handler is reached. SendMail is asynchronous, so it does not wait for the mail to get sent; it returns immediately.

You probably want a run loop in there to wait until the messages are sent.

You are right!

With:

  Do
    App.DoEvents
  Loop

the email is sended.

Thanks a lot.

Pietro

leave this connect calls away and also the loop waiting for connection.