Download Source Code

CMime Class Reference

Encapsulates the MIME format. More...

#include <Mime.h>

Collaboration diagram for CMime:

Collaboration graph
[legend]
List of all members.

Public Member Functions

BOOL ReplaceCid (LPMIMEBLOCK pmb, LPCTSTR pPath=NULL)
 Replaces the cid tags in the mime block with the specified path.
BOOL ReplaceCid (LPCTSTR pCid, LPCTSTR pReplace, LPCTSTR buf, DWORD size, LPSTR dest)
 Replaces the cid tags in the mime block with the specified path.
BOOL SaveExtras (LPCTSTR pDir)
 Saves attached data not marked as an attachment to specified folder.
BOOL DecodeBlock (LPMIMEBLOCK pmb)
 Decodes the data in a MIME block.
DWORD ahtoud (LPCTSTR pBuffer, BYTE ucBytes)
 Converts an ASCII hex string to binary.
BOOL DecodeQP (LPCTSTR pSrc, DWORD dwSrc, LPSTR pDst, LPDWORD pdwDst)
 Decodes 'Quoted Printable' encoding.
BOOL SaveAttachments (LPCTSTR pDir)
 Saves attachments to specified folder.
BOOL SaveBase64 (LPMIMEBLOCK pmb, LPCTSTR pFile)
 Decodes Base64 encoded data and saves it to a file.
BOOL SaveToFile (LPMIMEBLOCK pmb, LPCTSTR pFile)
 Saves the specified MIME block to a file, decodes as needed.
BOOL ReadBlock (LPBYTE buf, DWORD size, LPDWORD bsize)
 Reads MIME data from buffer into list.
BOOL ReadHeader (LPBYTE buf, DWORD size, LPDWORD hsize)
 Reads MIME header data.
BOOL LoadFromMemory (LPBYTE buf, DWORD size)
 Loads a MIME file from a memory buffer.
BOOL Load (LPCTSTR pFile)
 Loads a MIME file from disk file.
BOOL RemoveBlock (LPMIMEBLOCK node)
 Removes the specified MIME block structure from the list.
LPMIMEBLOCK AddBlock (LPMIMEBLOCK node)
 Adds a MIME block to the list.
void Destroy ()
 Releases all MIME blocks and other resources.
 CMime ()
 Default constructor.
virtual ~CMime ()
 Destructor.
DWORD GetNumBlocks ()
 Gets the total number of MIME blocks.
DWORD Size ()
 Returns the size of the internal MIME block list.
DWORD GetNumAttachments ()
 Returns the total number of attachements.
LPMIMEBLOCK GetNext (LPMIMEBLOCK ptr)
 Returns a pointer to the next MIME block.
LPMIMEBLOCK GetHeader ()
 Returns a pointer to the MIME header information block.
DWORD GetNumExtras ()
 Returns the number of attachements, not marked as attachments.
void SetPriority (DWORD p)
 Sets the priority of the MIME message.
DWORD GetPriority ()
 Returns the priority level of the MIME message.
void ZeroMimeBlock (LPMIMEBLOCK pMb)
 Initializes a MIME block data structure.
BOOL AddToHeader (LPCTSTR pParam, LPCTSTR pValue)
 Sets a value in the MIME header.
BOOL Save (LPCTSTR pFile)
 Saves a MIME file to disk.
LPMIMEBLOCK AddPlainText (LPCTSTR pText, DWORD dwSize=0)
 Adds a plain text MIME block.
LPMIMEBLOCK AddFile (LPCTSTR pFile, LPBYTE buf, DWORD size, DWORD flags=MBF1_ATTACHMENT)
 Adds file attachment MIME block.
LPMIMEBLOCK AddFile (LPCTSTR pFile, LPCTSTR pName=NULL, DWORD flags=MBF1_ATTACHMENT)
 Adds file attachment MIME block.
LPMIMEBLOCK Create (LPCTSTR pTo=NULL, LPCTSTR pFrom=NULL, LPCTSTR pSubject=NULL)
 Creates an E-mail style MIME file.
DWORD SaveBlock (LPMIMEBLOCK pmb, LPBYTE buf, DWORD size)
 Saves a MIME block to a memory buffer.
DWORD SaveToMem (LPBYTE buf, DWORD size)
 Saves a MIME file to a memory buffer.

Static Public Member Functions

static BOOL GetContentExtension (LPCTSTR pMime, LPSTR pExt)
 Returns the content extension for MIME string.
static BOOL GetContentType (LPCTSTR pFile, LPSTR pType)
 Returns the MIME content type for a filename.
static BOOL IsValidEmailCharacter (char ch)
 Returns non-zero if valid e-mail character.
static BOOL GetEmailComponents (LPCTSTR pEmail, LPSTR pName, LPSTR pUser, LPSTR pDomain, LPDWORD pdwNext=NULL)
 Breaks e-mail strings into base components.
static BOOL BuildEmail (LPSTR pEmail, LPCTSTR pName, LPCTSTR pUser, LPCTSTR pDomain, LPCTSTR pQuote="\"", LPCTSTR pStart="<", LPCTSTR pEnd=">")
 Builds an e-mail address string from its components.
static BOOL VerifyEmailList (LPSTR pDst, LPCTSTR pSrc, LPCTSTR pSep="; ")
 Builds an e-mail address string from its components.
static DWORD GetNextEmail (LPSTR pName, LPSTR pAddress, LPCTSTR pList, DWORD dwOffset=0, LPCTSTR pStart="<", LPCTSTR pEnd=">")
 Retrieves information about the next e-mail in a list.
static BOOL GenBoundry (LPSTR pBoundry)
 Creates a string suitable to be used as a MIME block boundry.

Detailed Description

Encapsulates the MIME format.

Reads / Writes files and memory buffers with the MIME data format. Allows simple creation of E-mail and News group messages. Supports multiple data types and attachements.

This example creates a simple e-mail message with an attachment.

    // Create object
    CMime mime;

    // Create the message
    mime.Create( "to@nwhere.com", "from@nowhere.com", "Subject Line" );

    // Attach a text file from a memory buffer
    char *pFileText = "This is an empty file.";
    mime.AddFile( "empty_file.txt", (LPBYTE)pFileText, strlen( pFileText ) );

    char *pMsg = "This is the message body";
    mime.AddPlainText( pMsg, strlen( pMsg ) );

    // Save e-mail message
    mime.Save( "Test.eml" );

Here's an example of how to extract the HTML portion from a MIME document and link any embedded cid parts.

static BOOL GetTextBlocks(CMime *pMime, LPMIMEBLOCK *plaintext, LPMIMEBLOCK *htmltext, LPMIMEBLOCK *multipart)
{
    LPMIMEBLOCK pmb = NULL;

    *plaintext = NULL;
    *htmltext = NULL;
    *multipart = NULL;

    // Find text blocks
    while ( ( NULL == *htmltext || NULL == *plaintext ) &&
            NULL != ( pmb = pMime->GetNext( pmb ) ) )
    {
        if (    ( pmb->f1 & MBF1_ATTACHMENT ) == 0 &&
                !strnicmp( pmb->ctype, "multipart/alternative", strlen( "multipart/alternative" ) ) )
            *multipart = pmb;

        else if (   ( pmb->f1 & MBF1_ATTACHMENT ) == 0 &&
                    pmb->dsize > 2 && *plaintext == NULL &&
                    !strnicmp( pmb->ctype, "text/plain", strlen( "text/plain" ) ) )
                    *plaintext = pmb;

        else if (   ( pmb->f1 & MBF1_ATTACHMENT ) == 0 &&
                    pmb->dsize > 2 && *htmltext == NULL &&
                    !strnicmp( pmb->ctype, "text/html", strlen( "text/html" ) ) )
            *htmltext = pmb;

        else if (   ( pmb->f1 & MBF1_ATTACHMENT ) == 0 && pmb->ctype[ 0 ] == 0 &&
                    pmb->dsize > 2 && *plaintext == NULL ) 
            *plaintext = pmb;
    } // end while

    return TRUE;
}

static BOOL GetTextBlocks(CMime *pMime, CMime *pMulti, LPMIMEBLOCK *plaintext, LPMIMEBLOCK *htmltext)
{
    LPMIMEBLOCK multipart = NULL;

    // Get text blocks
    GetTextBlocks( pMime, plaintext, htmltext, &multipart );

    // Get blocks from sub block(s)
    BOOL quit = FALSE;
    LPMIMEBLOCK oldmulti = NULL;
    while ( !quit &&
            ( *plaintext == NULL || *htmltext == NULL ) && 
            multipart != NULL && multipart != oldmulti )
    {
        // One shot
        oldmulti = multipart;

        if ( pMulti->LoadFromMemory( (LPBYTE)multipart->pdata, multipart->dsize ) )
        {
            LPMIMEBLOCK     pt = NULL, ht = NULL;
            GetTextBlocks( pMulti, &pt, &ht, &multipart );

            // Set pointers
            if ( pt != NULL && *plaintext == NULL ) quit = TRUE, *plaintext = pt;

            if ( ht != NULL && *htmltext == NULL ) quit = TRUE, *htmltext = ht;

        } // end if

    } // end while

    return TRUE;
}

    CMime mime;

    // File containing cid parts
    mime.Load( "Test_001.eml" );

    CMime       multi;
    LPMIMEBLOCK plaintext = NULL;
    LPMIMEBLOCK htmltext = NULL;

    // Get text components
    GetTextBlocks( &mime, &multi, &plaintext, &htmltext );
    
    // Extract attachments
    ::CreateDirectory( "./attachments", NULL );
    mime.SaveAttachments( "./attachments" );

    // Extract extra files
    ::CreateDirectory( "./extras", NULL );
    mime.SaveExtras( "./extras" );

    // Replace the cid's with actual file links
    mime.ReplaceCid( htmltext, "./extras" );

    // We need to process any multi-part as well
    // Is there a multipart?
    if ( multi.Size() )
    {   multi.SaveAttachments( "./attachments" );
        multi.SaveExtras( "./extras" );
        multi.ReplaceCid( htmltext, "./extras" );
    } // end if
  
    // Extract the html document if any
    if ( htmltext )
    {
        CWinFile f;
        if ( f.OpenNew( "Test_001.htm" ) )
            f.Write( htmltext->pdata , htmltext->dsize );

    } // end if


Member Function Documentation

LPMIMEBLOCK CMime::AddBlock LPMIMEBLOCK  node  ) 
 

Adds a MIME block to the list.

Parameters:
[in] node - Pointer to a MIME block
Returns:
MIME block pointer in node
See also:

LPMIMEBLOCK CMime::AddFile LPCTSTR  pFile,
LPCTSTR  pName = NULL,
DWORD  flags = MBF1_ATTACHMENT
 

Adds file attachment MIME block.

Parameters:
[in] pFile - File containing data
[in] pName - Filename to give MIME block
[in] flags - MIME block flags
Returns:
Pointer to new MIME block data structure
See also:

LPMIMEBLOCK CMime::AddFile LPCTSTR  pFile,
LPBYTE  buf,
DWORD  size,
DWORD  flags = MBF1_ATTACHMENT
 

Adds file attachment MIME block.

Parameters:
[in] pFile - Filename
[in] buf - Buffer containing data
[in] size - Size of data in buf
[in] flags - MIME block flags
Returns:
Pointer to new MIME block data structure
See also:

LPMIMEBLOCK CMime::AddPlainText LPCTSTR  pText,
DWORD  dwSize = 0
 

Adds a plain text MIME block.

Parameters:
[in] pText - Plain text
[in] dwSize - Number of bytes in pText
Returns:
Pointer to new MIME block data structure
See also:

BOOL CMime::AddToHeader LPCTSTR  pParam,
LPCTSTR  pValue
 

Sets a value in the MIME header.

Parameters:
[in] pParam - Parameter name to set
[in] pValue - Parameter value
Returns:
Non-zero if success
See also:

DWORD CMime::ahtoud LPCTSTR  pBuffer,
BYTE  ucBytes
 

Converts an ASCII hex string to binary.

Parameters:
[in] pBuffer - Buffer containing ASCII hex string
[in] ucBytes - Number of bytes in pBuffer to convert
Returns:
Number value
See also:

BOOL CMime::BuildEmail LPSTR  pEmail,
LPCTSTR  pName,
LPCTSTR  pUser,
LPCTSTR  pDomain,
LPCTSTR  pQuote = "\"",
LPCTSTR  pStart = "<",
LPCTSTR  pEnd = ">"
[static]
 

Builds an e-mail address string from its components.

Parameters:
[out] pEmail - Receives e-mail string
[in] pName - Name
[in] pUser - Username
[in] pDomain - Domain
[in] pQuote - Quote character
[in] pStart - E-mail address quote start
[in] pEnd - E-mail address end quote
Returns:
Non-zero if success
See also:

LPMIMEBLOCK CMime::Create LPCTSTR  pTo = NULL,
LPCTSTR  pFrom = NULL,
LPCTSTR  pSubject = NULL
 

Creates an E-mail style MIME file.

Parameters:
[in] pTo - Destination e-mail list
[in] pFrom - Source e-mail list
[in] pSubject - Subject line
Returns:
Pointer to MIME block or NULL if failure
See also:

BOOL CMime::DecodeBlock LPMIMEBLOCK  pmb  ) 
 

Decodes the data in a MIME block.

Parameters:
[in] pmb - MIME block information
Returns:
Non-zero if success
See also:

BOOL CMime::DecodeQP LPCTSTR  pSrc,
DWORD  dwSrc,
LPSTR  pDst,
LPDWORD  pdwDst
 

Decodes 'Quoted Printable' encoding.

Parameters:
[in] pSrc - 'Quoted Printable' encode string
[in] dwSrc - Number of bytes in pSrc
[in] pDst - Receives the decoded data
[in,out] pdwDst - Receives the number of bytes decoded.
Returns:
Non-zero if success
See also:

BOOL CMime::GenBoundry LPSTR  pBoundry  )  [static]
 

Creates a string suitable to be used as a MIME block boundry.

Parameters:
[out] pBoundry - Receives the boundry string
Returns:
Non-zero if success
See also:

BOOL CMime::GetContentExtension LPCTSTR  pMime,
LPSTR  pExt
[static]
 

Returns the content extension for MIME string.

Parameters:
[in] pMime - MIME string
[out] pExt - Receives content extension
If called with pMime == "image/jpeg", then "jpg" is returned in pExt.

Returns:
Non-zero if success
See also:

BOOL CMime::GetContentType LPCTSTR  pFile,
LPSTR  pType
[static]
 

Returns the MIME content type for a filename.

Parameters:
[in] pFile - Filename
[out] pType - MIME type
Returns:
Non-zero if success
See also:

BOOL CMime::GetEmailComponents LPCTSTR  pEmail,
LPSTR  pName,
LPSTR  pUser,
LPSTR  pDomain,
LPDWORD  pdwNext = NULL
[static]
 

Breaks e-mail strings into base components.

Parameters:
[in] pEmail - E-Mail string or E-mail list string
[out] pName - Name
[out] pUser - Username
[out] pDomain - Domain
[out] pdwNext - Offset to next e-mail in list
pEmail can contain a single e-mail address string or a list of strings separated by ';' character.

Examples:

            "Bob Smith<bsmith@wheresjames.com>"
            "\"Jane Smith\"<jsmith@wheresjames.com>;\"Tim Jones\"<tjones@wheresjames.com>"
Non-zero if success

Returns:
See also:

LPMIMEBLOCK CMime::GetNext LPMIMEBLOCK  ptr  )  [inline]
 

Returns a pointer to the next MIME block.

Parameters:
[in] ptr - Pointer to MIME block structure
If ptr is NULL, returns the head pointer

Returns:
See also:

DWORD CMime::GetNextEmail LPSTR  pName,
LPSTR  pAddress,
LPCTSTR  pList,
DWORD  dwOffset = 0,
LPCTSTR  pStart = "<",
LPCTSTR  pEnd = ">"
[static]
 

Retrieves information about the next e-mail in a list.

Parameters:
[out] pName - Name
[out] pAddress - E-mail address
[in] pList - Pointer to e-mail list
[in] dwOffset - Starting offset into pList
[in] pStart - E-mail start quote string
[in] pEnd - E-mail end quote string
Returns:
Offset of next e-mail
See also:

BOOL CMime::IsValidEmailCharacter char  ch  )  [static]
 

Returns non-zero if valid e-mail character.

Parameters:
[in] ch - Character to test
Returns:
Non-zero if ch is a valid e-mail character
See also:

BOOL CMime::Load LPCTSTR  pFile  ) 
 

Loads a MIME file from disk file.

Parameters:
[in] pFile - Filename
Returns:
Non-zero if success
See also:

BOOL CMime::LoadFromMemory LPBYTE  buf,
DWORD  size
 

Loads a MIME file from a memory buffer.

Parameters:
[in] buf - Buffer pointer
[in] size - Size of buffer in buf
Returns:
Non-zero if success
See also:

BOOL CMime::ReadBlock LPBYTE  buf,
DWORD  size,
LPDWORD  bsize
 

Reads MIME data from buffer into list.

Parameters:
[in] buf - Buffer containing MIME data
[in] size - Number of bytes in buf
[out] bsize - Number of bytes read
Returns:
Non-zero if success
See also:

BOOL CMime::ReadHeader LPBYTE  buf,
DWORD  size,
LPDWORD  hsize
 

Reads MIME header data.

Parameters:
[in] buf - Buffer containing MIME header data
[in] size - Number of bytes in buf
[in] hsize - Number of bytes read
Returns:
Non-zero if success
See also:

BOOL CMime::RemoveBlock LPMIMEBLOCK  node  ) 
 

Removes the specified MIME block structure from the list.

Parameters:
[in] node - Pointer to MIME block structure
Returns:
Non-zero if success
See also:

BOOL CMime::ReplaceCid LPCTSTR  pCid,
LPCTSTR  pReplace,
LPCTSTR  buf,
DWORD  size,
LPSTR  dest
 

Replaces the cid tags in the mime block with the specified path.

Parameters:
[in] pCid - CID string
[in] pReplace - Replacement string
[in] buf - Buffer containing replacement text
[in] size - Number of bytes in buf
[in] dest - Receives the processed data
Returns:
Non-zero if success
See also:

BOOL CMime::ReplaceCid LPMIMEBLOCK  pmb,
LPCTSTR  pPath = NULL
 

Replaces the cid tags in the mime block with the specified path.

Parameters:
[in] pmb - Pointer to MIME block
[in] pPath - Replacement data path
Returns:
Non-zero if success
See also:

BOOL CMime::Save LPCTSTR  pFile  ) 
 

Saves a MIME file to disk.

Parameters:
[in] pFile - Filename
Returns:
Non-zero if success
See also:

BOOL CMime::SaveAttachments LPCTSTR  pDir  ) 
 

Saves attachments to specified folder.

Parameters:
[in] pDir - Folder
Returns:
Non-zero if success
See also:

BOOL CMime::SaveBase64 LPMIMEBLOCK  pmb,
LPCTSTR  pFile
 

Decodes Base64 encoded data and saves it to a file.

Parameters:
[in] pmb - Pointer to MIME block structure
[in] pFile - Destination filename
Returns:
Non-zero if success
See also:

DWORD CMime::SaveBlock LPMIMEBLOCK  pmb,
LPBYTE  buf,
DWORD  size
 

Saves a MIME block to a memory buffer.

Parameters:
[in] pmb - Pointer to MIME block structure
[out] buf - Receives the MIME block data
[in] size - Size of buffer in buf
Call with buf set to NULL to return required buffer size.

Returns:
Number of bytes written to buf
See also:

BOOL CMime::SaveExtras LPCTSTR  pDir  ) 
 

Saves attached data not marked as an attachment to specified folder.

Parameters:
[in] pDir - Output folder
Returns:
Non-zero if success
See also:

BOOL CMime::SaveToFile LPMIMEBLOCK  pmb,
LPCTSTR  pFile
 

Saves the specified MIME block to a file, decodes as needed.

Parameters:
[in] pmb - Pointer to MIME block structure
[in] pFile - Destination filename
Returns:
Non-zero if success
See also:

DWORD CMime::SaveToMem LPBYTE  buf,
DWORD  size
 

Saves a MIME file to a memory buffer.

Parameters:
[out] buf - Receives MIME file data
[in] size - Size of buffer in buf
Call with buf set to NULL to return required buffer size.

Returns:
Number of bytes written to buf
See also:

void CMime::SetPriority DWORD  p  )  [inline]
 

Sets the priority of the MIME message.

Parameters:
[in] p - Priority level

BOOL CMime::VerifyEmailList LPSTR  pDst,
LPCTSTR  pSrc,
LPCTSTR  pSep = "; "
[static]
 

Builds an e-mail address string from its components.

Parameters:
[out] pDst - Receives verified e-mail list string
[in] pSrc - Source e-mail list string
[in] pSep - E-mail address separator
Returns:
Non-zero if success
See also:

void CMime::ZeroMimeBlock LPMIMEBLOCK  pMb  ) 
 

Initializes a MIME block data structure.

Parameters:
[in] pMb - MIME block structure to initialize


The documentation for this class was generated from the following files:
  • inc/Mime.h
  • Mime.cpp
Copyright Robert Umbehant
This documentation is covered by the LGPL