CMailMessage::GetFilteredText Method  
 
LONG GetFilteredText(
  LPTSTR lpszFiltered,  
  LONG nMaxLength,  
  DWORD dwOptions  
);

The GetFilteredText method returns the filtered contents of a HTML or plain text message.

Parameters

lpszFiltered
A null-terminated string buffer which will contain the filtered text when the method returns. This buffer must be large enough to contain all of the filtered content and a terminating null character. If this parameter is NULL, the filtered text will not be copied. This can be used to determine the amount of filtered text that would be returned. This can be used to determine how much memory should be allocated for the filtered text; however, remember to allocate additional space for the terminating null character.
nMaxLength
An integer which specifies the maximum number of characters which can be copied into the string buffer. If the maximum length is not large enough to store all of the filtered text and a terminating null character, the method will return an error. This value may be zero which indicates you want determine the length of the filtered text without copying any characters.
dwOptions
An unsigned integer which specifies one or more bit flags. which controls what content is filtered from the message. It may be one or more of the following values.
Value Description
MIME_FILTER_DEFAULT Perform default filtering which removes HTML markup and decodes HTML entities. No additional filtering of URLs or text is performed.
MIME_FILTER_REMOVE_QUERY Remove query parameters from hyperlinks such as ?id=123. Useful for stripping tracking information or session data.
MIME_FILTER_REMOVE_CREDENTIALS Remove embedded user credentials in URLs such as user:pass@example.com, preventing obfuscation or accidental disclosure.
MIME_FILTER_REMOVE_LINKS Remove all hyperlinks from the message content. This includes both anchor tags in HTML and plain text URLs.
MIME_FILTER_REMOVE_UNICODE Strip all non-ASCII (Unicode) characters from the message. Use this when only 7-bit clean output is required.
MIME_FILTER_REJECT_LOCAL Reject any link that refers to internal or local domains, including .local, .lan, and localhost.
MIME_FILTER_REJECT_IP Reject links that use raw IP addresses instead of hostnames, such as http://192.168.1.1.
MIME_FILTER_REJECT_PUNYCODE Reject hyperlinks using Punycode-encoded domain names, such as those with the xn-- prefix. This helps prevent spoofing using visually similar characters.
MIME_FILTER_REJECT_HTTP Reject any hyperlinks that use http:// instead of the secure https:// protocol.
MIME_FILTER_REJECT_UNSAFE Reject links using unsafe schemes such as file:, data:, or javascript:. These links are excluded by default unless overridden.
MIME_FILTER_ALLOW_PUNYCODE Explicitly permit Punycode domain names in URLs even when MIME_FILTER_REMOVE_UNICODE is specified. Use when legitimate IDN domains are expected.
MIME_FILTER_ALLOW_UNSAFE Allow potentially unsafe schemes such as file: or javascript: in message content. Use with caution and only when the message source is fully trusted.
MIME_FILTER_ALLOW_SYMBOLS Allow Unicode symbols such as emojis and pictographs in the filtered text.
MIME_FILTER_ALLOW_CONFUSABLES Allow Unicode characters which can be confused with normal ASCII characters. Use only when the message source is fully trusted.

Return Value

The method will return the number of characters of filtered text copied to the string buffer, not including the terminating null character. If the lpszFilteredText parameter is NULL, the method will return the length of the filtered text without copying any characters. A return value of zero indicates the message part does not contain any message text. If the method fails it will return MIME_ERROR. To get extended error information, call MimeGetLastError.

Remarks

The GetFilteredText method filters MIME-encoded message parts by removing HTML tags and other potentially unsafe or undesired content. It handles both HTML and plain text content types. For HTML, the method converts the markup to plain text and decodes HTML entities. For plain text, it scans for embedded URLs and applies filtering based on the specified options.

When filtering HTML markup from message content, this method:

  • Removes script, style, and other non-visible or unsafe elements.
  • Converts common tag structures, such as lists and headers, into readable plain text.
  • Extracts and optionally filters or removes hyperlinks and image alt text.
  • Decodes common HTML entities such as &trade and &copy, as well as numeric entities like &#x20.
  • Normalize Unicode characters to their canonical values (NKFD normalization).
  • Replace confusable homoglyphs with their ASCII counterparts.
  • Remove symbols such as emojis and pictographs which can confuse pattern matching.

The method is designed to produce clean, human-readable output suitable for previewing message contents in text-only environments, logging, or performing further analysis. It ensures that embedded credentials, tracking links, or insecure resources are removed or sanitized based on caller preferences.

By default, the method does not remove Unicode characters or strip links unless explicitly instructed via flags. The MIME_FILTER_STRICT constant is a recommended combination for maximum safety and removes potentially malicious or misleading elements from messages. Characters that are visually indistinguishable from standard Latin letters, such as the Cyrillic "a" (U+0430) and Latin "a" (U+0061), will be converted to their ASCII equivalent, preventing homoglyph attacks and attempts to obfuscate links or defeat pattern matching.

To obtain the original message content, use the GetText method.

Example

// Search a multipart message for the HTML version of the message
// and write the filtered content to standard output

if (!mailMessage.Open(pszFileName))
{
    _tprintf(_T("Unable to open message %s\n"), pszFileName);
    return;
}

// Get the number of parts in the message
INT nPartCount = mailMessage.GetPartCount();

for (INT nMessagePart = 0; nMessagePart < nPartCount; nMessagePart++)
{
    // Set the current message part
    mailMessage.SetPart(nMessagePart);

    if (mailMessage.IsContentType(_T("text/html")))
    {
        DWORD dwOptions = MIME_FILTER_STRICT;

        // Determine the amount of memory we need to allocate
        LPTSTR pszFiltered = NULL;
        LONG cchFiltered = mailMessage.GetFilteredText(NULL, 0, dwOptions);

        if (cchFiltered < 1)
        {
            // Unable to filter this message part even though its identified
            // as containing HTML or it does not contain any text
            break;
        }
        else
        {
            // Allocate memory for the filtered text; note the extra space is
            // included for the terminating null character
            pszFiltered = (LPTSTR)malloc((cchFiltered + 1) * sizeof(TCHAR));

            if (pszFiltered == NULL)
            {
                // Unable to allocate memory for the filtered text
                break;
            }

            // Return the filtered message contents in pszFiltered
            cchFiltered = mailMessage.GetFilteredText(
                                pszFiltered,
                                cchFiltered + 1,
                                dwOptions);

            if (cchFiltered > 0)
            {
                // Write the filtered text to standard output on the console
                fwrite(pszFiltered, sizeof(TCHAR), cchFiltered, stdout);
            }

            // Free the memory we allocated for the filtered text
            free(pszFiltered);
            break;
        }    
    }
}          

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