SMTPSecureSocket
From Xojo Documentation
Used to send secure email via the SMTP protocol using SSL or TLS encryption.
Events | ||||||
|
Methods | |||||||||||
|
Notes
The SMTPSecureSocket class is the same as the SMTPSocket class except that it is derived from SSLSocket instead of TCPSocket. As a result, you can use the Secure property of the SSLSocket class to provide secure communications.
If you use a constructor in a subclass of a SMTPSecureSocket, you must call the Super class's constructor in your subclass's constructor. The subclass will not work unless this is done.
Error Codes
For a list of codes, refer here:
Keep in mind that some codes may be server specific.
Gmail
In order to connect to Gmail you'll need to enable the "allow less secure apps" option as described in this Google support document:
https://support.google.com/accounts/answer/6010255
With that enabled, these settings work for most users:
MailSocket.Port = 465
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
MailSocket.SMTPConnectionType = SMTPSecureSocket.SMTPConnectionTypes.SSLTLS
MailSocket.Secure = True
MailSocket.UserName = <YourGmailUserName>
MailSocket.Password = <YourGmailPassword>
Xojo Cloud
To access an SMTP server from web apps running on Xojo Cloud, you will first have to use the FirewallPort class to open the port used to connect to the SMTP Server:
XojoCloud.FirewallPort.Direction.Outgoing)
fwp.Open // This call is synchronous
If fwp.isOpen Then
// Do what you need to do
End If
Sample Code
This code uses an SMTPSecureSocket called MailSocket that has been added to a Window to send a simple email using Gmail. Update the Username, Password and email address in this code to use it.
MailSocket.Address = "smtp.gmail.com"
MailSocket.Port = 465
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
MailSocket.SMTPConnectionType = SMTPSecureSocket.SMTPConnectionTypes.SSLTLS
MailSocket.Secure = True
MailSocket.Username = <YourGmailUsername> // Provide your Gmail username
MailSocket.Password = <YourGmailPassword> // Provide your Gmail password
// Create EmailMessage
Var mail As New EmailMessage
mail.FromAddress = "myemail@mydomain.com" // Your from email address
mail.AddRecipient("recipient@domain.com" // The recipient's email address
mail.Subject = "Test from Xojo"
mail.BodyPlainText = "This email was sent from a Xojo app."
mail.Headers.AddHeader("X-Mailer","SMTP Test")
// Send it
MailSocket.Messages.AddRow(mail)
MailSocket.SendMail
See Also
EmailMessage, HTTPSecureSocket, HTTPSocket, POP3SecureSocket, TCPSocket, POP3Socket, SMTPSocket, SocketCore classes. ]