Beispiel #1
0
int CMailMsg::MAPISend()
{

   TStrStrVector::iterator p;
   int                  nIndex = 0;
   size_t               nRecipients = 0;
   MapiRecipDesc*       pRecipients = NULL;
   MapiRecipDesc*       pOriginator = NULL;
   MapiRecipDesc*       pFirstRecipient = NULL;
   size_t               nAttachments = 0;
   MapiFileDesc*        pAttachments = NULL;
   ULONG                status = 0;
   MapiMessage          message;
   std::vector<MapiRecipDesc*>	buffersToFree;
   MapiRecipDesc*       pRecip;
   MapiRecipDesc		grecip;

   if (m_bReady || Initialize())
   {
	  LHANDLE hMapiSession;
	  status = m_lpMapiLogon(NULL, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &hMapiSession);
	  if (SUCCESS_SUCCESS != status) {
		  return FALSE;
	  }

      nRecipients = m_to.size() + m_cc.size() + m_bcc.size() + m_from.size();
      if (nRecipients)
	  {
         pRecipients = new MapiRecipDesc[nRecipients];
		 memset(pRecipients, 0, nRecipients * sizeof  MapiRecipDesc);
	  }

      nAttachments = m_attachments.size();
      if (nAttachments)
         pAttachments = new MapiFileDesc[nAttachments];

      if (pRecipients)
      {
         pFirstRecipient = pRecipients;
         if (m_from.size())
         {
            // set from
			 if (cResolveName(hMapiSession, m_from.begin()->first.c_str(), &pOriginator) == SUCCESS_SUCCESS) {
				buffersToFree.push_back(pOriginator);
			 }
         }
         if (m_to.size())
         {
			 if (cResolveName(hMapiSession, m_to.begin()->first.c_str(), &pRecip) == SUCCESS_SUCCESS) {
				if (pFirstRecipient == NULL)
					pFirstRecipient = &pRecipients[nIndex];
				pRecip->ulRecipClass = MAPI_TO;
				memcpy(&pRecipients[nIndex], pRecip, sizeof pRecipients[nIndex]);
				buffersToFree.push_back(pRecip);
				nIndex++;
			 }
			 else
			 {
				 if (pFirstRecipient == NULL)
					 pFirstRecipient = &pRecipients[nIndex];
				 grecip.ulRecipClass = MAPI_TO;
				 grecip.lpEntryID = 0;
				 grecip.lpszName = 0;
				 grecip.ulEIDSize = 0;
				 grecip.ulReserved = 0;
				 grecip.lpszAddress = (LPTSTR)(LPCTSTR)m_to.begin()->first.c_str();
				 memcpy(&pRecipients[nIndex], &grecip, sizeof pRecipients[nIndex]);
				 nIndex++;
			 }
         }		
         if (m_cc.size())
         {
            // set cc's
            for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
            {
				if ( cResolveName(hMapiSession, p->first.c_str(), &pRecip) == SUCCESS_SUCCESS) {
					if (pFirstRecipient == NULL)
						pFirstRecipient = &pRecipients[nIndex];
					pRecip->ulRecipClass = MAPI_CC;
					memcpy(&pRecipients[nIndex], pRecip, sizeof pRecipients[nIndex]);
					buffersToFree.push_back(pRecip);
					nIndex++;
				}
            }
         }
   
         if (m_bcc.size())
         {
            // set bcc
            for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
            {
				if ( cResolveName(hMapiSession, p->first.c_str(), &pRecip) == SUCCESS_SUCCESS) {
					if (pFirstRecipient == NULL)
						pFirstRecipient = &pRecipients[nIndex];
					pRecip->ulRecipClass = MAPI_BCC;
					memcpy(&pRecipients[nIndex], pRecip, sizeof pRecipients[nIndex]);
					buffersToFree.push_back(pRecip);
					nIndex++;
				}
            }
         }
      }
      if (pAttachments)
      {
         // add attachments
         for (p = m_attachments.begin(), nIndex = 0;
              p != m_attachments.end(); p++, nIndex++)
         {
            pAttachments[nIndex].ulReserved        = 0;
            pAttachments[nIndex].flFlags           = 0;
            pAttachments[nIndex].nPosition         = 0;
            pAttachments[nIndex].lpszPathName      = (LPTSTR)p->first.c_str();
            pAttachments[nIndex].lpszFileName      = (LPTSTR)p->second.c_str();
            pAttachments[nIndex].lpFileType        = NULL;
         }
      }
	  memset(&message, 0, sizeof message);
      message.ulReserved                        = 0;
	  if (!m_sSubject.empty())
	      message.lpszSubject                       = (LPTSTR)m_sSubject.c_str();
	  else
		  message.lpszSubject = "No Subject";
	  if (!m_sMessage.empty())
	      message.lpszNoteText                      = (LPTSTR)m_sMessage.c_str();
	  else
		  message.lpszNoteText = "No Message Body";
      message.lpszMessageType                   = NULL;
      message.lpszDateReceived                  = NULL;
      message.lpszConversationID                = NULL;
      message.flFlags                           = 0;
      message.lpOriginator                      = pOriginator;
      message.nRecipCount                       = nIndex;
      message.lpRecips                          = pFirstRecipient;
      message.nFileCount                        = nAttachments;
      message.lpFiles                           = pAttachments;

      status = m_lpMapiSendMail(hMapiSession, 0, &message, MAPI_DIALOG, 0);
		  
      m_lpMapiLogoff(hMapiSession, NULL, 0, 0);
	  std::vector<MapiRecipDesc*>::iterator iter;
	  for (iter = buffersToFree.begin(); iter != buffersToFree.end(); iter++) {
		  m_lpMapiFreeBuffer(*iter);
	  }
if (SUCCESS_SUCCESS != status) {
				string txt;
				TCHAR buf[MAX_PATH];
				_tprintf_s(buf, "Message did not get sent due to error code %d.\r\n", status);
				txt = buf;
			switch (status)
			{  
			case MAPI_E_AMBIGUOUS_RECIPIENT:
				txt += "A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. No message was sent.\r\n" ;
				break;
			case MAPI_E_ATTACHMENT_NOT_FOUND:
				txt += "The specified attachment was not found. No message was sent.\r\n" ;
				break;
			case MAPI_E_ATTACHMENT_OPEN_FAILURE:
				txt += "The specified attachment could not be opened. No message was sent.\r\n" ;
				break;
			case MAPI_E_BAD_RECIPTYPE:
				txt += "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. No message was sent.\r\n" ;
				break;
			case MAPI_E_FAILURE:
				txt += "One or more unspecified errors occurred. No message was sent.\r\n" ;
				break;
			case MAPI_E_INSUFFICIENT_MEMORY:
				txt += "There was insufficient memory to proceed. No message was sent.\r\n" ;
				break;
			case MAPI_E_INVALID_RECIPS:
				txt += "One or more recipients were invalid or did not resolve to any address.\r\n" ;
				break;
			case MAPI_E_LOGIN_FAILURE:
				txt += "There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed. No message was sent.\r\n" ;
				break;
			case MAPI_E_TEXT_TOO_LARGE:
				txt += "The text in the message was too large. No message was sent.\r\n" ;
				break;
			case MAPI_E_TOO_MANY_FILES:
				txt += "There were too many file attachments. No message was sent.\r\n" ;
				break;
			case MAPI_E_TOO_MANY_RECIPIENTS:
				txt += "There were too many recipients. No message was sent.\r\n" ;
				break;
			case MAPI_E_UNKNOWN_RECIPIENT:
				txt += "A recipient did not appear in the address list. No message was sent.\r\n" ;
				break;
			case MAPI_E_USER_ABORT:
				txt += "The user canceled one of the dialog boxes. No message was sent.\r\n" ;
				break;
			default:
				txt += "Unknown error code.\r\n" ;
				break;
			}
			::MessageBox(0, txt.c_str(), "Error", MB_OK);
}

      if (pRecipients)
         delete [] pRecipients;

      if (nAttachments)
         delete [] pAttachments;
   }

   if (SUCCESS_SUCCESS == status)
	   return 1;
   if (MAPI_E_USER_ABORT == status)
	   return -1;
   // other failure
   return 0;
}
Beispiel #2
0
BOOL CMailMsg::MAPISend()
{
  if(m_lpMapiSendMail==NULL)
      return FALSE;

   TStrStrMap::iterator p;
   int                  nIndex = 0;   
   MapiRecipDesc*       pRecipients = NULL;
   int                  nAttachments = 0;
   MapiFileDesc*        pAttachments = NULL;
   ULONG                status = 0;
   MapiMessage          message;

   if(!m_bReady && !MAPIInitialize())
     return FALSE;
   
   LHANDLE hMapiSession = 0;
   status = m_lpMapiLogon(NULL, NULL, NULL, MAPI_LOGON_UI|MAPI_PASSWORD_UI, NULL, &hMapiSession);
   if(status!=SUCCESS_SUCCESS)
   {
     m_sErrorMsg.Format(_T("MAPILogon has failed with code %X."), status);
     return FALSE;
   }


   pRecipients = new MapiRecipDesc[2];
   if(!pRecipients)
   {
     m_sErrorMsg = _T("Error allocating memory");
     return FALSE;
   }

   nAttachments = (int)m_attachments.size();
   if (nAttachments)
     pAttachments = new MapiFileDesc[nAttachments];
   if(!pAttachments)
   {
     m_sErrorMsg = _T("Error allocating memory");
     return FALSE;
   }

   // set from
   pRecipients[0].ulReserved = 0;
   pRecipients[0].ulRecipClass = MAPI_ORIG;
   pRecipients[0].lpszAddress = (LPSTR)m_from.c_str();
   pRecipients[0].lpszName = "";
   pRecipients[0].ulEIDSize = 0;
   pRecipients[0].lpEntryID = NULL;
      
   // set to
   pRecipients[1].ulReserved = 0;
   pRecipients[1].ulRecipClass = MAPI_TO;
   pRecipients[1].lpszAddress = (LPSTR)m_to.c_str();
   pRecipients[1].lpszName = (LPSTR)m_to.c_str();
   pRecipients[1].ulEIDSize = 0;
   pRecipients[1].lpEntryID = NULL;
      
   
   // add attachments
   nIndex=0;   
    for (p = m_attachments.begin(), nIndex = 0;
      p != m_attachments.end(); p++, nIndex++)
    {
      pAttachments[nIndex].ulReserved        = 0;
      pAttachments[nIndex].flFlags           = 0;
      pAttachments[nIndex].nPosition         = 0xFFFFFFFF;
	    pAttachments[nIndex].lpszPathName      = (LPSTR)p->first.c_str();
	    pAttachments[nIndex].lpszFileName      = (LPSTR)p->second.c_str();
      pAttachments[nIndex].lpFileType        = NULL;
    }
    
    message.ulReserved                        = 0;
	  message.lpszSubject                       = (LPSTR)m_sSubject.c_str();
    message.lpszNoteText                      = (LPSTR)m_sMessage.c_str();
    message.lpszMessageType                   = NULL;
    message.lpszDateReceived                  = NULL;
    message.lpszConversationID                = NULL;
    message.flFlags                           = 0;
    message.lpOriginator                      = pRecipients;
    message.nRecipCount                       = 1;
    message.lpRecips                          = &pRecipients[1];
    message.nFileCount                        = nAttachments;
    message.lpFiles                           = nAttachments ? pAttachments : NULL;
        
    status = m_lpMapiSendMail(hMapiSession, 0, &message, 0/*MAPI_DIALOG*/, 0);    

    if(status!=SUCCESS_SUCCESS)
    {
      m_sErrorMsg.Format(_T("MAPISendMail has failed with code %X."), status);      
    }

    m_lpMapiLogoff(hMapiSession, NULL, 0, 0);

    if (pRecipients)
       delete [] pRecipients;

    if (nAttachments)
         delete [] pAttachments;
   
   return (SUCCESS_SUCCESS == status);
}
Beispiel #3
0
BOOL CMailMsg::MAPISend()
{
	TStrStrMap::iterator p;
	int                  nIndex = 0;
	int                  nRecipients = 0;
	MapiRecipDesc*       pRecipients = NULL;
	int                  nAttachments = 0;
	MapiFileDesc*        pAttachments = NULL;
	ULONG                status = 0;
	MapiMessage          message;

	USES_CONVERSION;

	if (m_bReady || Initialize())
	{
		nRecipients = m_to.size() + m_cc.size() + m_bcc.size() + m_from.size();
		if (nRecipients)
			pRecipients = new MapiRecipDesc[nRecipients];

		nAttachments = m_attachments.size();
		if (nAttachments)
			pAttachments = new MapiFileDesc[nAttachments];

		if (pRecipients)
		{
			if (m_from.size())
			{
				// set from
				pRecipients[nIndex].ulReserved                 = 0;
				pRecipients[nIndex].ulRecipClass               = MAPI_ORIG;
				pRecipients[nIndex].lpszAddress                = T2A((LPTSTR)(LPCTSTR)m_from.begin()->first);
				pRecipients[nIndex].lpszName                   = T2A((LPTSTR)(LPCTSTR)m_from.begin()->second);
				pRecipients[nIndex].ulEIDSize                  = 0;
				pRecipients[nIndex].lpEntryID                  = NULL;
				nIndex++;
			}

			if (m_to.size())
			{
				// set to
				pRecipients[nIndex].ulReserved                 = 0;
				pRecipients[nIndex].ulRecipClass               = MAPI_TO;
				pRecipients[nIndex].lpszAddress                = T2A((LPTSTR)(LPCTSTR)m_to.begin()->first);
				pRecipients[nIndex].lpszName                   = T2A((LPTSTR)(LPCTSTR)m_to.begin()->second);
				pRecipients[nIndex].ulEIDSize                  = 0;
				pRecipients[nIndex].lpEntryID                  = NULL;
				nIndex++;
			}

			if (m_cc.size())
			{
				// set cc's
				for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
				{
					pRecipients[nIndex].ulReserved         = 0;
					pRecipients[nIndex].ulRecipClass       = MAPI_CC;
					pRecipients[nIndex].lpszAddress        = T2A((LPTSTR)(LPCTSTR)p->first);
					pRecipients[nIndex].lpszName           = T2A((LPTSTR)(LPCTSTR)p->second);
					pRecipients[nIndex].ulEIDSize          = 0;
					pRecipients[nIndex].lpEntryID          = NULL;
				}
			}

			if (m_bcc.size())
			{
				// set bcc
				for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
				{
					pRecipients[nIndex].ulReserved         = 0;
					pRecipients[nIndex].ulRecipClass       = MAPI_BCC;
					pRecipients[nIndex].lpszAddress        = T2A((LPTSTR)(LPCTSTR)p->first);
					pRecipients[nIndex].lpszName           = T2A((LPTSTR)(LPCTSTR)p->second);
					pRecipients[nIndex].ulEIDSize          = 0;
					pRecipients[nIndex].lpEntryID          = NULL;
				}
			}
		}

		if (pAttachments)
		{
			// add attachments
			for (p = m_attachments.begin(), nIndex = 0;
			p != m_attachments.end(); p++, nIndex++)
			{
				pAttachments[nIndex].ulReserved        = 0;
				pAttachments[nIndex].flFlags           = 0;
				pAttachments[nIndex].nPosition         = 0xFFFFFFFF;
				pAttachments[nIndex].lpszPathName      = T2A((LPTSTR)(LPCTSTR)p->first);
				pAttachments[nIndex].lpszFileName      = T2A((LPTSTR)(LPCTSTR)p->second);
				pAttachments[nIndex].lpFileType        = NULL;
			}
		}

		message.ulReserved                        = 0;
		message.lpszSubject                       = T2A((LPTSTR)(LPCTSTR)m_sSubject);
		message.lpszNoteText                      = T2A((LPTSTR)(LPCTSTR)m_sMessage);
		message.lpszMessageType                   = NULL;
		message.lpszDateReceived                  = NULL;
		message.lpszConversationID                = NULL;
		message.flFlags                           = 0;
		message.lpOriginator                      = m_from.size() ? pRecipients : NULL;
		message.nRecipCount                       = nRecipients - m_from.size(); // don't count originator
		message.lpRecips                          = nRecipients - m_from.size() ? &pRecipients[m_from.size()] : NULL;
		message.nFileCount                        = nAttachments;
		message.lpFiles                           = nAttachments ? pAttachments : NULL;


		LHANDLE hMapiSession = NULL;
		m_lpMapiLogon(NULL, NULL, NULL, 0, 0, &hMapiSession);
		status = m_lpMapiSendMail(hMapiSession, 0, &message, MAPI_DIALOG, 0);
		m_lpMapiLogoff(hMapiSession, NULL, 0, 0);

		if (pRecipients)
			delete [] pRecipients;

		if (nAttachments)
			delete [] pAttachments;
	}

	return (SUCCESS_SUCCESS == status);
}