Sending Email Class

Over time, we’ve witnessed many levels of frustration with getting email right in our code. Here, on the NUG, and on ye olde forums.

Could it be that such a project example already exists? Is the Email example in the Examples -> Communication -> Internet -> Email Example thorough and complete for each connection type?

As long as you don’t use port 25 for sending emails you should be fine. As usual ymmv.

This is what I’m working through. It seems to me that there should be a class that fully deals with all of the intricacies of sending email by now. After spending a few hours looking at that example project, I found it greatly lacking in anything more than basic, non-secure, port 25 operation.

The example, while demonstrating the building of an outbound mail object, doesn’t discuss SSL or TLS options with SecureSocket nor using alternate ports. The information in the User Guide Book 3: Framework is so basic as to be less than helpful. Looking at the Language Reference is even more barren of example info.

It just seems that every time someone asks about including email sending within their project, we revisit the same sequence of minimal snippets.

Has anyone put together a class or methods that handle sending mail beyond the port 25 example?

I believe it’s the same.

  Dim s As New SMTPSecureSocket
  s.Address = "secure.mail.server.com"
  
  Dim m As New EmailMessage
  m.Subject = "Hi"
  m.Body = "Hi"
  m.AddRecipient "john@doe.com"
  
  s.SendMail m

That doesn’t include error handling, but will send email securely.

Oh, if you want to do non-port 25 stuff, just add something like this to the above code, port 333 being a silly one for example:

s.Port = 333

It’s not clear to me what exactly you are asking for, but there are two examples included with Xojo: Email Example and the EmailSSLExample. Both demonstrate how to send and receive email. I believe the SSL one works with Gmail.

Hi Jeremy,

That’s exactly what I was talking about - lots of small code snippets.

I’ve dug back through the NUG (I archived them all since 2005) as well as the forums and it’s always the same thing - someone asks how and the answer is referential rather than solution. And the requests almost always stem from the lack of cohesiveness in the RS / Xojo documentation for the various framework elements that need to come together to make things work.

Paul, I guess my vision is an all-in-one, this is how you send mail under all conditions while double checking the success or failure of the operation example and layer of documentation.

Aha - and EmailSSLExample didn’t exist in the previous versions’ Examples folder :P.

Those details are host-specific. How would the Xojo documentation cover them? Just trying to understand what you’re asking for. It’s basically the same steps you go through when setting up your email client for a new provider.

I’m not recommending solving the problem “magically”, just suggesting that the examples and documentation could be a little more complete. As it stands, the User Guide and the Language Reference don’t provide a single point of reference for “How do I send an email from my Xojo project?”.

Look at it from the perspective of a new Xojo user.

Most of this is due to getting the details right for your email provider. The Xojo piece is pretty simple. Unfortunately, it’s a black box and it either works or it doesn’t, so it’s hard to diagnose.

amen!!

That’s the thing. If you know which “parts” (API) need to be used to assemble starship Email…

It’s the knowing which parts and what order those parts need to be assembled. With an upcoming update, a new project for SSL / secure mail is included in the examples. That’s a step in the right direction.

I’ll wager that you guys thought I was done with this … Here’s where I am logically -

To create a mechanism for sending email in an app, we need the following:

  • 2 Classes - SMTPSocket and SMTPSecureSocket (discounting POP3 since this is send only)
  • Methods in those classes to handle Connection, error monitoring, sending the mail
    A front end that allows the user to:
  • Select non-secure or SSL connections
  • Specify The mail host
  • Specify The authentication credentials
  • Specify a non-standard port

Workflow steps:
Class MailSocket -

  • Add a Class
  • Set Class Super to SocketCore -> TCPSocket -> SMTPSocket
    Class SecureMailSocket -
  • Add a Class
  • Set Class Super to SocketCore -> TCPSocket -> SSLSocket -> SMTPSecureSocket
    Add a module named “MailHandler” set to Global and add The following methods:
  • mhConnectionEstablished
  • mhMailError
  • mhServerError
  • mhMailSent
  • mhMessageSent
  • mhSendProgress

These are to provide a single set of code for the class events (of the same base names) to call.

Add the event handlers to each class and call the equivalently named method

From a general perspective, am I mapping this correctly?

Seems right to me. If using a centralized module, MailHandler, maybe you want to add helper methods?

Sub SendMail(fromAddress As String, toAddress As String, subject As String, body As String)
Sub SendMail(msg As EmailMessage)

@Jeremy Cowgar, that’s where I was heading for the controller. I’m just trying to get things sorted from a workflow point of view.

BTW - the result of all of this will hopefully be an easy to follow “HOWTO” that will help provide a solution-oriented answer instead of the referential answers that I’ve found previously.

You might be able to simplify this a bit by just using SMTPSecureSocket and setting the Secure property to False when you don’t need a secure connection.

@Paul Lefebvre - Another example of how scattered this information is. That is non-intuitive and I thought that the SMTPSecureSocket.Secure property was a read-only property to determine if the connection was secure.

That will simplify things drastically.

@Paul Lefebvre - Is that possible in RS as well as Xojo? I can’t get theSocket.Secure to autocomplete except if I’m reading it.

Seems to autocomplete for me if I add a STMPSecureSocket subclass to a window with RS2012r2.1.

Found it - I was using:

Dim theSocket As SMTPSocket ... theSocket = New MailSocket

MailSocket was an SMTPSecureSocket, but theSocket was not.