TMail

Source: source/classes/tmail.prg

TMail is FiveWin's MAPI (Messaging API) email class. It uses the Windows Simple MAPI interface to send emails through the default email client (such as Microsoft Outlook, Windows Mail, or any MAPI-compliant email application). TMail supports subject, body text, multiple recipients, file attachments, read receipts, and originator information.

Architecture

flowchart LR subgraph "FiveWin Application" A[TMail] end subgraph "Windows MAPI" B[MAPISendMail] C[MAPILogOff] end subgraph "Default Email Client" D[Outlook / Windows Mail / etc.] end subgraph "Email Output" E[To: Recipients] F[Subject / Body] G[Attachments] end A -->|"Activate()"| B A -->|"End()"| C B --> D D --> E D --> F D --> G

Key DATA Members

DATATypeDefaultDescription
cSubjectCharacter""Email subject line
cNoteTextCharacter""Email body text
cMsgTypeCharacterMessage type (optional, e.g. "IPM.Note")
cConversationIDCharacter""Conversation thread identifier
dDateDateDate()Message date
cTimeCharacterTime()Message time
lReceiptLogical.F.Request a read receipt
lFromUserLogical.T.Prompt user before sending
aOriginArray{}Originator address information
aRecipientsArray{}Array of recipients; each item is { cName [, cAddress ] }
aFilesArray{}Array of file paths to attach
nRetCodeNumeric0Return code from MAPISendMail (0 = success)

Methods

MethodDescription
New( cSubject, cNoteText, cMsgType, cConversationID, dDate, cTime, lReceipt, lFromUser, aOrigin, aRecipients, aFiles )Create a new mail message with all optional parameters. Defaults: empty subject, empty body, today's date, current time, no receipt, prompt user.
Activate()Send the email via MAPI. Calls MAPISendMail() with all configured properties. Stores the return code in nRetCode.
End()Log off from the MAPI session. Calls MAPILogOff().

Example: Send Email with Attachments

#include "FiveWin.ch"

function Main()

   local oMail

   // Configure the email
   oMail := TMail():New( "Report for May 2026", ;
                         "Please find the attached report.",,,, .T. )

   // Add recipients: each entry is { cName [, cAddress ] }
   // Single string alone is not enough for all MAPI clients (e.g. Thunderbird).
   oMail:aRecipients := { { "user@example.com" }, ;
                          { "Manager", "manager@example.com" } }

   // Attach files
   oMail:aFiles := { "C:\reports\may2026.xlsx", "C:\reports\summary.pdf" }

   // Request read receipt
   oMail:lReceipt := .T.

   // Send without prompting the user
   oMail:lFromUser := .F.

   // Activate sends the email
   oMail:Activate()

   if oMail:nRetCode == 0
      MsgInfo( "Email sent successfully!" )
   else
      MsgStop( "Failed to send email. Error code: " + Str( oMail:nRetCode ) )
   endif

   oMail:End()

return nil

MAPI Return Codes

CodeDescription
0Success
1User aborted
2General failure
3Login failure
4Disk full
5Insufficient memory
6Access denied
14Unknown recipient
15Bad recipient type

DEFINE MAIL command

#include "FiveWin.ch"
#include "Mail.ch"

function Main()

   local oMail, cEmail := "user@example.com"

   DEFINE MAIL oMail ;
      SUBJECT "Report" ;
      TEXT "Please find the attached report." ;
      TO cEmail ;              // name only — OK (FWH fills address if empty)
      // TO cEmail, cEmail ;   // explicit name + address (always safe)
      FILES "C:\reports\may2026.xlsx", "may2026.xlsx" ;
      FROM USER

   ACTIVATE MAIL oMail

   MsgInfo( oMail:nRetCode )

return nil

TO <cName> [,<cAddress>] maps to a MapiRecipDesc pair. If only one parameter is given and the address would otherwise be empty, FWH fills it with the typed form SMTP:cName when the name looks like an e-mail (contains @), so clients such as Thunderbird fill the To: field and Outlook/Exchange deliver attachments correctly. Call SetMapiAddrFill( .F. ) before sending to disable the fill and leave lpszAddress empty for the client to resolve (pre-26.07 behaviour). Prefer TO cEmail, cEmail when you want the dual form explicit in source — the explicit address is never rewritten.

Notes

See Also