Followed instructions from Documents on EMail

The OP was experiencing compile errors. In my first post I made a suggestion and asked a question. I’ve yet to see a response to that.

1 Like

Alternative options should always be presented. The user may believe that SMTPSocket is the only way, or maybe even the best way. In my opinion, it’s probably the worst way. As a developer with experience in this department, why would I not share that experience?

To a degree, that’s why Christian is doing: sharing an alternative.

Here’s another alternative: don’t use your own SMTP server. If the project can afford it, farm your emails out to a third party like MailGun, SendGrid, or (my personal favorite) Postmark. You’ll submit the email via HTTP using the URLConnection socket, which is easier and gives you more feedback. Your email will be more deliverable too.

If you really want to get SMTPSocket going, the issue likely scoping. The socket needs to exist long enough to finish delivery. If it were placed on a window that is closing, or the app is exiting, the socket probably goes out of scope before it finishes its work. It’s a very common usage mistake.

Personally I use the SSLSocket for both sending and receiving mail. It’s only text, after all. I subclass SSLSocket and give the subclass the characteristics it needs, including a timer.

The OP was experiencing compile errors. In my first post I made a suggestion and asked a question. I’ve yet to see a response to that.”

Sorry, life has taken me away from my projects. Thank you for the answers o far. I am just now reading them all and working on implementing them.

The email is a simple notice that is sent to our vendors when they sign up for an event. As part of the sign up process a confirmation email is sent to them. Nothing fancy or complex.

Thank you again, I will start digging into the answers as time permits.

2 Likes

No prob. Feel free to pose more questions as and when.

(Meanwhile I’m feeling cheerful as I get my Covid jab in two days).

3 Likes

Okay, so I have made the appropriate corrections (thanks for the pics… really helped to get it set up!).

The program compiles and runs, I click the appropriate button and no errors… and no mail too! Not sure what I am missing.
// *********** JHGC ************
’ Ian McDonald - Develper/Maintainer
’ (999) 999-9999
ian.mcdian@aplaceintheethers.org
’ Dump Microsoft! Go Penquin Computing!
// *********** JHGC ************

// Variable Declarations
Var MailSocket as SMTPSecureSocket
MailSocket = new SMTPSecureSocket

// Test format of email before moving forward...
Var blnTest As Boolean = modSysFunc.mthEmailCheck( txtVendorEmail.Value.ToText )

// Code in Action!
If( blnTest = True ) Then
  MailSocket.Address = "mali@joyfulheartgardens.com" '"smtp.gmail.com"
  MailSocket.Port =  143 ' 465
  MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
  MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
  MailSocket.Secure = True
  'MailSocket.UserName = "creativemalaina@gmail.com"
  'MailSocket.Password = "**************"
  MailSocket.UserName = "mali@joyfulheartgardens.com"
  MailSocket.Password = "****************"
  
  Var email As New EmailMessage
  email.FromAddress = "mali@joyfulheartgardens.com"
  email.Subject = "Hello, world!"
  email.BodyPlainText = "I love Xojo!"
  email.AddRecipient( "ian.mcdian@zoho.com" )
  
  MailSocket.Messages.AddRow( email )
  MailSocket.SendMail
  MessageBox( "It clicked... did it werk?" )
  
Else
  MessageBox( "Apparently you do not know how to write an email address..." )
  Exit
End If

Where have you put this code?

And I would have thought that MailSocket.Address needs to be the address of the remote smtp server.

MailSocket must be a property of the window, not a local variable.
And the sending is asynchronous, so your message box will come before it sends.

You need to make MailSocket and probably email be non-local variables. Otherwise at the end of whereever you’ve put this, they just vanish. Poof!

Are you using an email address instead of a server address?

Yep. As was said in the last part of the solution that was moved:

“something missing from my post above - that of a specific instance if the new class (eg on the page) to make it work and renaming of the ‘MailSocket’ name to whatever the instance is called”

I think I see my mistake… please standby…

I have corrected that mistake… now to figure out the rest…

2 Likes

So where would I put the declarations for the following?

Var MailSocket as SMTPSecureSocket
MailSocket = new SMTPSecureSocket

Var email As New EmailMessage

I did get this corrected:
MailSocket.Address = "smtp.joyfulheartgardens.com" '"smtp.gmail.com"

Make them properties of the window, is probably the simplest for now. You may also want to do some error checking. Look up smtpsocket and email message in the doccy and see what errors you can detect. There may be an error event you can handle.

K, thanks for the push!

Don’t forget to remove the Vars. Otherwise those local declarations will override the more globals ones. But you’ll still need to new them in your code.

Okay so if I code this:

'Var email As New EmailMessage
email.FromAddress = "mali@joyfulheartgardens.com"
email.Subject = "Hello, world!"

I get a Nil Object Exception on this line email.FromAddress = “mali@joyfulheartgardens.com

But if I do this
Var email As New EmailMessage
email.FromAddress = “mali@joyfulheartgardens.com
email.Subject = “Hello, world!”

Nothing… nada… and I did put the MailSocket as a property of the working form (window).

I read what I could find and nothing seems to answer why.

Try uncommenting ‘Var email’ line. As it will be added by reference to the page (window) property it should persist.

IOW this line should take care of the persistence for the ‘email’ object reference variable (and therefore think using var in the above code (for ‘email’ only) should work):

MailSocket.Messages.AddRow( email )

Add

email = new EmailMessage

Before setting the fromAddress