Web Email Attempt

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

MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
MailSocket.Secure = True
MailSocket.UserName = “my gmail username”
MailSocket.Password = “app password generated by google”

’ 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

try add a new class to your project and set super to SMTPSecureSocket, then add all events.

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.

Peter;

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

hope it helps.

LD

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.

Peter;

Initializing the property in your code at the beginning of your method will fix the nilobjectexception.

I suggest something like this:

Me.MailSocket = New SMTPSecureSocket

You should be good to go after that.

LD

i tried with my google mail account, i got error event message :frowning:

you need to set-up an application password specific to your application. The regular account password will not work.

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

If you do that then your socket is probably going out of scope before the email can be sent.

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.

Oh… @Beatrix_Willius beat me to the finish line. :wink:

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

MailSocket = New SMTPSecureSocket
MailSocket.Address = “smtp.gmail.com
MailSocket.Port = 587
MailSocket.ConnectionType = SMTPSecureSocket.TLSv12
MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
MailSocket.SSLEnabled = True

MailSocket.Secure = True
MailSocket.UserName = “(my gmail email address)”
MailSocket.Password = “(my app password)”

mail.fromAddress = “(my gmail email address)”

mail.subject = “Xojo email test”

strMessage = “sent from port 587”

mail.bodyPlainText = strMessage

mail.AddRecipient(“(my yahoo email address”)

MailSocket.Messages.Add mail
MailSocket.SendMail

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.

i not saw this option in gmail settings.
have you an screenshot as reference point?

1 Like

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.

2 Likes