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 IsConnected fa-lock-32.png SMTPConnectionType
BytesAvailable fa-lock-32.png LocalAddress fa-lock-32.png SSLConnected fa-lock-32.png
BytesLeftToSend fa-lock-32.png Messages SSLConnecting fa-lock-32.png
CertificateFile NetworkInterface SSLConnectionType
CertificatePassword Password SSLEnabled
CertificateRejectionFile Port UserName
Handle fa-lock-32.png RemoteAddress fa-lock-32.png
Methods
Close Listen ReadAll
Connect Lookahead RemoveAllMessages
Disconnect Poll SendMail
DisconnectFromServer Purge

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.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:

Var 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.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. ]