CMailMessage::EnumRecipients Method  
 
INT EnumRecipients(
  LPCTSTR lpszExtraAddress,  
  LPTSTR lpBuffer,  
  LPDWORD lpcchBuffer  
);
INT EnumRecipients(
  LPTSTR lpBuffer,  
  LPDWORD lpcchBuffer  
);
INT EnumRecipients(
  CStringArray& arrayRecipients,  
  LPCTSTR lpszExtraAddress  
);

The EnumRecipients method returns a null-terminated list of strings which contain the email address of each recipient for the specified message.

Parameters

lpszExtraAddress
A pointer to a string which contains one or more additional email addresses that should be included in the list, in addition to those found in the message. If more than one address is specified, each address should be separated by a comma. This parameter may be NULL if there are no extra addresses to include in the recipient list.
lpBuffer
Pointer to buffer which will contain zero or more null-terminated strings. The end of the string list is indicated by an additional terminating null. If this parameter is NULL, the method will calculate the minimum number of characters required to store the addresses and return the value in the lpcchBuffer parameter.
lpcchBuffer
A pointer to an unsigned integer which should be initialized to the maximum number of characters that can be copied into the buffer specified by the lpBuffer parameter. When the method returns, it will be updated to contain the actual number of characters copied into the buffer. If the lpBuffer parameter is NULL, then this value will contain the minimum number of characters required to store all of the recipient addresses in the current message.
arrayRecipients
A reference to a CStringArray object that will contain each of the recipient addresses specified in the message. This version of the method is only available if the program is compiled using Microsoft Foundation Classes (MFC).

Return Value

If the method succeeds, the return value is the total number of recipients for the current message. If the method fails, the return value is MIME_ERROR. To get extended error information, call GetLastError.

Remarks

The EnumRecipients method returns a list of recipient email addresses for the specified message, with each address being terminated by a null character. The end of the list is indicated by an additional null character. To determine the size of the buffer you should pass to this function, you can specify the lpBuffer parameter as NULL and initialize the value of the lpcchBuffer parameter to zero.

An alternative to the EnumRecipients method is the GetAllRecipients method that returns a comma-separated list of recipient addresses in a string buffer.

Example

LPTSTR lpRecipients = NULL;
DWORD cchRecipients = 0;
INT nRecipients = 0;

// Determine the number of characters that should be allocated to store
// all of the recipient addresses in the current message

nRecipients = pMessage->EnumRecipients(NULL, &cchRecipients);

// Allocate the memory for the string buffer that will contain all
// of the recipient addresses and call EnumRecipients
// again to store those addresses in the buffer

if (nRecipients > 0 && cchRecipients > 0)
{
    lpRecipients = (LPTSTR)::LocalAlloc(LPTR, (cchRecipients * sizeof(TCHAR));
    if (lpRecipients == NULL)
        return; // Virtual memory exhausted

    nRecipients = pMessage->EnumRecipients(lpRecipients,
                                           &cchRecipients);
}

// Move through the buffer, processing each recipient address
// that was returned

if (nRecipients > 0)
{
    LPTSTR lpszAddress = lpRecipients;
    INT cchAddress;

    while (lpszAddress != NULL)
    {
        if ((cchAddress = lstrlen(lpszAddress)) == 0)
            break;

        // lpszAddress specifies a recipient address
        // Advance to the next address string in the buffer
        lpszAddress += cchAddress + 1;
    }
}

if (lpRecipients != NULL)
{
    LocalFree((HLOCAL)lpRecipients);
    lpRecipients = NULL;
}

Requirements

Minimum Desktop Platform: Windows 7 (Service Pack 1)
Minimum Server Platform: Windows Server 2008 R2 (Service Pack 1)
Header File: cstools10.h
Import Library: csmsgv10.lib
Unicode: Implemented as Unicode and ANSI versions.

See Also

GetAllRecipients, GetFirstHeader, GetHeader, GetNextHeader, SetHeader