Trying to Connect to SendGrid

Greetings - first post, here, as I usually use the NUG.

Trying to make an e-mail based event notifier for remote scientific equipment with satellite internet. I have set up and validated a SendGrid account. Currently testing from a Mac on a large university network. My test code is as follows:

  //set up the socket, Socket1 is an smpt socket 
  Dim Socket1 as SMTPSocket = new SMTPSocket
  Socket1.Address = "smtp.sendgrid.net
  Socket1.Port = 587
  Socket1.Username = myusername
  Socket1.Password = mypassword
  Socket1.connect
  If Socket1.LastErrorCode > 0 then
    MsgBox "The SMPTPSocket in SendMail has detected an error. The ErrorCode is "+str(Socket1.LastErrorCode)
    return false
  End if
  If Socket1.IsConnected then
    MsgBox "Successful connection to the specified SMTP Server!! WAHOO!"
  else
    MsgBox "OOOPSIE! The connection to the SMTP Server was not successful!"
    return false
  End if
  Socket1.Disconnect  //temp for debugging

After the connect operation, there is no non-zero LastErrorCode. It shows a port number other than 587, which, from the Xojo docs, sounds like it was bound to a port at the other end. But, it shows that it IsConnected is false. So, a couple of questions:

  1. Is this a valid test (trying to “connect” to see if the connection is possible)?
  2. Is my code ok?
  3. Is there something I am missing?
  4. Or, are my expectations not realistic?

Thanks
Jim Wagner
Oregon State University & Oregon Research Electronics

Probably need to do something ( a loop ) to poll the socket at least a little so it can get data & process events etc

Thanks, I’ll try that.

Jim

I loop for 1000000 microseconds before the LastErrorCode test.

It then returns error=102 (Lost Connection). Is 1 second too long?

Does this have any implications about how the message is constructed and sent? For example, should the message be constructed before the connection attempt? Is it necessary to wait after the connection request to send the message?

Thanks
Jim

try something like this

//set up the socket, Socket1 is an smtp socket
Dim Socket1 as SMTPSocket = new SMTPSocket
Socket1.Address = "smtp.sendgrid.net
Socket1.Port = 587
Socket1.Username = myusername
Socket1.Password = mypassword

//Here’s the part that’s new

Dim mail as New EmailMessage
mail.fromAddress="test@test.com"
mail.subject="subject"
mail.bodyPlainText = "test"

// send the email
Socket1.messages.append mail
Socket1.sendMail

OK, the connect request test was just being used for my own learning, the walk before you run principle. I’ll go ahead and try that.

Thanks
Jim

That works. The delay was substantial (3-5 minutes), but that is WAAAAY better than nothing, and for free!!

Appreciate your help.

Jim

Make Socket1 a property of a module. It’s asynchronous and may go out of scope before the message is sent.