Gracefully detect errors with SMTPSocket

I’m trying to utilize Xojos SMTPSocket and SMTPSecureSocket features. I made some test code from samples in the documenation and here on the forums that works great, no problems at all…

But now I’m attempting to harden the code so it will be able to detect execptions and failures, and I’m running into some problems. No matter what fault I simulate I’m unable to get .LastErrorCode to be anything other than zero, and no exceptions occur to be caught either. Even when I remove my exception handlers, no exceptions occur to cause the IDE to break on the exception. Can anyone please share the suggested way to detect errors and exceptions while using SMTPSocket or SMTPSecureSocket?

Here is some example code that on my Windows Vista 64 bit (fully updated and current) Xojo IDE that will not detect failures or exceptions. In fact I can run this code exactly as shown below and it works the same (no errors) as it does if I replace credentials and server info with real values.

I’m going to try this out on my Mac Mini I have around here somewhere to see if its an OS issue, if it is no problem, my goal is to deploy on Linux but it would be nice if I could test this aspect of the code on my main Windows dev machine.


Sub sendTestEmail()
  
  dim smtpss as SMTPSecureSocket
  smtpss = new SMTPSecureSocket
  smtpss.Address = "my.server.com"
  smtpss.Username = "myUserName"
  smtpss.Password = "myPassword"
  smtpss.Port = 587
  
  dim m As EmailMessage
  m = new EmailMessage
  m.FromAddress = "from@somewhere.com"
  m.Subject = "Xojo Email Test"
  m.BodyPlainText = "This plain text content body part of this email came from a Xojo app"
  m.BodyHTML = "This <b>HTML</b> content body part of this email came from a Xojo app"
  m.AddRecipient "to@somewhere.com"
  
  smtpss.Messages.Append m
  smtpss.SendMail
  
  'Gracefully handle common errors
  Dim myLastErrorCode as integer = smtpss.LastErrorCode
  MsgBox "myLastErrorCode = " + Str(myLastErrorCode)
  
Exception e as RuntimeException
  'Detect other errors we don't yet explicitly handle
  MsgBox "An exception of type " + e.Type + " was caught."
  
End Sub


I’m using this general exception handler, but I haven’t been able to take it any further than this because I’m not able to get any exceptions out SMTPSocket or SMTPSecureSocket.


Function Type(extends e as RuntimeException) As String
  dim T as Introspection.TypeInfo = Introspection.GetType(e)  
  
  if T <> nil then    
    return T.FullName  
  else    
    //this should never happen...    
    return ""  
  end if
  
End Function

smtpss.SendMail is an asynchronous call. You’ll have to look at events this socket defines to keep track of the status. I haven’t seen them thow exceptions. They indicate errors via events.

-Brad

Brad,

Thanks, that was a major mistake on my end, I should have studied SMTPSocket more.

I reveiewed http://documentation.xojo.com/index.php/SMTPSecureSocket and added handlers but I’m still not getting any of these events to fire when I test. Just got my MacMini set back up so I’ll test on that shortly to rule out any issues with trying this on a Windows environment.


  smtpss.Messages.Append m

  AddHandler smtpss.Error , Addressof MailError
  AddHandler smtpss.ServerError , Addressof MailServerError
  AddHandler smtpss.ConnectionEstablished , Addressof MailConnectionEstablished 
  AddHandler smtpss.MailSent, Addressof MailSent 
  
  smtpss.SendMail

Opps - spoke to soon, got the expected error! It just takes 20-30 seconds which is fine. This is great, glad I can trap errors to log and instrument them. Thanks again for your help.

I’ve managed to get the events to fire when added with AddHandler, but when the events are added through the GUI (with Add to … Add Event Handler) they never fire. I’ve not gotten any events added with the GUI to fire for a SMTPSecureSocket.

Odd… I don’t think that’s expected behavior. But at least they work when added manually with AddHandler.

This post was very helpful. Thanks.

With the sub SendTestEmail I’d be surprised if you got anything to work. The smtpss object would go out of scope before anything happened.

I have not been able to get the SMTPSecureSocket to work as expected yet. Using the details from the Intro to Programming book, I can successfully send a message through gmail. But when I add all of the available event handlers to the SMTPSecureSocket object, none of them seem to fire off when running the examples. I put a MsgBox in each event handler just to see if perhaps the asynchronous nature of the SendEmail method were causing some issues.

Yet when I manually add event handlers with AddHandler, some events do fire. I get a MailSent event when SendEmail is done with its task sent to the AddHandler. At least that works as expected.

The problem is that the SMTPSecureSocket Connect method does not seem to work. Aside from not firing off its ConnectionEstablished method, it seems to immediately drop the connection and set LastErrorCode to code 102. This is using the exact same details from the SendMail example, so I’m quite sure the connection parameters are right. There doesn’t seem to be an event for a connection error though.

When calling SendMail with an empty email list, there is no connection made.

How can I test a SMTPSecureSocket connection without sending at least one message?

I’ve never been able to get a ConnectionEstablished event on an SMTPSecureSocket, and would dearly love to see that “greeting” string! Is there something wrong with the ConnectionEstablished event on the SMTPSecureSocket?

Thank you for your help,

Mars

[quote=34179:@Michael Mars Landis]The problem is that the SMTPSecureSocket Connect method does not seem to work. Aside from not firing off its ConnectionEstablished method, it seems to immediately drop the connection and set LastErrorCode to code 102. This is using the exact same details from the SendMail example, so I’m quite sure the connection parameters are right. There doesn’t seem to be an event for a connection error though.
[/quote]
You should not use the Connect method on an SMTPSocket. It is a carryover from the underlying socket classes and really should throw an exception if you try to use it. The documentation is wrong here. It should not expose this method.

[quote]When calling SendMail with an empty email list, there is no connection made.

How can I test a SMTPSecureSocket connection without sending at least one message?
[/quote]
I don’t think you can.

Thanks Tim that’s nice to know. I’ve also discovered that when executing SendMail, if the connection parameters aren’t correct and the connection is not able to be established, there is absolutely no feedback via any events to the class, and ErrorCode is not set.

I am experimenting with using SSLSocket / TCPSocket to merely connect to a server first, and then disconnect, just to see if it is there and can be connected to with the parameters given. Do you have any experience with that?

This seems like a bug in SMTPSecureSocket to me, because a subclass should support the functionality of its superclass at a minimal level where it makes sense to do so.

Any more feedback is always appreciated.

I am interested in where this ultimately ended. I too am having issues with the asynchronicity of the socket. I send mail, but have not feedback from a mailsent event to know if anything went wrong and why.

Does anyone know if the server handling the socket would have logs for this what could be examined on the other end?

if you need async or sync mail and more options, especially a verbose error log, maybe you want to try the MBS CURL Plugin?

I recently switched all apps to CURL when I discovered the security problem with SMTPSecureSocket.