MimeGetMessageText Function  
 
LONG WINAPI MimeGetMessageText(
  HMESSAGE hMessage,  
  LONG nOffset,  
  LPTSTR lpszMessage,  
  LONG nMaxLength  
);

The MimeGetMessageText function copies the text of the current message part to the specified buffer.

Parameters

hMessage
Handle to the message.
nOffset
The byte offset from the beginning of the message. A value of zero specifies the first character in the body of the message part.
lpszMessage
The string buffer that will contain a copy of the message text when the function returns. Note that the buffer will typically contain multiple lines of text. If this parameter is NULL the function will return the number of characters that would be copied.
nMaxLength
The maximum number of bytes to copy into the buffer. The size of the buffer provided must be larger than the content length for the current message part. If this parameter is zero, the text will not be copied.

Return Value

If the function succeeds, the return value is number of bytes copied into the buffer. If the function fails, the return value is MIME_ERROR. To get extended error information, call MimeGetLastError.

Remarks

If your project targets a multi-byte character set, this function will always return the message contents as UTF-8 text, regardless of the original character set specified in the message itself. This ensures that characters in the original text are preserved, regardless of the default ANSI code page on the local system. It is recommended you build your project to use Unicode whenever possible. If your application must use ANSI, you can use the MimeLocalizeText method to convert the Unicode text to a specific character set.

You should not determine the maximum size of the output buffer using the MimeGetContentLength function. That function returns the content size in bytes as it is stored in the message, and does not account for any character encoding or Unicode conversion which may be required. The content length can be used to estimate the amount of text stored in the message part, but you should always allocate a buffer which is larger than the length specified in the message.

If the nMaxLength parameter does not specify a buffer size large enough to store the contents of the current message part, this function will fail and the last error code will be set to ST_ERROR_BUFFER_TOO_SMALL. Your application must ensure the buffer is large enough to contain the complete message text and a terminating null character. If you wish to determine the actual length of the decoded message text, you can specify the lpszMessage parameter as NULL and nMaxLength as zero. This will cause the function to return the number of characters in the current message part, not including the terminating null. When allocating a buffer on the heap, make sure you include additional space for the terminating null character. See the example below.

If the current message part contains HTML markup and you want to display it as plain text to the user, you can use the MimeFilterMessageText function. It can remove markup from the message text, as well as remove potentially dangerous hyperlinks.

Example

if (MimeGetContentType(hMessage) == MIME_CONTENT_TEXT)
{
    // Get the size of the decoded/converted message text
    LONG cchMessage = MimeGetMessageText(hMessage, 0, 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 +1 for the null character
    LPTSTR pszMessage = (LPTSTR)malloc((cchMessage + 1) * sizeof(TCHAR));
    if (pszMessage == NULL)
        return;

    // Copy the message contents to the buffer
    cchMessage = MimeGetMessageText(hMessage, 0, 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