Sending an email

I gleamed the following code from various places on this forum and in the latest documentation but can’t get an email to successfully go out. Can someone tell me what I’m doing wrong.

I’m using my authentication credentials for my Office 365 account, but I have them blocked out intentionally below.

var MailSocket As New SMTPSecureSocket
MailSocket.secure = True
MailSocket.Address = “smpt.office365.com
MailSocket.SSLConnectionType = SMTPSecureSocket.SSLConnectionTypes.TLSv1
MailSocket.SMTPConnectionType = SMTPSecureSocket.SMTPConnectionTypes.STARTTLS
MailSocket.Username = “jxxxxxx@hotmail.com
MailSocket.Password = “Tanaxxxx”
MailSocket.Port = 587

Var mail1 As New EmailMessage
mail1.FromAddress = tfReplyEmail.Text.Trim
mail1.AddRecipient(tfRecipient.Text.Trim)
mail1.Subject = tfSubject.Text.Trim
mail1.BodyPlainText = taDesc.Text.Trim

'if f_Attachment <> nil and f_Attachment.Exists then
'var attachfile As EmailAttachment
'attachfile = New EmailAttachment
'attachfile.ContentEncoding = “UTF8”
'attachfile.LoadFromFile(f_Attachment)
'mail1.Attachments.Add(attachfile)
'end

MailSocket.Messages.Add(mail1)
MailSocket.SendMail

I should have stated that the reply email address is my hotmail email address just in case someone thought I was using a different one.

Don’t use a local variable. Make MailSocket a property of the window. Also Microsoft wants you to use OAuth now and not SMTP.

Ignorance on my part. The codes works, it’s just that I was closing the form before the email was sent.

Just had to add a message box stating that the email was sent and everything now works.

If you mean what I think you mean, then you better hope the user is not extremely quick in dismissing the message box. If they are, then the mail will sometimes be sent, sometimes not. Better follow @Beatrix_Willius 's advice.

2 Likes

That’s just a bandaid. The code will include a progress bar during the sending of the email and they can’t close the form till the sending of the email completes.

I’ll check out OAuth.

You will also need to catch errors and you can’t do that in your current design.

OAuth is a pain for Microsoft. I managed to set it up, but I have the app configured incorrectly somewhere. Microsoft has a nice developer sandbox. Christian recently did a video for Microsoft 365 for Filemaker which can be applied directly to Xojo.

Do you have a link for the FileMaker setup. I use it for some internal apps.

I think it was this here: Microsoft Office 365 Authentication with oAuth 2.0 in FileMaker with the MBS Plugin - Livestream - YouTube . It was pure gold for all the stupid IDs.