Example #1
0
HRESULT ReadNamedProperties(LPMESSAGE lpMessage, ULONG ulCount, LPMAPINAMEID *lppTag,
			    LPSPropTagArray *lppPropertyTagArray, LPSPropValue *lppPropertyArray)
{
	HRESULT hr = hrSuccess;
	ULONG ulReadCount = 0;

	hr = lpMessage->GetIDsFromNames(ulCount, lppTag, 0, lppPropertyTagArray);
	if(hr != hrSuccess) {
		cout << "Failed to obtain IDs from names." << endl;
		/*
		 * Override status to make sure FAILED() will catch this,
		 * this is required to make sure the called won't attempt
		 * to access lppPropertyArray.
		 */
		hr = MAPI_E_CALL_FAILED;
		goto exit;
	}

	hr = lpMessage->GetProps(*lppPropertyTagArray, 0, &ulReadCount, lppPropertyArray);
	if (FAILED(hr)) {
		cout << "Failed to obtain all properties." << endl;
		goto exit;
	}

exit:
	return hr;
}
Example #2
0
BOOL CMapiApi::HandleContentsItem( ULONG oType, ULONG cb, LPENTRYID pEntry)
{
  if (oType == MAPI_MESSAGE) {
    LPMESSAGE pMsg;
    if (OpenEntry( cb, pEntry, (LPUNKNOWN *) &pMsg)) {
      LPSPropValue pVal;
      pVal = GetMapiProperty( pMsg, PR_SUBJECT);
      ReportStringProp( "PR_SUBJECT:", pVal);
      pVal = GetMapiProperty( pMsg, PR_DISPLAY_BCC);
      ReportStringProp( "PR_DISPLAY_BCC:", pVal);
      pVal = GetMapiProperty( pMsg, PR_DISPLAY_CC);
      ReportStringProp( "PR_DISPLAY_CC:", pVal);
      pVal = GetMapiProperty( pMsg, PR_DISPLAY_TO);
      ReportStringProp( "PR_DISPLAY_TO:", pVal);
      pVal = GetMapiProperty( pMsg, PR_MESSAGE_CLASS);
      ReportStringProp( "PR_MESSAGE_CLASS:", pVal);
      ListProperties( pMsg);
      pMsg->Release();
    }
    else {
      MAPI_TRACE0( "    Folder type - error opening\n");
    }
  }
  else
    MAPI_TRACE1( "    ObjectType: %ld\n", oType);

  return( TRUE);
}
Example #3
0
/* Like all the other Complete methods this one is called after OnOpen
   has been called for all registered extensions.  FLAGS may have these
   values:

    0 - The open action has not been canceled.

    EEME_FAILED or EEME_COMPLETE_FAILED - both indicate that the
        open action has been canceled.  

    Note that this method may be called more than once for each OnOpen
    and may even occur without an OnOpen (i.e. while setting up a new
    extension).

    The same return values as for OnOpen may be used..
*/ 
STDMETHODIMP 
GpgolItemEvents::OnOpenComplete (LPEXCHEXTCALLBACK eecb, ULONG flags)
{
  HRESULT hr;
  
  log_debug ("%s:%s: received, flags=%#lx", SRCNAME, __func__, flags);

  /* If the message has been processed by us (i.e. in OnOpen), we now
     use our own display code.  */
  if (!flags && m_processed)
    {
      LPMDB mdb = NULL;
      LPMESSAGE message = NULL;
      HWND hwnd = NULL;

      if (FAILED (eecb->GetWindow (&hwnd)))
        hwnd = NULL;
      hr = eecb->GetObject (&mdb, (LPMAPIPROP *)&message);
      if (hr != S_OK || !message) 
        log_error ("%s:%s: error getting message: hr=%#lx",
                   SRCNAME, __func__, hr);
      else
        {
          if (message_display_handler (message, hwnd))
            m_wasencrypted = true;
        }
      if (message)
        message->Release ();
      if (mdb)
        mdb->Release ();
    }
  
  return S_FALSE;
}
Example #4
0
HRESULT AddMailW(LPMAPISESSION lpMAPISession,
	LPMAPIFOLDER lpFolder,
	LPWSTR szSubject, // PR_SUBJECT_W, PR_CONVERSATION_TOPIC
	LPWSTR szBody, // PR_BODY_W
	LPWSTR szRecipientName, // Recipient table
	BOOL bHighImportance, // PR_IMPORTANCE
	BOOL bReadReceipt, // PR_READ_RECEIPT_REQUESTED
	BOOL bSubmit,
	BOOL bDeleteAfterSubmit)
{
	if (!lpFolder) return MAPI_E_INVALID_PARAMETER;
	HRESULT hRes = S_OK;
	LPMESSAGE lpMessage = 0;

	// Create a message and set its properties
	hRes = lpFolder->CreateMessage(0,
		0,
		&lpMessage);
	if (SUCCEEDED(hRes))
	{
		// Since we know in advance which props we'll be setting, we can statically declare most of the structures involved and save expensive MAPIAllocateBuffer calls
		hRes = SetPropsW(lpMessage, szSubject, szBody, szRecipientName, bHighImportance, bReadReceipt, bSubmit, bDeleteAfterSubmit, KEEP_OPEN_READWRITE, lpMAPISession);
	}
	if (lpMessage) {
		lpMessage->Release();
		lpMessage = NULL;
	}
	return hRes;
}
Example #5
0
HRESULT ReadProperties(LPMESSAGE lpMessage, ULONG ulCount, ULONG *lpTag, LPSPropValue *lppPropertyArray)
{
	HRESULT hr = hrSuccess;
	LPSPropTagArray lpPropertyTagArray = NULL;
	ULONG ulPropertyCount = 0;

	hr = MAPIAllocateBuffer(sizeof(SPropTagArray) + (sizeof(ULONG) * ulCount), (void**)&lpPropertyTagArray);
	if (hr != hrSuccess)
		goto exit;

	lpPropertyTagArray->cValues = ulCount;
	for (ULONG i = 0; i < ulCount; i++)
		lpPropertyTagArray->aulPropTag[i] = lpTag[i];

	hr = lpMessage->GetProps(lpPropertyTagArray, 0, &ulPropertyCount, lppPropertyArray);
	if (FAILED(hr)) {
		cout << "Failed to obtain all properties." << endl;
		goto exit;
	}

exit:
	if (lpPropertyTagArray)
		MAPIFreeBuffer(lpPropertyTagArray);

	return hr;
}
Example #6
0
HRESULT AddRecipientW(LPMESSAGE lpMessage,
	ULONG ulRecipientType,
	LPWSTR szRecipientName){
	HRESULT			hRes = S_OK;
	LPADRLIST		lpAdrList = NULL;  // ModifyRecips takes LPADRLIST
	LPADRBOOK		lpAddrBook = NULL;

	
	hRes = HrAllocAdrList(NUM_RECIP_PROPS, &lpAdrList);
	if (SUCCEEDED(hRes) && lpAdrList)
	{
		// Set up the recipient by indicating how many recipients
		// and how many properties will be set on each recipient.
		lpAdrList->cEntries = 1;	// How many recipients.
		lpAdrList->aEntries[0].cValues = NUM_RECIP_PROPS;  // How many properties per recipient

		lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].ulPropTag = PR_DISPLAY_NAME_W;
		lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].ulPropTag = PR_RECIPIENT_TYPE;

		lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].Value.lpszW = szRecipientName;
		lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].Value.l = ulRecipientType;

		// If everything goes right, add the new recipient to the message
		// object passed into us.
		hRes = lpMessage->ModifyRecipients(MODRECIP_ADD, lpAdrList);
		
	}
	
	if (lpAdrList) FreePadrlist(lpAdrList);
	if (lpAddrBook) lpAddrBook->Release();
	return hRes;
}
Example #7
0
LPSPropValue Workshare::Mail::Mapi::GetMessageTag(LPMESSAGE lpMessage, const std::tstring& sTag)
{
	LPSPropValue pPropVal = 0;

	IMAPIPropPtr spMapiProp;
	HRESULT hr = lpMessage->QueryInterface(IID_IMAPIProp, (void**) &spMapiProp);
	if(SUCCEEDED(hr))
	{
		LPMAPINAMEID pTagName = 0;
		hr = MAPIAllocateBuffer(sizeof(MAPINAMEID), (LPVOID*)&pTagName);
		if(SUCCEEDED(hr))
		{
			pTagName[0].lpguid = (LPGUID)&PS_PUBLIC_STRINGS;
			pTagName[0].ulKind = MNID_STRING;
			_bstr_t bsTag(sTag.c_str());
			pTagName[0].Kind.lpwstrName = bsTag;

			LPSPropTagArray pPropTag = 0;
			hr = spMapiProp->GetIDsFromNames(1, &pTagName, 0, &pPropTag);
			if (!FAILED(hr))
			{
				if(1 == pPropTag->cValues && PT_ERROR != PROP_TYPE(pPropTag->aulPropTag[0]))
				{
					ULONG ulCount = 0;
					spMapiProp->GetProps(pPropTag, 0, &ulCount, &pPropVal);
				}
				MAPIFreeBuffer(pPropTag);
			}
			MAPIFreeBuffer(pTagName);
		}
	}
	return pPropVal;
}
Example #8
0
HRESULT AddRecipientA(LPMAPISESSION lpMAPISession,
	LPMESSAGE lpMessage,
	ULONG ulRecipientType,
	LPSTR szRecipientName)
{
	HRESULT			hRes = S_OK;
	LPADRLIST		lpAdrList = NULL;  // ModifyRecips takes LPADRLIST
	LPADRBOOK		lpAddrBook = NULL;

	if (!lpMessage || !lpMAPISession) return MAPI_E_INVALID_PARAMETER;

	hRes = lpMAPISession->OpenAddressBook(
		NULL,
		NULL,
		NULL,
		&lpAddrBook);
	if (SUCCEEDED(hRes) && lpAddrBook)
	{
		hRes = HrAllocAdrList(NUM_RECIP_PROPS, &lpAdrList);
		if (SUCCEEDED(hRes) && lpAdrList)
		{
			// Set up the recipient by indicating how many recipients
			// and how many properties will be set on each recipient.
			lpAdrList->cEntries = 1;	// How many recipients.
			lpAdrList->aEntries[0].cValues = NUM_RECIP_PROPS;  // How many properties per recipient

			lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].ulPropTag = PR_DISPLAY_NAME_A;
			lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].ulPropTag = PR_RECIPIENT_TYPE;

			lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].Value.lpszA = szRecipientName;
			lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].Value.l = ulRecipientType;

			hRes = lpAddrBook->ResolveName(
				0L,
				MAPI_UNICODE,
				NULL,
				lpAdrList);
			if (SUCCEEDED(hRes))
			{
				// If everything goes right, add the new recipient to the message
				// object passed into us.
				hRes = lpMessage->ModifyRecipients(MODRECIP_ADD, lpAdrList);
			}
		}
	}
	if (lpAdrList) FreePadrlist(lpAdrList);
	if (lpAddrBook) lpAddrBook->Release();
	return hRes;
}
Example #9
0
HRESULT AddReportTag(LPMESSAGE lpMessage)
{
	if (!lpMessage) return MAPI_E_INVALID_PARAMETER;
	HRESULT hRes = S_OK;
	ULONG cValues = 0;
	LPSPropValue lpPropArray = NULL;

	SizedSPropTagArray(2, sptaProps) = { 2, { PR_PARENT_ENTRYID, PR_SEARCH_KEY } };

	hRes = lpMessage->GetProps((LPSPropTagArray)&sptaProps, 0, &cValues, &lpPropArray);
	if (SUCCEEDED(hRes))
	{
		SPropValue sProp = { 0 };
		sProp.ulPropTag = PR_REPORT_TAG;
		hRes = BuildReportTag(NULL,
			NULL,
			NULL,
			NULL,
			NULL,
			NULL,
			(lpPropArray[0].ulPropTag == PR_PARENT_ENTRYID) ? lpPropArray[0].Value.bin.cb : 0,
			(lpPropArray[0].ulPropTag == PR_PARENT_ENTRYID) ? lpPropArray[0].Value.bin.lpb : 0,
			(lpPropArray[1].ulPropTag == PR_SEARCH_KEY) ? lpPropArray[1].Value.bin.cb : 0,
			(lpPropArray[1].ulPropTag == PR_SEARCH_KEY) ? lpPropArray[1].Value.bin.lpb : 0,
			"",
			&sProp.Value.bin.cb,
			&sProp.Value.bin.lpb);
		if (SUCCEEDED(hRes) && sProp.Value.bin.cb && sProp.Value.bin.lpb)
		{
			hRes = lpMessage->SetProps(1, &sProp, NULL);
		}
		delete[] sProp.Value.bin.lpb;
	}

	return hRes;
}
Example #10
0
/**
 * Writes the new timestamp in the store to now() when the last quota
 * mail was send to this store.  The store must have been opened as
 * SYSTEM user, which can always write, eventhough the store is over
 * quota.
 *
 * @param[in]	lpMDB	Store to update the last quota send timestamp in
 * @return MAPI error code
 */
HRESULT ECQuotaMonitor::UpdateQuotaTimestamp(LPMESSAGE lpMessage)
{
	HRESULT hr = hrSuccess;
	SPropValue sPropTime;
	FILETIME ft;

	GetSystemTimeAsFileTime(&ft);

	sPropTime.ulPropTag = PR_EC_QUOTA_MAIL_TIME;
	sPropTime.Value.ft = ft;

	hr = HrSetOneProp(lpMessage, &sPropTime);
	if (hr != hrSuccess)
		goto exit;

	hr = lpMessage->SaveChanges(KEEP_OPEN_READWRITE);
	if (hr != hrSuccess) {
		m_lpThreadMonitor->lpLogger->Log(EC_LOGLEVEL_FATAL, "Unable to save config message, error code: 0x%08X", hr);
		goto exit;
	}

exit:
	return hr;
}
nsresult nsOutlookMail::ImportAddresses(uint32_t *pCount, uint32_t *pTotal, const PRUnichar *pName, uint32_t id, nsIAddrDatabase *pDb, nsString& errors)
{
  if (id >= (uint32_t)(m_addressList.GetSize())) {
    IMPORT_LOG0("*** Bad address identifier, unable to import\n");
    return NS_ERROR_FAILURE;
  }

  uint32_t  dummyCount = 0;
  if (pCount)
    *pCount = 0;
  else
    pCount = &dummyCount;

  CMapiFolder *pFolder;
  if (id > 0) {
    int32_t idx = (int32_t) id;
    idx--;
    while (idx >= 0) {
      pFolder = m_addressList.GetItem(idx);
      if (pFolder->IsStore()) {
        OpenMessageStore(pFolder);
        break;
      }
      idx--;
    }
  }

  pFolder = m_addressList.GetItem(id);
  OpenMessageStore(pFolder);
  if (!m_lpMdb) {
    IMPORT_LOG1("*** Unable to obtain mapi message store for address book: %S\n", pName);
    return NS_ERROR_FAILURE;
  }

  if (pFolder->IsStore())
    return NS_OK;

  nsresult  rv;

  nsCOMPtr<nsIImportFieldMap>    pFieldMap;

  nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
  if (NS_SUCCEEDED(rv)) {
    rv = impSvc->CreateNewFieldMap(getter_AddRefs(pFieldMap));
  }

  CMapiFolderContents    contents(m_lpMdb, pFolder->GetCBEntryID(), pFolder->GetEntryID());

  BOOL      done = FALSE;
  ULONG      cbEid;
  LPENTRYID    lpEid;
  ULONG      oType;
  LPMESSAGE    lpMsg;
  nsCString    type;
  LPSPropValue  pVal;
  nsString    subject;

  while (!done) {
    (*pCount)++;

    if (!contents.GetNext(&cbEid, &lpEid, &oType, &done)) {
      IMPORT_LOG1("*** Error iterating address book: %S\n", pName);
      return NS_ERROR_FAILURE;
    }

    if (pTotal && (*pTotal == 0))
      *pTotal = contents.GetCount();

    if (!done && (oType == MAPI_MESSAGE)) {
      if (!m_mapi.OpenMdbEntry(m_lpMdb, cbEid, lpEid, (LPUNKNOWN *) &lpMsg)) {
        IMPORT_LOG1("*** Error opening messages in mailbox: %S\n", pName);
        return NS_ERROR_FAILURE;
      }

      // Get the PR_MESSAGE_CLASS attribute,
      // ensure that it is IPM.Contact
      pVal = m_mapi.GetMapiProperty(lpMsg, PR_MESSAGE_CLASS);
      if (pVal) {
        type.Truncate();
        m_mapi.GetStringFromProp(pVal, type);
        if (type.EqualsLiteral("IPM.Contact")) {
          // This is a contact, add it to the address book!
          subject.Truncate();
          pVal = m_mapi.GetMapiProperty(lpMsg, PR_SUBJECT);
          if (pVal)
            m_mapi.GetStringFromProp(pVal, subject);

          nsIMdbRow* newRow = nullptr;
          pDb->GetNewRow(&newRow);
          // FIXME: Check with Candice about releasing the newRow if it
          // isn't added to the database.  Candice's code in nsAddressBook
          // never releases it but that doesn't seem right to me!
          if (newRow) {
            if (BuildCard(subject.get(), pDb, newRow, lpMsg, pFieldMap)) {
              pDb->AddCardRowToDB(newRow);
            }
          }
        }
        else if (type.EqualsLiteral("IPM.DistList"))
        {
          // This is a list/group, add it to the address book!
          subject.Truncate();
          pVal = m_mapi.GetMapiProperty(lpMsg, PR_SUBJECT);
          if (pVal)
            m_mapi.GetStringFromProp(pVal, subject);
          CreateList(subject.get(), pDb, lpMsg, pFieldMap);
        }
      }

      lpMsg->Release();
    }
  }

  rv = pDb->Commit(nsAddrDBCommitType::kLargeCommit);
  return rv;
}
Example #12
0
/* Update the crypto info icon.  */
static void
update_crypto_info (LPDISPATCH inspector)
{
  HRESULT hr;
  LPDISPATCH button;
  const char *tooltip = "";
  int iconrc = -1;

  button = get_button (inspector, "GpgOL_Inspector_Crypto_Info");
  if (!button)
    {
      log_error ("%s:%s: Crypto Info button not found", SRCNAME, __func__);
      return;
    }

  if (!is_inspector_in_composer_mode (inspector))
    {
      LPDISPATCH obj;
      LPUNKNOWN unknown;
      LPMESSAGE message = NULL;

      obj = get_oom_object (inspector, "get_CurrentItem");
      if (obj)
        {
          unknown = get_oom_iunknown (obj, "MAPIOBJECT");
          if (!unknown)
            log_error ("%s:%s: error getting MAPI object", SRCNAME, __func__);
          else
            {
              hr = unknown->QueryInterface (IID_IMessage, (void**)&message);
              if (hr != S_OK || !message)
                {
                  message = NULL;
                  log_error ("%s:%s: error getting IMESSAGE: hr=%#lx",
                             SRCNAME, __func__, hr);
                }
              unknown->Release ();
            }
          obj->Release ();
        }
      if (message)
        {
          int is_encrypted = 0;
          int is_signed = 0;
          
          switch (mapi_get_message_type (message))
            {
            case MSGTYPE_GPGOL_MULTIPART_ENCRYPTED:
            case MSGTYPE_GPGOL_OPAQUE_ENCRYPTED:
            case MSGTYPE_GPGOL_PGP_MESSAGE:
              is_encrypted = 1;
              if ( mapi_test_sig_status (message) )
                is_signed = 1;
              break;
            case MSGTYPE_GPGOL:
            case MSGTYPE_SMIME:
            case MSGTYPE_UNKNOWN:
              break;
            default:
              is_signed = 1;
              break;
            }
          
          if (is_signed && is_encrypted)
            {
              tooltip =  _("This is a signed and encrypted message.\n"
                           "Click for more information. ");
              iconrc = IDB_DECRYPT_VERIFY_16;
            }
          else if (is_signed)
            {
              tooltip =  _("This is a signed message.\n"
                           "Click for more information. ");
              iconrc = IDB_VERIFY_16;
            }
          else if (is_encrypted)
            {
              tooltip =  _("This is an encrypted message.\n"
                           "Click for more information. ");
              iconrc = IDB_DECRYPT_16;
            }
          
          message->Release ();
        }
    }

  put_oom_string (button, "TooltipText", tooltip);
  if (iconrc != -1)
    put_oom_icon (button, iconrc, 16);
  put_oom_bool (button, "Visible", (iconrc != -1));
  button->Release ();
}
Example #13
0
/* Called for a click on an inspector button.  BUTTON is the button
   object and TAG is the tag value (which is guaranteed not to be
   NULL).  INSTID is the instance ID of the button. */
void
proc_inspector_button_click (LPDISPATCH button, const char *tag, int instid)
{
  LPMESSAGE message;
  HWND hwnd = NULL; /* Fixme  */

  if (!tagcmp (tag, "GpgOL_Inspector_Encrypt"))
    {  
      toggle_button (button, tag, instid);
    }
  else if (!tagcmp (tag, "GpgOL_Inspector_Sign"))
    {  
      toggle_button (button, tag, instid);
    }
  else if (!tagcmp (tag, "GpgOL_Inspector_Verify")
           || !tagcmp (tag, "GpgOL_Inspector_Crypto_Info"))
    {
      LPDISPATCH inspector;

      message = get_message_from_button (instid, &inspector);
      if (message)
        {
          if (message_incoming_handler (message, hwnd, true))
            message_display_handler (message, inspector, hwnd);
          message->Release ();
        }
      if (inspector)
        {
          update_crypto_info (inspector);
          inspector->Release ();
        }
    }
  else if (!tagcmp (tag, "GpgOL_Inspector_Debug-0"))
    {
      log_debug ("%s:%s: command Debug0 (showInfo) called\n",
                 SRCNAME, __func__);
      message = get_message_from_button (instid, NULL);
      if (message)
        {
          message_show_info (message, hwnd);
          message->Release ();
        }
    }
  else if (!tagcmp (tag, "GpgOL_Inspector_Debug-1"))
    {
      log_debug ("%s:%s: command Debug1 (not used) called\n",
                 SRCNAME, __func__);
    }
  else if (!tagcmp (tag, "GpgOL_Inspector_Debug-2"))
    {
      log_debug ("%s:%s: command Debug2 (change message class) called", 
                 SRCNAME, __func__);
      message = get_message_from_button (instid, NULL);
      if (message)
        {
          /* We sync here. */
          mapi_change_message_class (message, 1);
          message->Release ();
        }
    }
  else if (!tagcmp (tag, "GpgOL_Inspector_Debug-3"))
    {
      log_debug ("%s:%s: command Debug3 (revert_message_class) called", 
                 SRCNAME, __func__);
      message = get_message_from_button (instid, NULL);
      if (message)
        {
          int rc = gpgol_message_revert (message, 1, 
                                         KEEP_OPEN_READWRITE|FORCE_SAVE);
          log_debug ("%s:%s: gpgol_message_revert returns %d\n", 
                     SRCNAME, __func__, rc);
          message->Release ();
        }
    }

}
Example #14
0
void SaveMsg2File(){
	ULONG cbStrSize = 0L;
	LPWSTR lpWideCharStr = NULL;
	wchar_t szPath[_MAX_PATH];
	// get temp file directory
	GetTempPath(_MAX_PATH, szPath);
#ifdef MAPI32_W
	wcscat_s(szPath, L"Correct.msg");
#else 
	wcscat_s(szPath, L"Garbage.msg");
#endif
	IStorage *pStorage = NULL;
	LPMSGSESS pMsgSession = NULL;
	LPMESSAGE pMessage = NULL;
	HRESULT hr = S_OK;

	//LPWSTR subject = L"テスト日本の";// L"ceshi测试12";
	//LPWSTR body = L"テスト日本のテスト日本の"; // L"lhy测试12";
	//LPWSTR receipt = L"*****@*****.**";

	LPWSTR subject = L"Η πολιτική αβεβαιότητα ανησυχεί τις αγορές";// L"ceshi测试12";
	LPWSTR body = L"Η πολιτική αβεβαιότητα ανησυχεί τις αγορές"; // L"lhy测试12";
	LPWSTR receipt = L"*****@*****.**";

	LPSTR subjectA = ConvertUnicode2Ansi(subject);
	LPSTR bodyA = ConvertUnicode2Ansi(body);
	LPSTR receiptA = ConvertUnicode2Ansi(receipt);
	//LPSTR subjectA = "ceshi测试12";
	//LPSTR bodyA = "lhy测试12";
	//LPSTR receiptA = "*****@*****.**";

	do{
		MAPIINIT_0 mapiInit = { 0, MAPI_MULTITHREAD_NOTIFICATIONS };
		hr = MAPIInitialize(&mapiInit);
		DEFINE_IF_HR_NT_OK_BREAK(hr);

		LPMALLOC pMalloc = MAPIGetDefaultMalloc();

		hr = StgCreateDocfile(szPath, STGM_READWRITE | STGM_TRANSACTED | STGM_CREATE, 0, &pStorage);
		DEFINE_IF_HR_NT_OK_BREAK(hr);

		
		hr = OpenIMsgSession(pMalloc, 0, &pMsgSession);
		DEFINE_IF_HR_NT_OK_BREAK(hr);
#ifdef MAPI32_W
		// lhy comment:if load exmapi32.dll, this function will failed with error code 0x80040106.
		hr = OpenIMsgOnIStg(pMsgSession, MAPIAllocateBuffer, MAPIAllocateMore, MAPIFreeBuffer, pMalloc, NULL, pStorage, NULL, 0, MAPI_UNICODE, &pMessage); 
#else
		hr = OpenIMsgOnIStg(pMsgSession, MAPIAllocateBuffer, MAPIAllocateMore, MAPIFreeBuffer, pMalloc, NULL, pStorage, NULL, 0, 0, &pMessage);
#endif
		DEFINE_IF_HR_NT_OK_BREAK(hr);

		hr = WriteClassStg(pStorage, CLSID_MailMessage);
		DEFINE_IF_HR_NT_OK_BREAK(hr);
#ifdef MAPI32_W
		hr = SetPropsW(pMessage, subject, body, receipt, false, false, false, false, FORCE_SAVE);
#else 
		
		hr = SetPropsA(pMessage, subjectA, bodyA, receiptA, false, false, false, false, FORCE_SAVE);
#endif
		DEFINE_IF_HR_NT_OK_BREAK(hr);

		hr = pStorage->Commit(STGC_DEFAULT);
		DEFINE_IF_HR_NT_OK_BREAK(hr);

	} while (0);
	
	delete subjectA;
	delete bodyA;
	delete receiptA;
	if (pMessage){
		pMessage->Release();
		pMessage = NULL;
	}
	if (pStorage){
		pStorage->Release();
		pStorage = NULL;
	}
	if (pMsgSession){
		CloseIMsgSession(pMsgSession);
		pMsgSession = NULL;
	}
	MAPIUninitialize();
}
Example #15
0
HRESULT SetPropsA(
	LPMESSAGE lpMessage,
	LPSTR szSubject, // PR_SUBJECT_W, PR_CONVERSATION_TOPIC
	LPSTR szBody, // PR_BODY_W
	LPSTR szRecipientName, // Recipient table
	BOOL bHighImportance, // PR_IMPORTANCE
	BOOL bReadReceipt, // PR_READ_RECEIPT_REQUESTED
	BOOL bSubmit,
	BOOL bDeleteAfterSubmit,
	ULONG msgSaveFlag,
	LPMAPISESSION lpMAPISession = NULL) {
	SPropValue spvProps[NUM_PROPS] = { 0 };
	spvProps[p_PR_MESSAGE_CLASS_W].ulPropTag = PR_MESSAGE_CLASS_A;
	spvProps[p_PR_ICON_INDEX].ulPropTag = PR_ICON_INDEX;
	spvProps[p_PR_SUBJECT_W].ulPropTag = PR_SUBJECT_A;
	spvProps[p_PR_CONVERSATION_TOPIC_W].ulPropTag = PR_CONVERSATION_TOPIC_A;
	spvProps[p_PR_BODY_W].ulPropTag = PR_BODY_A;
	spvProps[p_PR_IMPORTANCE].ulPropTag = PR_IMPORTANCE;
	spvProps[p_PR_READ_RECEIPT_REQUESTED].ulPropTag = PR_READ_RECEIPT_REQUESTED;
	spvProps[p_PR_MESSAGE_FLAGS].ulPropTag = PR_MESSAGE_FLAGS;
	spvProps[p_PR_MSG_EDITOR_FORMAT].ulPropTag = PR_MSG_EDITOR_FORMAT;
	spvProps[p_PR_MESSAGE_LOCALE_ID].ulPropTag = PR_MESSAGE_LOCALE_ID;
	spvProps[p_PR_INETMAIL_OVERRIDE_FORMAT].ulPropTag = PR_INETMAIL_OVERRIDE_FORMAT;
	spvProps[p_PR_DELETE_AFTER_SUBMIT].ulPropTag = PR_DELETE_AFTER_SUBMIT;
	spvProps[p_PR_INTERNET_CPID].ulPropTag = PR_INTERNET_CPID;
	spvProps[p_PR_CONVERSATION_INDEX].ulPropTag = PR_CONVERSATION_INDEX;


	spvProps[p_PR_MESSAGE_CLASS_W].Value.lpszA = "IPM.Note";
	spvProps[p_PR_ICON_INDEX].Value.l = 0x103; // Unsent Mail
	spvProps[p_PR_SUBJECT_W].Value.lpszA = szSubject;
	spvProps[p_PR_CONVERSATION_TOPIC_W].Value.lpszA = szSubject;
	spvProps[p_PR_BODY_W].Value.lpszA = szBody;
	spvProps[p_PR_IMPORTANCE].Value.l = bHighImportance ? IMPORTANCE_HIGH : IMPORTANCE_NORMAL;
	spvProps[p_PR_READ_RECEIPT_REQUESTED].Value.b = bReadReceipt ? true : false;
	spvProps[p_PR_MESSAGE_FLAGS].Value.l = MSGFLAG_UNSENT;
	spvProps[p_PR_MSG_EDITOR_FORMAT].Value.l = EDITOR_FORMAT_PLAINTEXT;
	spvProps[p_PR_MESSAGE_LOCALE_ID].Value.l = 2052;//1033; // (en-us)
	spvProps[p_PR_INETMAIL_OVERRIDE_FORMAT].Value.l = NULL; // Mail system chooses default encoding scheme
	spvProps[p_PR_DELETE_AFTER_SUBMIT].Value.b = bDeleteAfterSubmit ? true : false;
	spvProps[p_PR_INTERNET_CPID].Value.l = CONVERT_CODE_PAGE;// cpidASCII;

	HRESULT hRes = BuildConversationIndex(
		&spvProps[p_PR_CONVERSATION_INDEX].Value.bin.cb,
		&spvProps[p_PR_CONVERSATION_INDEX].Value.bin.lpb);

	if (SUCCEEDED(hRes))
	{
		hRes = lpMessage->SetProps(NUM_PROPS, spvProps, NULL);
		if (SUCCEEDED(hRes))
		{
			if (lpMAPISession)
				hRes = AddRecipientA(lpMAPISession,
				lpMessage,
				MAPI_TO,
				szRecipientName);
			else
				hRes = AddRecipientA(lpMessage,
				MAPI_TO,
				szRecipientName);

			if (SUCCEEDED(hRes))
			{
				if (bReadReceipt)
				{
					hRes = AddReportTag(lpMessage);
				}

				if (SUCCEEDED(hRes))
				{
					hRes = lpMessage->SaveChanges(msgSaveFlag);
					if (SUCCEEDED(hRes) && bSubmit)
					{
						hRes = lpMessage->SubmitMessage(NULL);
					}
				}
			}
		}
	}
	if (spvProps[p_PR_CONVERSATION_INDEX].Value.bin.lpb)
		delete[] spvProps[p_PR_CONVERSATION_INDEX].Value.bin.lpb;

	return hRes;
}