CMailMessage::GetLinks Method  
 
LONG GetLinks(
  LPTSTR lpszLinkList,  
  LONG nMaxLength,  
  LONG *lpnLinkCount  
  DWORD dwLinkTypes,  
);
LONG GetLinks(
  CString &strLinkList,  
  LONG *lpnLinkCount  
  DWORD dwLinkTypes,  
);
INT GetLinks(
  CStringArray &arrLinks,  
  DWORD dwLinkTypes  
);
INT GetLinks(
  CSimpleArray<CString> &arrLinks,  
  DWORD dwLinkTypes  
);

The GetLinks method returns the hyperlinks in the current message part.

Parameters

lpszLinkList
A null-terminated string buffer which will contain all of the matching links found in the current message part. This buffer must be large enough to contain the entire list, including the terminating null character. If you wish to determine the number of characters required to store the list, this parameter can be NULL and the nMaxLength parameter should be zero. If you are dynamically allocating memory for the list, make sure you are including additional space for the terminating null. If multiple links are found in the message contents, each link will be separated with a single space. An alternate version of this method accepts a reference to a CString object if the project uses MFC or ATL.
arrLinks
An alternate version of this method accepts a reference to a CStringArray object in MFC or a CSimpleArray object in ATL. In this case, the list of links will be split and returned as a collection of CString objects. Note that this version of the method does not return the total length of the link list. Instead, it returns the number of links added to the array.
nMaxLength
The maximum number of characters which can be copied into the string buffer. The buffer must be large enough to store the complete list of links and the terminating null character. If this parameter is zero, the list of links are not copied to the buffer, but the method will return the number of characters required to store the complete list of links, not including the terminating null. If the lpszLinkList parameter is NULL, this parameter must be zero.
lpnLinkCount
A pointer to an optional integer which will contain the number of matching links extracted from the current message part. If this information is not required, this parameter can be NULL. This value will be initialized to zero and updated with the link count if the method is successful.
dwLinkTypes
An unsigned integer value which specifies one or more bitflags which identify what types of links you would like to extract. To extract all supported link types, omit this parameter or use a value of 0.
Value Description
MIME_LINK_HTTP Return standard http:// links in the current message part.
MIME_LINK_HTTPS Return secure https:// links in the current message part.
MIME_LINK_FTP Return standard ftp:// links in the current message part.
MIME_LINK_FTPS Return secure ftps:// and ftpes:// links in the current message part.
MIME_LINK_SFTP Return secure sftp:// links in the current message part.
MIME_LINK_FILE Return local file:// links in the current message part.

Return Value

If the method succeeds, the return value is the number of characters copied into the string buffer provided. If the nMaxLength parameter is zero, the return value specifies the number of characters required to store the list of links, not including the terminating null character. If you use this method to determine the required buffer size, allocate memory for the returned character count plus one additional character for the terminating null. If the method fails, it will return zero. To get extended error information, call the GetLastError method. The last error code will be set to NO_ERROR if the parameters are valid but there are no links found in the current message part.

If you use the version of this method which returns the list of links in a CString collection, the return value will be the number of links added to the collection. These versions of this method are only available to applications which are built with support for the Microsoft Foundation Classes (MFC) or Active Template Library (ATL).

Remarks

The GetLinks method enables you to obtain a list of the hyperlinks which are found in the current message part. The dwLinkTypes parameter allows you to specify the types of links you would like to extract. The message contents are decoded and converted to UTF-8 before searching for matching links. If the message content uses HTML markup, it is possible for this method to return links in the HTML header as well as the body. Messages which are composed using Outlook will typically have links to XML namespaces like http://www.w3.org/TR/REC-html40. You can omit these links by not including MIME_LINK_HTTP in the link type bitmask.

This method can only be used to extract links from message text and cannot be used with file attachments or encoded binary data. For example, if the current message part contains encoded image data, this method will return zero. This method ignores any links that use URL schemes not listed above, such as mailto: or data:. If you have a need to find a different type of link, you can use the FindText method to search the message using a regular expression that matches your specific requirements.

Example

if (!mailMessage.Open(TEST_HTMLMSG)
{
     // Unable to open the message file
     return;
}

// Get the number of parts in a multipart message; if there is only
// one part then this is an simple RFC 822 formatted message
INT nPartCount = mailMessage.GetPartCount();

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

    // If this message part is not plain text or HTML, move to the next
    if (mailMessage.GetContentType() != MIME_CONTENT_TEXT)
        continue;

    // Populate an ATL CSimpleArray<CString> object with any links
    CSimpleArray<CString> arrLinks;
    INT nLinkCount = mailMessage.GetLinks(arrLinks, MIME_LINK_HTTPS);

    if (nLinkCount > 0)
    {
        _tprintf(_T("Message part %d has %d secure links\n"), nMessagePart, nLinkCount);

        // Print each of the links in this message part
        for (INT nIndex = 0; nIndex < nLinkCount; nIndex++)
            _tprintf(_T("\tLink %d: %s\n"), nIndex + 1, (LPCTSTR)arrLinks[nIndex]);
    }
}

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