Click here to Skip to main content
15,898,010 members
Articles / Programming Languages / C++

SendTo Mail Recipient

Rate me:
Please Sign up or sign in to vote.
4.87/5 (25 votes)
31 Mar 20033 min read 400.8K   2K   45  
Programmatically use the SendTo mail recipient shortcut
This article provides a code snippet in order to programmatically send one or more files to the mail recipient, mimicking the SendTo mail recipient shell extension. I have heard many people searching for this feature, and actually I thought that, as there doesn't seem to be such source code posted on the net, I could just as well post it.
static const IID CLSID_SendMail = { 0x9E56BE60, 0xC50F, 0x11CF, 
   { 0x9A, 0x2C, 0x00, 0xA0, 0xC9, 0x0A, 0x90, 0xCE } 
};

  ::OpenClipboard(NULL);

  // let's send c:\346.jpg and c:\readme.doc by email
  LPTSTR szFile = "c:\\346.jpg\0c:\\readme.doc";
  int cch = _tcslen(szFile);

  HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, 
                       sizeof(DROPFILES) + (cch + 2) * sizeof(TCHAR)); 
  LPDROPFILES pDropFiles = (LPDROPFILES) ::GlobalLock(hglbCopy);
  pDropFiles->pFiles = sizeof(DROPFILES);
  pDropFiles->pt.x = pDropFiles->pt.y = 0;
  pDropFiles->fNC = TRUE;
  pDropFiles->fWide = FALSE; // ANSI charset
  LPTSTR lptstrCopy = (LPTSTR) pDropFiles;
  lptstrCopy += pDropFiles->pFiles;
  memcpy(lptstrCopy, szFile, cch * sizeof(TCHAR)); 
  lptstrCopy[cch] = (TCHAR) '\0';    // null character 
  lptstrCopy[cch+1] = (TCHAR) '\0';    // null character 
  ::GlobalUnlock(hglbCopy); 

  ::SetClipboardData(CF_HDROP, hglbCopy);

  ::CloseClipboard();


  // get an IDataObject from this code
  LPDATAOBJECT pDataObject = NULL;
  HRESULT hr = ::OleGetClipboard(&pDataObject);

  if (!pDataObject)
  {
    ::CloseClipboard();
    return FALSE;
  }


  ::CoInitialize(NULL);

  IDropTarget *pDropTarget = NULL;

  // create an instance, and mimic drag-and-drop
  hr = CoCreateInstance( CLSID_SendMail, 
                         NULL, CLSCTX_ALL,
                         IID_IDropTarget, 
                         (void **)&pDropTarget);
  if (SUCCEEDED(hr))
  {
    POINTL pt = {0,0};
    DWORD dwEffect = 0;
    pDropTarget->DragEnter(pDataObject, MK_LBUTTON, pt, &dwEffect);
    pDropTarget->Drop(pDataObject, MK_LBUTTON, pt, &dwEffect);

    ::Sleep(6*1000);

    pDropTarget->Release();
  }

  ::CoUninitialize();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
France France
Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).

Need a fast Excel generation component? Try xlsgen.

Comments and Discussions