FFBase64Open - FFBase64Get512Bytes - FFBase64Encode - FFBase64Close
base64 encoding routines

Prototype

Put the following prototype in the prototypes section of the application program header file:

bool ( __stdcall *pfnFFBase64Open )( char* );
int  ( __stdcall *pfnFFBase64Get512Bytes )( char* );
int  ( __stdcall *pfnFFBase64Encode )( char* );
void ( __stdcall *pfnFFBase64Close )( void );

Purpose

Base 64 encoding for binary mail attachments.

Description and use

FFBase64Open, FFBase64Get512Bytes and FFBase64Close are used to encode binary attachments on the fly when writing to the socket connected to the SMTP (or ESMTP) server. The application program should do the following:
  • prepare the attachment
  • open the attachment (FFBase64Open)
  • enter a loop to get 512 encoded bytes at a time (FFBase64Get512Bytes) and print the bytes directly to the socket
  • close the attachment
Very simple. See the given example to see it all.

FFBase64Encode is a general purpose encoder, but I have developed it to encode username and password when the mail client is connecting to an ESMTP server which requires the AUTH LOGIN and, you know, you must encode those two strings. Again: see the example.

Specification

Please refer to any base-64 encoding specification on the Internet.

Parameters
  • FFBase64Open:
    • complete path and filename of the attachment file
  • FFBase64Get512Bytes:
    • pointer to the 512 bytes buffer: this function automatically reads 384 bytes from the opened file and fills with 512 bytes of encoded data the buffer pointed to by this parameter; this function adds a NULL character (0x00) at the end of the 512 encoded bytes, making the buffer an ASCIIZ string; at the end of the file this function reads 384 bytes or less (and the buffer is filled accordingly to the number of read bytes; the NULL character is put just after the last encoded byte); in the given example the buffer is declared some bytes more (char szMessage [ 516 ];) because it is necessary to add a carriage return & line feed pair
  • FFBase64Encode:
    • pointer to the string to encode: the string should be max 384 characters; but remember to allocate 4/3 space more, because the encoded result is given in the same place pointed to by this parameter and, as you know, base-64 encoding expands the source stream by a factor of 4/3
  • FFBase64Close:
    • (no parameters)

Return value

  • FFBase64Open:
    • true: the file has been opened
    • false: the file has not been opened
  • FFBase64Get512Bytes:
    • true: the buffer contains encoded data to print to the socket
    • false: the buffer is empty (the input file is finished), stop printing to the socket
  • FFBase64Encode:
    • the length of the encoded data
  • FFBase64Close:
    • (no return value)

Application example
  • complete example: the example illustrates the whole conversation between the mail client and the mail server; during the conversation a binary attachment is sent, and it is base-64 encoded on the fly; the example is taken from one of my Borland C++ Builder 3 Pro projects (even if later versions of the C++ Builder RAD have been released, I still continue using my BCB 3 Pro !!! and, by the way: I also still continue developing in Windows 95: I leave to my customers the pleasure of buying more recent version of Windows (after all I never had troubles in running my BCB3/Win95 apps under Win98, Win2000 and WinXP)

See also

Notes