CMailMessage::FindText Method  
 
BOOL FindText(
  LPCTSTR lpszPattern,  
  LONG *lpnOffset,  
  LONG *lpnLength,  
  DWORD dwOptions  
);
BOOL FindText(
  LPCTSTR lpszPattern,  
  DWORD dwOptions  
);

The FindText method searches for matching text in the current message part.

Parameters

lpszPattern
A null-terminated string which specifies the text that should be searched for. This may be a literal string or it may specify a regular expression. The search options can be used to specify if the search is case-sensitive, and whether the search should filter HTML markup from the message. This parameter cannot be NULL or a string which only contains whitespace.
lpnOffset
A pointer to an integer which will contain the offset where the match was found. This value will be initialized as zero and then updated with the offset of the match when the method returns. If this information is not required, this parameter may be NULL.
lpnLength
A pointer to an integer which will contain the length of the matched text in characters. This value will be initialized as zero and then updated with the length of the match when the method returns. If this information is not required, this parameter may be NULL.
dwOptions
A value which specifies one or more options. This parameter is constructed by using a bitwise operator with any of the following values:
Value Description
MIME_SEARCH_DEFAULT The default matching option where matches are not case-sensitive and if the search pattern appears to be a regular expression, it will be compiled and the search will be performed using a simplified regular expression match. If the search pattern is not a regular expression, a simple substring search is performed.
MIME_SEARCH_CASE_SENSITIVE The pattern match will be case-sensitive. If this option is not specified, matches are not case-sensitive. If you are searching for domain names or email addresses in the message, it is recommended that you do not use this option.
MIME_SEARCH_LITERAL_MATCH Perform a literal match of the search pattern, even if the pattern appears to be a regular expression. This option can be useful if you want to search for content that may be enclosed in brackets or parenthesis, or uses characters which may be interpreted as wildcards (such as "." and "?") without needing to escape them.
MIME_SEARCH_FILTER_HTML If the current message part contains HTML markup, this option will filter out the markup prior to performing the search. This can eliminate potential cases of false positive matches where your pattern may match some part of the HTML markup instead of the visible content in the message. See the remarks below for discussion about the implications of using this option, particularly when it concerns the lpnOffset and lpnLength parameter values. These values would refer to the filtered text, not the original message content.

Return Value

If the method succeeds, the return value is non-zero. The method will return zero if the specified pattern was not found in the message, or if the current message part does not contain any searchable text (for example, if it contains a file attachment). To get extended error information, call the GetLastError method.

Remarks

The FindText method is used to search the current message part for text which matches a specific pattern. This method can only be used to search for text content in the current message part. It cannot be used to search encoded data for file attachments or other parts of a multipart message. It is intended to only be used to search parts of the message which would potentially be displayed to a user. The pattern you are matching against must be a null-terminated string and it cannot consist of only whitespace characters (spaces, tabs, carriage returns and linefeeds). This is because a search for whitespace in an email message is not useful in any practical sense, it would always match.

If the lpszPattern parameter does not contain any regular expression constructs (such as ^, $, [ ], or quantifiers like * and +), the search is performed using a simple substring comparison. If a regular expression pattern is specified, the method compiles the pattern and searches the message content for a match. The regular expression matching uses a simplified version which only supports anchors, wildcards, match classes (enclosed in brackets) and a limited number of escape sequences such as \d to match any digit or \s to match any space character. It does not support grouping or replacement.

Supported regular expression constructs include:

  • The caret ^ and dollar symbol $ for the beginning and end of the text. This would rarely be used in the context of email messages.
  • The period . to match any character.
  • Character classes such as [abc] which would match any of the characters enclosed in brackets.
  • Negated classes such as [^abc] which would match if none of those characters are found.
  • Escaped matches such as \d for digits, \w for word characters and \s for whitespace characters
  • The + quantifier can be used to match one or more occurrences.
  • The * quantifier will match zero or more occurrences.

This method uses a minimal regular expression parser and does not include support for complex constructs such as grouping matches. If you want to search for text which may contain characters typically used in regular expressions such as [ or $, you can either escape them using a backslash, or you can use the MIME_SEARCH_LITERAL_MATCH option which forces the method to only perform a substring search.

Applications searching for arbitrary user-entered text should usually specify the MIME_SEARCH_LITERAL_MATCH option to avoid unintended interpretation of regular expression characters. This will avoid situations where a user may unexpectedly enter search text which appears to be a regular expression pattern.

If you provide a regular expression as the pattern to match, it is also important to keep in mind that matches can span multiple lines (they do not end at line breaks) and the regular expression algorithm will attempt to match as much text as possible (this is sometimes referred to as a "greedy match"). This can yield unexpected results if you use wildcards in the pattern which are overly broad, matching large sections of the message text. For example, a pattern like ".*abc" will match everything until the last instance of "abc" is found in the text.

Important: The optional lpnOffset and lpnLength parameters can be used to provide more information about the location and length of the pattern match in the message. However, there are several important considerations with regards to these values. The pattern search is performed after the message contents have been decoded and/or converted from its original character set to UTF-8. Because all message text is normalized as UTF-8 internally, regardless of the original character set used to compose the message, this means you should not attempt to search for text using non-ASCII characters in your current locale. If the text was encoded, for example using quoted-printable or base64 encoding, the offset and length values will not be meaningful with regards to the original message content. The GetText method will return a copy of the decoded text.

If you specify the MIME_SEARCH_FILTER_HTML option, this will cause the method to filter HTML markup from the message text prior to matching the pattern against the contents. This is functionally similar to calling the FilterText method with its default options and then searching the plain text it returns. In this case, the offset and length values are only relevant to the filtered text, not the original text in the message. If you want to manipulate the filtered text after performing a search, for consistency you should always use FilterText to obtain the filtered text rather than GetText.

If you want to return a list of links in the current message, a simpler approach is to use the GetLinks method which designed specifically for this purpose.

Example

// Search a message to determine if any of the message text mentions
// a specific email address
LPCTSTR lpszAddress = _T("jsmith@example.com");

if (mailMessage.Open(pszMessageFile))
{
    // Get the number of parts to this message
    INT nPartCount = mailMessage.GetPartCount(hMessage);
    BOOL bFoundAddress = FALSE;

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

        // If this part of the message contains text, then we should search
        // for the email address using a literal (substring) match; this
        // includes both text/plain and text/html content
        if (MimeGetContentType(hMessage) == MIME_CONTENT_TEXT)
        {
            bFoundAddress = mailMessage.FindText(lpszAddress, MIME_SEARCH_LITERAL_MATCH);

            if (bFoundAddress)
                break;
        }
    }

    if (bFoundAddress)
    {
        _tprintf(_T("Found the email address %s in this message\n"), lpszAddress);
    }

    mailMessage.Close();
}

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