CMailMessage::GetText Method  
 
LONG GetText(
  LPTSTR lpszMessage,  
  LONG nMaxLength,  
  LONG nOffset  
);
LONG GetText(
  CString& strMessage,  
  LONG nOffset  
);

The GetText method copies the text of the current message part to the specified buffer.

Parameters

lpszMessage
A null-terminated string buffer that will receive a copy of the message text when the method returns.
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.
nOffset
An optional parameter which specifies the byte offset from the beginning of the message. A value of zero specifies the first character in the body of the message part.

Return Value

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

Remarks

If your project targets a multi-byte character set, this method 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 LocalizeText method to convert the Unicode text to a specific character set.

You should not determine the maximum size of the output buffer using the GetContentLength method. That method 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. It is recommended you use the GetTextLength method to determine how large the string buffer should be.

If the nMaxLength parameter does not specify a buffer size large enough to store the contents of the current message part, this method 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 method 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 GetFilteredText method. It can remove markup from the message text, as well as remove potentially dangerous hyperlinks.

Example

if (mailMessage.GetContentType(hMessage) == MIME_CONTENT_TEXT)
{
    // Get the size of the decoded/converted message text
    LONG cchMessage = mailMessage.GetTextLength();
    if (cchMessage < 1)
    {
        _tprintf(_T("Message part %d does not contain any text\n"), nMessagePart);
        return;
    }

    // Allocate a string buffer for the message text +1 for the null character
    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