CMailMessage::IsContentType Method  
 
BOOL IsContentType(
  LPCTSTR lpszContentType  
);
BOOL IsContentType(
  INT nMessagePart,  
  LPCTSTR lpszContentType  
);

The MimeIsContentType method checks if the content type for the message matches the specified content type.

Parameters

hMessage
Handle to the message.
nMessagePart
An optional integer value which specifies which part of the message to compare against the content type. A value of zero returns compares the content type against the main message header, while a value greater than zero compares the content type against the content type for a specific part of a multipart message. If this parameter is omitted or a value of MIME_CURRENT_PART (-1) is specified, the content type will be compared against the current message part.
lpszContentType
Pointer to a string which specifies the content type to compare against. This parameter cannot be NULL and it must specify a valid MIME media type. It is permitted to omit the media subtype, in which case the content type match will only be made against the base type. For example, both "application/pdf" and "application" are permitted as valid content types. Note that this is a prefix match, not a wildcard pattern match.

Return Value

The method will return a non-zero value if the specified content type matches the content type for the message part. If the content types do not match or one of the parameters is invalid, the method will return zero. To get extended error information, call GetLastError. If the parameters are valid but the content types do not match, the last error code will be set to NO_ERROR.

Remarks

The IsContentType method is used to check the content type for a specific part of a MIME-encoded message against a known media type. Multipurpose Internet Mail Extensions (MIME) defines a standard way of classifying the content of an email message or its individual parts, such as plain text, HTML, images, or file attachments.

A MIME media type, also referred to as a content type, is typically expressed in the form type/subtype. For example, the content type text/plain identifies unformatted plain text, while text/html identifies HTML formatted text. The content type for each message part is typically specified in its Content-Type header field. This method performs a case-insensitive comparison between the message part's declared content type and the type provided by the caller. It does not consider optional parameters, such as character encoding or boundary markers. For example, a content type of "text/html; charset=UTF-8" will match "text/html".

The following table lists some of the most common MIME media types used in email messages:

Media Type Description
text/plain Plain text with no formatting
text/html HTML-formatted text, commonly used for rich email content
multipart/alternative Message that contains multiple representations of the same content, such as both plain text and HTML
multipart/mixed Message that includes a combination of text and attachments
multipart/related Message that includes related resources, such as inline images referenced by HTML content
image/jpeg JPEG image
image/png PNG image
application/pdf PDF document
application/octet-stream Generic binary data, typically used for file attachments

To obtain the full content type string for a message part, including any optional parameters, use the GetHeader method with the header name "Content-Type".

Example

// Search the message for a PDF attachment by checking the content type
// for each part of a multipart message.
//
// If the content type matches "application/pdf" then that message part
// should contain a PDF attachment. We prompt the user where to store
// the file and then exit the loop.

INT nParts = mailMessage.GetPartCount();

for (INT nMessagePart = 1; nMessagePart < nParts; nMessagePart++)
{
    if (mailMessage.SetPart(nMessagePart) == MIME_ERROR)
        break;

    if (mailMessage.IsContentType(_T("application/pdf")))
    {
        TCHAR szAttachment[MAX_PATH] = _T("");

        // Get the name of the file attached to the message. This can be
        // useful for display purposes, but exercise caution if you want
        // to use that name to store the file locally.
        if (mailMessage.GetAttachedFileName(szAttachment, MAX_PATH))
        {
            _tprintf(_T("Found attachment \"%s\" in part %d\n"), szAttachment, nMessagePart);

            // The recommended approach is to prompt your user for where
            // they would like you to store store the file attachment;
            // avoid using the remote filename directly without validating
            // or prompting the user.
            TCHAR szLocalFile[MAX_PATH] = _T("");

            OPENFILENAME ofn = {0};
            ofn.lStructSize = sizeof(OPENFILENAME);
            ofn.hwndOwner = NULL; // No owner window
            ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
            ofn.lpstrFile = szLocalFile;
            ofn.nMaxFile = MAX_PATH;
            ofn.lpstrTitle = "Select File to Save";
            ofn.Flags = OFN_OVERWRITEPROMPT;

            // If the user cancels the dialog, we are finished
            if (!GetSaveFileName(&ofn))
                 break;

            // Extract the attachment to the file name specified by the user
            if (!mailMessage.ExtractFile(szLocalFile))
            {
                DWORD dwError = mailMessage.GetLastError();
                _tprintf(_T("Failed to extract file (Error 0x%08lX)\n"), dwError);
            }
        }
    }
}          

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