I’ve been reading post after post and taking code from examples to try and piece together how to send en email from a xojo web app. I have enabled 2FA on my gmail account and am using the app password provided by gmail. When I run this code, I don’t receive any error messages (in Xojo), but don’t receive an email either. I’m not sure where or how to look for any other error messages that might be occurring. I am not using any other components or plugins–just the code below in a button. Any thoughts on what I’m doing wrong?
Dim MailSocket As New SMTPSecureSocket
MailSocket.Address = “smtp.gmail.com”
MailSocket.Port = 567
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
’ populate the email message
Var mail As EmailMessage
mail = New EmailMessage
mail.AddRecipient “my yahoo email address”
mail.FromAddress = “my gmail address”
mail.Subject = “My Subject”
mail.BodyPlainText = “Hello from port 567”
mail.Headers.AddHeader(“X-Mailer”,“SMTP Test”)
’ send the email
MailSocket.Messages.AddRow(mail)
MailSocket.SendMail
Hmm,
I did try creating a class of SMTPSecureSocket, added the events, and then coded each event to output their variables to a textbox so I could see if they were being triggered. So far, nothing is being triggered so I must be doing something else entirely incorrectly. I’ll keep digging.
Try to create your SMTPSecureSocket as a property of the page, container or dialog. Also, try specifying port 587 instead of port 567 (I believe this to be a typo on your part). Works for me. Finally, my working code uses TLS V1.2 instead of TLS V1. You may also want to try that if the port change does not fix the issue
Thanks, I did update the incorrect port and changed to TLSv12. Unfortunately, adding MailSocket as an SMTPSecureSocket property to the page causes a nilobjecterror whenever I try to assign a value to any of its properties (such as Address, port, etc.) Can’t quite figure that one out.
I do have an application password. My understanding is that all I do is replace my regular user account password with the application password and keep everything else such as the user name and settings the same. I’m not sure if that is accurate, but that’s what I found in one post.
Yes, I noticed that you are using it in your code. I was making the comment to Markus.
Did you get past the nilobjectexception? initializing a New mailSocket (see my post above) before assigning values to its properties should resolve it and you should be good with the code that I see above.
I tried putting the initialization in the Open event of the web page, but it still gave me the nilobject exception for some reason. If I declare the mailsocket in code and do not have it as a property of the page, it doesn’t generate an error. I’m sure there is something very obvious that I’m missing so I’ll try starting over from the beginning today
It is better to declare the MailSocket as a property of the page because it may go out of scope either when sending mail or if you wish to use some of the events.
Try initializing the New MailSocket in the same procedure that you are assigning properties to it.
Ohhhh. okay. I’ll work on that approach some more today. Thank you for the feedback. I feel like I’m stumbling in the dark by myself. The advice at last gives me a glimmer of hope!
By initiating the MailSocket variable inside the procedure, the nilobject error WAS eliminated. Using the code below now produces no error, but no email. I’m wondering if using gmail is just so difficult that finding an alternative SMTP mail service would be the solution.
dim mail as new EmailMessage
dim strMessage as String
I am using GMail successfully in pretty much the same way that you are. I am on a task right now, I will review your code a bit later and compare with mine. Perhaps there is a small detail. But rest assured, you can use GMail with yout Xojo web application. I do.
In the mean time, If you enabled the firewall on your dev computer, please check it to make sure that you can send emails. Also verify that you don’t have another applicatin binding to the port and preventing your dev app to do so.
The hoops Google make users jump through are in no small part an effort to deter this type of use. I would encourage you to review their terms of service.