SMTPSecureSocket

From Xojo Documentation

Class (inherits from SSLSocket)

Used to send secure email via the SMTP protocol using SSL or TLS encryption.

Events
ConnectionEstablished MailSent SendProgress
Error MessageSent ServerError
Properties
Address Handle fa-lock-32.png Port
BytesAvailable fa-lock-32.png IsConnected fa-lock-32.png RemoteAddress fa-lock-32.png
BytesLeftToSend fa-lock-32.png LastErrorCode fa-lock-32.png SMTPConnectionMode
CertificateFile LocalAddress fa-lock-32.png SSLConnected fa-lock-32.png
CertificatePassword Messages SSLConnecting fa-lock-32.png
CertificateRejectionFile NetworkInterface Secure
ConnectionType Password UserName
Methods
Close DisconnectFromServer Purge
Connect Listen ReadAll
DeleteAllMessages Lookahead SendMail
Disconnect Poll

Constants

For use with SMTPConnectionMode property:

  • ModeSTARTTLS = 0
  • ModeSSLTLS = 1

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.Address = "smtp.gmail.com"
MailSocket.Port = 465
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
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:

Dim fwp As New XojoCloud.FirewallPort(587, _
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.

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

MailSocket.Username = <YourGmailUsername> // Provide your Gmail username
MailSocket.Password = <YourGmailPassword> // Provide your Gmail password

// Create EmailMessage
Dim 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.AppendHeader("X-Mailer","SMTP Test")

// Send it
MailSocket.Messages.Append(mail)
MailSocket.SendMail

See Also

EmailMessage, HTTPSecureSocket, HTTPSocket, POP3SecureSocket, TCPSocket, POP3Socket, SMTPSocket, SocketCore classes. ]