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.
if (MimeGetContentType(hMessage) == MIME_CONTENT_TEXT)
{
LONG cchMessage = MimeGetMessageText(hMessage, 0, NULL, 0);
if (cchMessage < 1)
{
_tprintf(_T("Message part %d does not contain any text\n"), nMessagePart);
return;
}
LPTSTR pszMessage = (LPTSTR)malloc((cchMessage + 1) * sizeof(TCHAR));
if (pszMessage == NULL)
return;
cchMessage = MimeGetMessageText(hMessage, 0, pszMessage, cchMessage + 1);
if (cchMessage > 0)
{
fwrite(pszMessage, sizeof(TCHAR), cchMessage, stdout);
fflush(stdout);
}
free(pszMessage);
}