CMailMessage::GetContentType Method  
 
INT GetContentType(
  INT nMessagePart,  
  LPTSTR lpszSubType,  
  INT cchSubType,  
  LPTSTR lpszParams,  
  INT cchParams  
);
INT GetContentType(
  INT nMessagePart,  
  LPTSTR lpszSubType,  
  INT cchSubType,  
);
INT GetContentType();

Parameters

nMessagePart
An integer value which specifies the message part. Message parts are zero-based with the part zero being the primary content and additional parts typically containing message content and file attachments. A value of MIME_CURRENT_PART (-1) specifies the content type for the current message part should be returned.
lpszSubType
A null-terminated string buffer that will receive the content subtype, such as "html" or "plain". This parameter may be NULL if the subtype is not required.
cchSubType
The maximum number of characters that can be copied into the lpszSubType buffer, including the terminating null character. If the buffer is too small, the method will return an error.
lpszParams
A null-terminated string buffer that will receive any parameters associated with the content type, such as charset=utf-8. This parameter may be NULL if parameters are not needed.
cchParams
The maximum number of characters that can be copied into the lpszParams buffer, including the terminating null character. If the buffer is too small, the method will return an error.

Return Value

The method returns an integer value that identifies the top-level media type for the current message part. This value may be one of the following predefined constants:

Value Description
MIME_CONTENT_TEXT Textual content (e.g.: text/plain, text/html)
MIME_CONTENT_IMAGE Image content (e.g.: image/jpeg, image/png)
MIME_CONTENT_AUDIO Audio content (e.g.: audio/mpeg)
MIME_CONTENT_VIDEO Video content (e.g.: video/mp4)
MIME_CONTENT_APPLICATION Application-specific data (e.g.: application/pdf)
MIME_CONTENT_MESSAGE Embedded message (e.g.: message/rfc822)
MIME_CONTENT_MULTIPART Multipart content (e.g.: multipart/alternative)
MIME_CONTENT_WIDETEXT Extended text using wide character encoding
MIME_CONTENT_UNKNOWN The content type is unknown or could not be determined

If the method fails, it returns MIME_ERROR. To retrieve extended error information, call GetLastError.

Remarks

The GetContentType method parses the Content-Type header of the current message part and returns the content type information in a normalized form. If requested, the subtype name and any optional parameters are copied to the caller-supplied buffers. This method eliminates the need for manual parsing of MIME headers and is useful for determining how message parts should be processed based on their media type. For example, consider a message part with:

Content-Type: text/html; charset=utf-8

This method will return MIME_CONTENT_TEXT, and optionally a subtype of "html", and the parameter string "charset=utf-8". If only the numeric media type is required for use in a switch or if-then-else statement, the content type and parameters buffers may be omitted or passed as NULL.

If you wish to determine if the content type for a message part matches a specific value, you can use the IsContentType method.

Example

TCHAR szSubType[32] = {0};
TCHAR szParams[64] = {0};

// Check if the current message part is plain text; szParams may
// contain the original character set used
INT nContentType = mailMessage.GetContentType(
                        MIME_CURRENT_PART,
                        szSubType, _countof(szSubType),
                        szParams, _countof(szParams));

if (nContentType == MIME_CONTENT_TEXT && lstrcmpiA(szSubType, "plain") == 0)
{
    // Get the size of the decoded/converted message text
    LONG cchMessage = mailMessage.GetText(NULL, 0);
    if (cchMessage < 1)
    {
        _tprintf(_T("Message part %d does not contain any text\n"), nMessagePart);
        return;
    }

    // Allocate a buffer for the message text on the heap
    LPTSTR pszMessage = (LPTSTR)malloc((cchMessage + 1) * sizeof(TCHAR));
    if (pszMessage == NULL) // Virtual memory exhausted
        return;

    // Copy the message contents to the buffer
    cchMessage = mailMessage.GetText(pszMessage, cchMessage + 1);
    if (cchMessage > 0)
    {
        // Write the contents of the message part to the console
        fwrite(pszMessage, sizeof(TCHAR), cchMessage, stdout);
        fflush(stdout);
    }

    // Free the memory allocated for our text buffer
    free(pszMessage);
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csmsgv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also