Office 365 E-Mail and Xojo

Office 365 E-Mail and Xojo

The Xojo forum is full of posts on how to send emails; this seems to confuse beginners in particular since email has become a little more complicated in recent years.

There are, of course, many different questions from users about certificates, encryption, X-headers, and sockets. Still, it is then difficult to find the solution to the simple question: How do I send an email to Xojo?

Powerful plugins for Xojo, such as, which can implement almost everything. But in the following code skeleton, I’ll show you how to quickly send an email in native Xojo code with a SMTPSecureSocket:

Var MailSocket as New SMTPSecureSocket

MailSocket.Secure = True
MailSocket.Address = "smtp.office365.com"
MailSocket.ConnectionType = SMTPSecureSocket.TLSv12
MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSTARTTLS
MailSocket.Username = "myO365useremail"
MailSocket.Password = "myO365password"
MailSocket.Port = 587

// Create EmailMessage
Var mail As New EmailMessage
mail.FromAddress = "myO365useremail"
mail.AddRecipient("donald.duck@dontbelieveme.com")
mail.Subject = "Test E-Mail"
mail.BodyPlainText = "This email was sent from a Xojo app."

// Send it
MailSocket.Messages.AddRow(mail)
MailSocket.SendMail

This code is working, but there are a few things to consider: We don’t use any events in this example to check if Xojo sent the E-Mail (it usually will). That’s up to you to adjust.

It is important to note that the SMTPSecureSocket must stay in context until your app sent the e-mail. For example: if you use the above code in a button of your app, it will work, as the control will stay on your webpage (unless the user closes the browser faster than the socket could send the e-mail). If you use this snippet in a method and that methods lose the context after closing, the chances are high that the socket did not have enough time to send your e-mail.

Then there are a few more things to consider. These days, every trustworthy e-mail provider will ensure that the SMTP server can not be misused (too quickly) as a spamming vector. With Office 365, you can only use the e-mail address you are using for your credentials as the sender. Something like this will NOT work:

...
MailSocket.Username = "myO365useremail"
...
...
mail.FromAddress = "anyotheremail"

MailSocket.Username and mail.FromAddress have to be identical for the use in Office365.

If you are testing your shiny new mail algorithm, please apply the KISS principle (Keep it sexy and simple). Almost every e-Mail provider will only allow you to send only a certain amount of e-mails per minute.

Hence, testing your code in a big loop, trying to send out hundreds of e-mails at a time, will not work. You might even get banned for some time, which makes testing even more difficult. Please find below the limitations of the different Office 365 plans as of the time of writing this article:

Source:

Of course, plugins are excellent, specifically MBS because it has superior debug logs embedded. But if a Xojo developer is on a budget or only wants to send an e-mail from time to time from a Xojo Web App: it can be done quickly with native Xojo functionality.

Did you find this article valuable?

Support Jeannot Muller by becoming a sponsor. Any amount is appreciated!