Cannot send mail from Xojo Cloud

When I run the app in the IDE, everything works as expected. But once the app is deployed to Xojo Cloud, the mail never reaches the recipient.

I read the other posts in this channel, and tried to open the firewall port as instructed, but it does not work either.

Here is my code:

Var fwp As New XojoCloud.FirewallPort(587, XojoCloud.FirewallPort.Direction.Outgoing)
fwp.Open() // This call is synchronous

theMailServer = New SMTPSocket 
theMailServer.Address = ""
theMailServer.Port = 587
theMailServer.Username = ""
theMailServer.Password = p

I will appreciate any help.

Thank you in advance.

The code you’ve shown there just sets up the connection. Is the email sent in this same method?

If not, that firewall port is closing before you get to use it, when it goes out of scope.

Indeed, the code I posted is in app.Open, and the rest is in a button Action.

Here is the code, where I now open the firewall before sending. Yet, it still does not work.

Var fwp As New XojoCloud.FirewallPort(587, XojoCloud.FirewallPort.Direction.Outgoing)
fwp.Open() // This call is synchronous

app.mail.bodyPlainText = TextArea1.Text
app.mail.headers.appendHeader "X-Mailer","Example SMTP Demo"
app.mail.addRecipient ""
app.mail.headers.appendHeader "Reply-To",TextField1.Text
app.theMailServer.messages.append app.mail

If this is async… then i think this still applies:

1 Like

Right, as Jürg said, sendmail is asynchronous so the current method closes and fwp goes out of scope.

How about subclassing SMTPSocket, add an fwp property and then override sendMail to set up the port if it’s not already open, and close it in the Error event.

oh and you probably need that to be an SMTPSecureSocket as well… port 587 expects an encrypted connection usually.

Thats not mentioned in the docs @Geoff_Perlman

1 Like

I have picked the email example, and adapted it to Web 1 (2019R32).

Now it works as expected. I will just have to apply that to my other apps.

I applied what is advised above. All the code is in the same event, and I open the firewall before sending.

Thank you all :slight_smile:

Here is the code in the send button:

if TargetXojoCloud then
  Var fwp As New XojoCloud.FirewallPort(465, XojoCloud.FirewallPort.Direction.Outgoing)
  fwp.Open() // This call is synchronous
end if

// Connect to smtp
MailSocket.Address = ""
MailSocket.Port = 465
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
MailSocket.Secure = True

MailSocket.Username = ""
MailSocket.Password = ""

// Create EmailMessage
Dim mail As New EmailMessage
mail.FromAddress = ""
mail.Subject = SubjectField.Text
mail.BodyPlainText = MessageArea.Text
mail.Headers.AppendHeader("X-Mailer","SMTP Test")

// Send it

Here is the project:

1 Like

You really need to hold on to that reference of the firewall entry. The cleanup of the firewall objects when they’ve gone out of scope is done periodically by the Xojo Cloud server itself and while you’ve been lucky enough not to run into it yet, you most certainly will at some point.

1 Like

I have already modified another one of my apps with that code, including the firewall opening.

It works as expected.