Sending Email Using Outlook/Office 365 and TLS Encryption

A short while back I was looking for help sending email using Microsoft Exchange Server. That same customer has now just changed to using Office 365 and Outlook. My understanding of sockets and email functions in general is less than that of the lowest level neophyte. Although I have found several forum references to it, they seem to just further confuse me. I’ve always sent simple email using the following code:

[code] SendMailSocket.Address = “mail.sc.rr.com” // your SMTP email server
SendMailSocket.Port = 25 // Check your server for the property port # to use

// Create the actual email message
Dim mail As New EmailMessage
mail.FromAddress = “xyz@sc.rr.com
mail.Subject = “Test Email from CaseDMS”
mail.BodyPlainText = “I need my new case model approved for release”
//mail.Headers.AppendHeader(“Case DMS”, “Request for Approval”) // Sample header
mail.headers.appendHeader “X-Mailer”,“Case DMS Request for Approval”
mail.AddRecipient(“abc@sc.rr.com”)

// Add the message to the SMTPSocket and send it
SendMailSocket.Messages.Append(mail)
MailProgress.Visible = true
SendMailSocket.SendMail
[/code]

I found the settings that need to be used for Office 365:
POP Setting
Server name: outlook.office365.com
Port: 995
Encryption method: SSL

IMAP Setting
Server name: outlook.office365.com
Port: 993
Encryption method: SSL

SMTP Setting
Server name: smtp.offoce365.com
Port: 587
Encryption method: TLS

Now, besides the obvious need to change the port to 587 for SMTP, how do I take those settings above required for Office 365 Outlook and modify my code to allow for sending email within their environment? I know the TLS encryption requirement for the SMTP settings has a lot to do with it and I’ve never used this before. My customer tells me that as long as the email is TLS encrypted, the SMTP connection should work. Frankly, with my lack of knowledge about this subject, I’m not even sure I’m asking the question in the right way. ^^

Any help would be greatly appreciated!
… Don

I haven’t done any sending of mails with TLS but I only access Imap accounts. TLS is used the following way:

  • You initiate the connection without SSL.
  • Then you use the the command starttls.

I think this was already discussed previously here.

This has not been tested because I don’t have an Office365 account but try:

[code]SendMailSocket.Secure = True
SendMailSocket.Address = “smtp.office365.com” // your SMTP email server
SendMailSocket.ConnectionType = SMTPSecureSocket.TLSv12
SendMailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSTARTTLS
SendMailSocket.Port = 587 // Check your server for the property port # to use

// Create the actual email message
Dim mail As New EmailMessage
mail.FromAddress = “xyz@sc.rr.com
mail.Subject = “Test Email from CaseDMS”
mail.BodyPlainText = “I need my new case model approved for release”
//mail.Headers.AppendHeader(“Case DMS”, “Request for Approval”) // Sample header
'mail.headers.appendHeader “X-Mailer”,“Case DMS Request for Approval”
mail.AddRecipient(“abc@sc.rr.com”)

// Add the message to the SMTPSocket and send it
SendMailSocket.Messages.Append(mail)
MailProgress.Visible = true
SendMailSocket.SendMail
[/code]
SendMailSocket should be a SMTPSecureSocket. If Office365 does not support TLSv1.2 then try v1.1 or v1.0.

Thanks so much for that info, Beatrix. I appreciate you taking your time to answer. I’m trying to put your input together with all the other “tidbits” I’ve picked up while reading about the subject over the past day or so now.

Not much … I think I spent over 6 hours yesterday searching the Xojo and RealStudio forums … This might be what you’re referring to (at least it’s as close as I found to anything that resembled what I’m looking to do), and although it added a lot to my understanding, it never really arrives at a conclusion (which may just be my ignorance of the subject blinding me to an answer that is there and I just don’t see it) or gives an example that would allow me to see how to modify the code I normally use for email functionality without TLS requirements on SMTP sends.

Bottom line … I’m still searching and reading. I’m going to try and put together a few simple code passes at it today and have my customer try them (I can’t on my end since I don’t use Office 365). Fortunately for me, my customer in this situation has worked a long time with me and has no problem performing my “trial & error” attempts. Not a great way of doing it, but the only way I have at the moment.

Thanks a bunch, Frederick! I’ll give it a go with my customer and see how it flies.

Hi Don, I also spent many hours trying to send email from Xojo code through Office365 without success.
At the end, because I was constrained to solve the problem for my customer, I find a good and simple workaround.

Email relay is an opensource/cross platform SMTP proxy. I’ve installed it into my customer’s server, setting it with Office365 parameters. Than, I’ve setted my Xojo code to send emails through the “pretended SMTP” at localhost:25 and all works fine.

I hope that my experience will be useful for you.

[quote=164312:@Frederick Roller]This has not been tested because I don’t have an Office365 account but try:
[/quote]

I have just tried a variation of Frederick’s suggestion using a username and password:

[code] //theMailServer is a property of type SMTPSecureSocket
theMailServer = New SMTPSecureSocket

theMailServer.Secure = True
theMailServer.Address = “smtp.office365.com” // your SMTP email server
theMailServer.ConnectionType = SMTPSecureSocket.TLSv12
theMailServer.SMTPConnectionMode = SMTPSecureSocket.ModeSTARTTLS
theMailServer.Username = “xyz@sc.rr.com
theMailServer.Password = “YourOffice365Password”
theMailServer.Port = 587 // Check your server for the property port # to use

// Create the actual email message
Dim mail As New EmailMessage
mail.FromAddress = “xyz@sc.rr.com
mail.Subject = “Test Email”
mail.BodyPlainText = “I need my new case model approved for release”

// Add the message to the SMTPSocket and send it
theMailServer.Messages.Append(mail)
theMailServer.SendMail[/code]

And it works! :slight_smile: Thanks Frederick.

1 Like