title: How do I programmatically send email w/attachment to a known recipient using MAPI in C++? MAPISendMail()
body: <p><a href="https://stackoverflow.com/questions/262451/how-do-i-programmatically-send-an-email-in-the-same-way-that-i-can-send-to-mail">This question</a> is similar, but does not show how to add a recipient.</p>
  
  <p>How do I do both?</p>
  
  <p>We'd like the widest support possible for as many Windows platforms as possible (from XP and greater)</p>
  
  <p>We're using visual studio 2008</p>
  
  <p>Essentially we want to send an email with:</p>
  
  <ul>
  <li>pre-filled destination address</li>
  <li>file attachment </li>
  <li>subject line</li>
  </ul>
  
  <p>from our program and give the user the ability to add any information or cancel it. </p>
  
  <p>EDIT 
  I am trying to use MAPISendMail()
  I copied much of the code from the questions linked near the top, but I get no email dlg box and the error return I get from the call is: 0x000f - "The system cannot find the drive specified" </p>
  
  <p>If I comment out the lines to set the recipient, it works fine (of course then I have no recipient pre-filled in)</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;tchar.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;mapi.h&gt;
  #include &lt;mapix.h&gt;
  
  int _tmain( int argc, wchar_t *argv[] )
  {
      HMODULE hMapiModule = LoadLibrary( _T( "mapi32.dll" ) );
  
      if ( hMapiModule != NULL )
      {
          LPMAPIINITIALIZE lpfnMAPIInitialize = NULL;
          LPMAPIUNINITIALIZE lpfnMAPIUninitialize = NULL;
          LPMAPILOGONEX lpfnMAPILogonEx = NULL;
          LPMAPISENDDOCUMENTS lpfnMAPISendDocuments = NULL;
          LPMAPISESSION lplhSession = NULL;
          LPMAPISENDMAIL lpfnMAPISendMail = NULL;
  
          lpfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress( hMapiModule, "MAPIInitialize" );
          lpfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress( hMapiModule, "MAPIUninitialize" );
          lpfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress( hMapiModule, "MAPILogonEx" );
          lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress( hMapiModule, "MAPISendDocuments" );
          lpfnMAPISendMail =      (LPMAPISENDMAIL)GetProcAddress( hMapiModule, "MAPISendMail" );
  
          if ( lpfnMAPIInitialize &amp;&amp; lpfnMAPIUninitialize &amp;&amp; lpfnMAPILogonEx &amp;&amp; lpfnMAPISendDocuments )
          {
              HRESULT hr = (*lpfnMAPIInitialize)( NULL );
  
              if ( SUCCEEDED( hr ) )
              {
                  hr = (*lpfnMAPILogonEx)( 0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT, &amp;lplhSession );
  
                  if ( SUCCEEDED( hr ) )
                  {
                      // this opens the email client 
                      // create the msg.  We need to add recipients AND subject AND the dmp file              
  
                      // file attachment
                      MapiFileDesc filedesc;              
                      ::ZeroMemory(&amp;filedesc, sizeof(filedesc));                  
                      filedesc.nPosition = (ULONG)-1;
                      filedesc.lpszPathName = "E:\\Development\\Open\\testmail\\testmail.cpp";    
  
                      // recipient(s)
                      MapiRecipDesc recip;
                      ::ZeroMemory(&amp;recip, sizeof(recip));        
                      recip.lpszName = "QA email";
                      recip.lpszAddress = "qa@myaccount.com";
  
                      // the message
                      MapiMessage msg;
                      ::ZeroMemory(&amp;msg, sizeof(msg));
                      msg.lpszSubject     = "Test";   
                      msg.nRecipCount     = 1; // if I comment out this line it works fine...                 
                      msg.lpRecips        = &amp;recip;                                       
                      msg.nFileCount      = 1;
                      msg.lpFiles         = &amp;filedesc;                
  
                      hr = (*lpfnMAPISendMail)(0, NULL, &amp;msg, MAPI_LOGON_UI|MAPI_DIALOG, 0);
  
                      if ( SUCCEEDED( hr ) )
                      {
                          hr = lplhSession-&gt;Logoff( 0, 0, 0 );
                          hr = lplhSession-&gt;Release();
                          lplhSession = NULL;
                      }
                  }
              }
  
              (*lpfnMAPIUninitialize)();
          }
  
          FreeLibrary( hMapiModule );
      }
  
      return 0;
  }
  </code></pre>