SMTPSocket

From Xojo Documentation

Class (inherits from TCPSocket)

Used to send email via the SMTP protocol. Use the EmailMessage class to create the email message to send.

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

Notes

The POP3Socket and SMTPSocket classes are used together to form the basis of an email client. POP3 is the standard internet protocol for receiving messages and SMTP (Simple Mail Transfer Protocol) is the standard internet protocol for sending emails.

SMTPSocket supports the 'login' authentication type as well as 'plain.' This increases the authentication compatibility with some servers.

If you use a constructor in a subclass of SMTPSocket, you must call the Super class's constructor in your subclass's constructor. The subclass will not work unless this is done.

Gmail

Use SMTPSecureSocket to connect to Gmail.

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

The following code assumes an SMTPSocket named SendMailSocket, has been added to the window.

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

For complete examples that send and receive email, refer to these two projects that are included in the Xojo Examples:

  • Examples/Communication/Internet/Email Example
  • Examples/Communication/Internet/EmailSSLExample

See Also

EmailAttachment, EmailHeaders, EmailMessage, HTTPSecureSocket, HTTPSocket, POP3SecureSocket, POP3Socket, SMTPSecureSocket, SocketCore, TCPSocket classes.