WEB: how to send an email?

Hi,

I searched the forums an the docs, but I did not found a working solution.

How can I send an email from a webapp, for example in an registration process?

Thanks!

From the LR:

SendMailSocket.Address = "mail.mySMTPServer.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 = "bill@yourdomain.com"
mail.Subject = "Test Email from Xojo"
mail.BodyPlainText = "Hello, World!"
mail.Headers.AppendHeader("X-Mailer", "Xojo SMTP Example") // Sample header
mail.AddRecipient("john@domain.com")

// Add the message to the SMTPSocket and send it
SendMailSocket.Messages.Append(mail)
SendMailSocket.Send

That should work. What I’ve done for my latest project is I’ve created a console app that handles all e-mail sending.
The WE app just puts all info the console app needs in a DB. The console app reads whats in the db and delivers the email.
A kind of e-mail queue :slight_smile:

Hi Albin,

thank you! I unfortunately had not purchased the console-packed to build console-apps.

Is there a way to send a mail from the webapp?

if I put this code into my web app
on this lines is the folowing Issue:

SendMailSocket.Messages.Append(mail) SendMailSocket.Send

I used dim SendMailSocket as HTTPSocket

Put (SendMailSocket as SMTPSocket) as property of the page and use:

SendMailSocket = New SMTPSocket

you may want to encrypt your socket?

see
https://forum.xojo.com/22855-smtpsecuresocket-encrypted-transfer

and maybe use our MBS CURL Plugin for that.

no, encryption is not necessary this time. I need to try it on my Server. because I am behind a proxy. And this seems to block the sending mail when I debug my app.

Thanks @Albin Kiland - I hope this works

From the web form I have been using for years :

App.theMailServer As SMTPSocket

if TextArea1.Text = "" then msgBox "Nothing to send. Please enter your message before clicking 'Send'. Thank you." TextArea1.SetFocus Return end if if TextField1.Text = "" then msgBox "Please enter your email address. Thank you." TextField1.SetFocus Return end if Dim mail As New EmailMessage mail.fromAddress= TextField1.Text mail.subject= "Match-Software.com / Message" mail.bodyPlainText = TextArea1.text 'mail.bodyHTML = htmlFld.text mail.headers.appendHeader "X-Mailer","Match SMTP" mail.addRecipient "your@addressemail.net" app.theMailServer.messages.append mail app.theMailServer.sendMail msgBox "Your message has been sent. I will be processed as soon as possible. Thank you." showurl "http://Match-Software.com"

[quote=195076:@Michel Bujardet]

msgBox "Your message has been sent. I will be processed as soon as possible. Thank you." [/quote]

At the risk of being one of these intriguing “Korinthenkacker” things, and unless you are cheese or somesuch, that would be “It will be processed as soon as possible.”

Congratulation for spotting the error. You win a raisin :wink:

Thank you.

Actually a currant, from what i read. :wink:

Are you guys using a properties or initialization/config file to determine the mailserver and port address, or is there a better method to do that?

I wrote an example for a fellow in this thread on how to send through gmail from an app

Gmail Example

If someone wants a copy of the actual project message me and I will email it to you.

We have email in a web app as part of our video training series. Our start to finish web application, “LinkShare”, has integrated email. More information at http://xojo.bkeeney.com/XojoTraining/

[quote=195076:@Michel Bujardet]From the web form I have been using for years :

App.theMailServer As SMTPSocket

if TextArea1.Text = "" then msgBox "Nothing to send. Please enter your message before clicking 'Send'. Thank you." TextArea1.SetFocus Return end if if TextField1.Text = "" then msgBox "Please enter your email address. Thank you." TextField1.SetFocus Return end if Dim mail As New EmailMessage mail.fromAddress= TextField1.Text mail.subject= "Match-Software.com / Message" mail.bodyPlainText = TextArea1.text 'mail.bodyHTML = htmlFld.text mail.headers.appendHeader "X-Mailer","Match SMTP" mail.addRecipient "your@addressemail.net" app.theMailServer.messages.append mail app.theMailServer.sendMail msgBox "Your message has been sent. I will be processed as soon as possible. Thank you." showurl "http://Match-Software.com"[/quote]

I have a question about this… What happens if the mail fails to send at that moment (mail server unreachable, DNS failure, mail rejection, etc…)? Is there any built-in mechanism to retry sending the email?

If no, why does no one ever seem to recommend using the servers built-in mail sending mechanisms with queues, etc that already exists and are already fully developed? I am curious about this. I have always sent email through the existing mail server because I have found rolling your own SMTP connections to be troublesome.

Thoughts?

We save the email messages in the database and if there’s an error it’s marked as not sent. We’ll attempt to send it five times and if it doesn’t send we give up. Once a message is sent we check the email queue for the next email to send.

This is something that you have developed yourself though - correct? There is no built-in mechanism for this is there?

I am just curious (not picking a fight here), especially on a webserver which probably has postfix running on it (if it is linux) - why not just use the built-in smtp daemon? You get 16 years of development experience for free. Is there some reason NOT to use it? Or no, just more fun to roll you own? (which is perfectly legitimate)

I’m doing the exact same thing :slight_smile:

[quote=195222:@John Joyce]I have a question about this… What happens if the mail fails to send at that moment (mail server unreachable, DNS failure, mail rejection, etc…)? Is there any built-in mechanism to retry sending the email?

If no, why does no one ever seem to recommend using the servers built-in mail sending mechanisms with queues, etc that already exists and are already fully developed? I am curious about this. I have always sent email through the existing mail server because I have found rolling your own SMTP connections to be troublesome.[/quote]

That is precisely why I use the SMTP server from my site. Unless really exceptional loss of connectivity exactly at the time the SMTPSocket contacts the server, all messages are queued automatically and the server itself will take care of resending if the recipient is unavailable.

At one point, indeed, an atomic bomb may fall on the roof, but I am not sure I need to live in fear because of that.

All depends what you need to do with the mail. My code is just for a mail form. Maybe ten messages a day tops. If I was to send a mass mailing, I would want a logging system, and probably a way to resend messages.

Correct.

We generally have very little control over which system the web app is on. I could use whatever built-in mail server there is but I know our own implementation works regardless of platform. Is it ideal? No, but it’s lead to zero issues over four or five years of usage.

[quote=195231:@Michel Bujardet]That is precisely why I use the SMTP server from my site. Unless really exceptional loss of connectivity exactly at the time the SMTPSocket contacts the server, all messages are queued automatically and the server itself will take care of resending if the recipient is unavailable.

At one point, indeed, an atomic bomb may fall on the roof, but I am not sure I need to live in fear because of that.

All depends what you need to do with the mail. My code is just for a mail form. Maybe ten messages a day tops. If I was to send a mass mailing, I would want a logging system, and probably a way to resend messages.[/quote]

At first read Michel, I did not realize that you were using your server as an SMTP relay … That’s an interesting approach with little server configuration required.

I totally get it Bob - I personally have had issues in the past with emails not being received and it is a headache. I never took the time to develop a separate email server class… I just went straight to postfix because it was there and already works great. But generally I have a lot of control over the environment I am working in, also administering the server, etc…

Thanks for your answers guys.