TSmtp

Source: source/classes/tsmtp.prg

TSmtp is FiveWin's email-sending class. It implements the SMTP protocol to send messages through a mail server, with support for authentication (AUTH LOGIN), HTML content, file attachments, CC/BCC recipients, and return receipts. It is built on top of TSocket and uses the asynchronous event model for communication.

Key DATA Members

DATATypeDefaultDescription
cIPServerCharacterSMTP server hostname or IP address
nPortNumeric25SMTP server port
cFromCharacterSender email address
aToArray{}Array of recipient email addresses
aCCArray{}Array of CC recipient addresses
aBCCArray{}Array of BCC recipient addresses
cSubjectCharacter"[no subject]"Email subject line
cMsgCharacter""Plain text message body
cHTMLCharacter""HTML message body (alternative to plain text)
aFilesArray{}Array of file paths to attach
cPriorityCharacter"Normal"Message priority: Normal, High, or Low
cUserCharacter""Username for SMTP authentication
cPasswordCharacter""Password for SMTP authentication
lDoAuthLogical.F.Enable SMTP authentication (AUTH LOGIN)
cReplyToCharacterReply-To email address
lReceiptLogical.F.Request a return receipt (Disposition-Notification-To)
cMailerCharacter"FiveWin Mailer"X-Mailer header value
cErrorCharacter""Error description if sending fails
bConnectingBlockCallback during connection
bConnectedBlockCallback when connected to server
bDoneBlockCallback when message is successfully sent
bFailureBlockCallback when an error occurs

Methods

MethodDescription
New( cIPServer, nPort, lAuth, cUser, cPassword )Create a new SMTP session. nPort defaults to 25. Resolves hostname to IP if needed.
SendMail( cFrom, aTo, cMsg, cSubject, aFiles, aCC, aBCC, lReceipt, cHTML )Compose and send an email. All parameters after aTo are optional.
End()Close the underlying socket and clean up
Priority()Return numeric priority: 1=High, 3=Normal, 5=Low

Example: Send a Simple Email

#include "FiveWin.ch"

function Main()

   local oSmtp

   oSmtp := TSmtp():New( "mail.example.com" )

   oSmtp:SendMail( "sender@example.com", ;
                   { "recipient@example.com" }, ;
                   "Hello, this is a test message.", ;
                   "Test Subject" )

return nil

Example: Authenticated SMTP with Attachment

#include "FiveWin.ch"

function Main()

   local oSmtp

   // Gmail SMTP with authentication
   oSmtp := TSmtp():New( "smtp.gmail.com", 587, .T., ;
                         "user@gmail.com", "password" )

   oSmtp:bConnecting := { || MsgInfo( "Connecting to SMTP server..." ) }
   oSmtp:bDone       := { || MsgInfo( "Email sent successfully!" ) }
   oSmtp:bFailure    := { |o,n,c| MsgStop( "Error: " + oSmtp:cError ) }

   oSmtp:SendMail( "user@gmail.com", ;
                   { "recipient@example.com" }, ;
                   "Please find the attached report.", ;
                   "Monthly Report", ;
                   { "C:\reports\report.pdf" } )

return nil

Example: HTML Email with CC

#include "FiveWin.ch"

function Main()

   local oSmtp, cHtml

   TEXT INTO cHtml
   <html>
   <body style="font-family: Arial; color: #333;">
      <h1>Welcome!</h1>
      <p>This is an <b>HTML email</b> sent from FiveWin.</p>
      <hr>
      <p>Regards,<br>The FiveWin Team</p>
   </body>
   </html>
   ENDTEXT

   oSmtp := TSmtp():New( "mail.example.com" )

   oSmtp:SendMail( "sender@example.com", ;
                   { "alice@example.com" }, ;
                   "Plain text fallback message.", ;
                   "HTML Email Test", , ;
                   { "bob@example.com" }, ;   // CC
                   .F., cHtml )

return nil

Notes

See Also