文書番号: 239795 - 最終更新日: 2011年7月22日 - リビジョン: 3.0

MAPI で受信トレイ内のメッセージのリストの方法

お知らせお使いのオペレーティング システムには適用しない情報が含まれている場合があります。
すべて展開する | すべて折りたたむ

概要

拡張 MAPI がメールボックスにアクセスして、メッセージを操作に使用できます。この資料では、MAPI を使用して、受信トレイ内のメッセージの一覧を表示する方法を示します。

詳細

次のサンプル コードは、MAPI を使用して、受信トレイ内のメッセージの一覧を表示する方法を示します。
  • Visual C を開き新しい Win32 コンソール アプリケーションを作成します。
  • "GetInbox"をプロジェクト名を入力します。
  • 切り取りし、「メイン」の手順を置き換えますが、インクルード ファイルのままにするように生成された GetInbox.cpp ファイルで次のコードを貼り付けます。
  • MAPI32.lib を追加するのには、プロジェクトの設定] で、 リンク タブします。
  • コンパイルし、アプリケーションを実行します。
   #include <stdio.h>
   #include <conio.h>
   #include <mapix.h>
   #include <mapiutil.h>

   STDMETHODIMP ListMessages(
   LPMDB lpMDB,
   LPMAPIFOLDER lpInboxFolder);

   STDMETHODIMP OpenDefaultMessageStore(
   LPMAPISESSION lpMAPISession,
   LPMDB * lpMDB);

   STDMETHODIMP OpenInbox(
   LPMDB lpMDB,
   LPMAPIFOLDER *lpInboxFolder);

   void main() 
   {
   HRESULT       hRes;
   LPMAPISESSION lpMAPISession = NULL;
   LPMDB         lpMDB = NULL;
   LPMAPIFOLDER  lpInboxFolder = NULL;
   LPSPropValue  tmp = NULL;

   hRes = MAPIInitialize(NULL);
   if (FAILED(hRes)) goto quit;

   hRes = MAPILogonEx(0,
      NULL,//profile name
      NULL,//password - This parameter should ALWAYS be NULL
      MAPI_LOGON_UI, //Allow a profile picker box to show if not logged in
      &lpMAPISession);//handle of session
   if (FAILED(hRes)) goto quit;

   hRes = OpenDefaultMessageStore(
      lpMAPISession,
      &lpMDB);
   if (FAILED(hRes)) goto quit;

   hRes = OpenInbox(
      lpMDB,
      &lpInboxFolder);
   if (FAILED(hRes)) goto quit;

   //Checking to see that we did get the Inbox
   hRes = HrGetOneProp(
      lpInboxFolder,
      PR_DISPLAY_NAME,
      &tmp);
   if (FAILED(hRes)) goto quit;
   printf("I managed to open the folder '%s'\n",tmp->Value.lpszA);

   hRes = ListMessages(
      lpMDB,
      lpInboxFolder);
   if (FAILED(hRes)) goto quit;

   quit:
   if (tmp) MAPIFreeBuffer(tmp);
   UlRelease(lpInboxFolder);
   UlRelease(lpMDB);
   UlRelease(lpMAPISession);
   MAPIUninitialize();
   if (FAILED(hRes))
   {
      printf("Failed with hRes of %x\n",hRes);		
   }
   printf("Hit any key to continue\n");
   while(!_kbhit()){ Sleep(50);};

   }

  STDMETHODIMP ListMessages(
   LPMDB lpMDB,
   LPMAPIFOLDER lpInboxFolder)
   {
   HRESULT hRes = S_OK;
   LPMAPITABLE lpContentsTable = NULL;
   LPSRowSet pRows = NULL;
   LPSTREAM lpStream = NULL;
   ULONG i;

   //You define a SPropTagArray array here using the SizedSPropTagArray Macro
   //This enum will allows you to access portions of the array by a name instead of a number.
   //If more tags are added to the array, appropriate constants need to be added to the enum.
   enum {
      ePR_SENT_REPRESENTING_NAME,
      ePR_SUBJECT,
      ePR_BODY,
      ePR_PRIORITY,
      ePR_ENTRYID,
      NUM_COLS};
   //These tags represent the message information we would like to pick up
   static SizedSPropTagArray(NUM_COLS,sptCols) = { NUM_COLS,
      PR_SENT_REPRESENTING_NAME,
      PR_SUBJECT,
      PR_BODY,
      PR_PRIORITY,
      PR_ENTRYID
   };

   hRes = lpInboxFolder->GetContentsTable(
      0,
      &lpContentsTable);
   if (FAILED(hRes)) goto quit;

    hRes = HrQueryAllRows(
      lpContentsTable,
      (LPSPropTagArray) &sptCols,
      NULL,//restriction...we're not using this parameter
      NULL,//sort order...we're not using this parameter
      0,
      &pRows);
   if (FAILED(hRes)) goto quit;

   for (i = 0; i < pRows -> cRows; i++)
   {
      LPMESSAGE lpMessage = NULL;
      ULONG ulObjType = NULL;
      LPSPropValue lpProp = NULL;

      printf("Message %d:\n",i);
      if (PR_SENT_REPRESENTING_NAME == pRows -> aRow[i].lpProps[ePR_SENT_REPRESENTING_NAME].ulPropTag)
      {   
         printf("From: %s\n",pRows->aRow[i].lpProps[ePR_SENT_REPRESENTING_NAME].Value.lpszA);
      }   
      if (PR_SUBJECT == pRows -> aRow[i].lpProps[ePR_SUBJECT].ulPropTag)
      {   
         printf("Subject: %s\n",pRows->aRow[i].lpProps[ePR_SUBJECT].Value.lpszA);
      }   
      if (PR_PRIORITY == pRows -> aRow[i].lpProps[ePR_PRIORITY].ulPropTag)
      {   
         printf("Priority: %d\n",pRows->aRow[i].lpProps[ePR_PRIORITY].Value.l);
      }   

   //the following method of printing PR_BODY will not always get the whole body
/*    if (PR_BODY == pRows -> aRow[i].lpProps[ePR_BODY].ulPropTag)
      {   
         printf("Body: %s\n",pRows->aRow[i].lpProps[ePR_BODY].Value.lpszA);
      }
*/ 

      //PR_BODY needs some special processing...
      //The table will only return a portion of the PR_BODY...if you want it all, we should
      //open the message and retrieve the property. GetProps (which HrGetOneProp calls
      //underneath) will do for most messages. For some larger messages, we would need to 
      //trap for MAPI_E_NOT_ENOUGH_MEMORY and call OpenProperty to get a stream on the body.

      if (MAPI_E_NOT_FOUND != pRows -> aRow[i].lpProps[ePR_BODY].Value.l)
      {
         hRes = lpMDB->OpenEntry(
            pRows->aRow[i].lpProps[ePR_ENTRYID].Value.bin.cb,
            (LPENTRYID) pRows->aRow[i].lpProps[ePR_ENTRYID].Value.bin.lpb,
            NULL,//default interface
            MAPI_BEST_ACCESS,
            &ulObjType,
            (LPUNKNOWN *) &lpMessage);
         if (!FAILED(hRes))
         {
            hRes = HrGetOneProp(
               lpMessage,
               PR_BODY,
               &lpProp);
            if (hRes == MAPI_E_NOT_ENOUGH_MEMORY)
            {
               char szBuf[255];
               ULONG ulNumChars;
               hRes = lpMessage->OpenProperty(
                  PR_BODY,
                  &IID_IStream,
                  STGM_READ,
                  NULL,
                  (LPUNKNOWN *) &lpStream);

               do
               {
                  lpStream->Read(
                     szBuf,
                     255,
                     &ulNumChars);
                  if (ulNumChars >0) printf("%.*s",ulNumChars,szBuf);
               }
               while (ulNumChars >= 255);

               printf("\n");

               hRes = S_OK;
            }
            else if (hRes == MAPI_E_NOT_FOUND)
            {
               //This is not an error. Many messages do not have bodies.
               printf("Message has no body!\n");
               hRes = S_OK;
            }
            else
            {
               printf("Body: %s\n",lpProp->Value.lpszA);
            }
         }
      }

      MAPIFreeBuffer(lpProp);
      UlRelease(lpMessage);
      hRes = S_OK;

   }

   quit:
   FreeProws(pRows);
   UlRelease(lpContentsTable);
   return hRes;
   }


   STDMETHODIMP OpenInbox(
   LPMDB lpMDB,
   LPMAPIFOLDER *lpInboxFolder)
   {
   ULONG        cbInbox;
   LPENTRYID    lpbInbox;
   ULONG        ulObjType;
   HRESULT      hRes = S_OK;
   LPMAPIFOLDER	lpTempFolder = NULL;
	
   *lpInboxFolder = NULL;

   //The Inbox is usually the default receive folder for the message store
   //You call this function as a shortcut to get it's Entry ID
   hRes = lpMDB->GetReceiveFolder(
      NULL,      //Get default receive folder
      NULL,      //Flags
      &cbInbox,  //Size and ...
      &lpbInbox, //Value of the EntryID to be returned
      NULL);     //You don't care to see the class returned
   if (FAILED(hRes)) goto quit;

   hRes = lpMDB->OpenEntry(
      cbInbox,                      //Size and...
      lpbInbox,                     //Value of the Inbox's EntryID
      NULL,                         //We want the default interface    (IMAPIFolder)
      MAPI_BEST_ACCESS,             //Flags
      &ulObjType,                   //Object returned type
      (LPUNKNOWN *) &lpTempFolder); //Returned folder
   if (FAILED(hRes)) goto quit;

   //Assign the out parameter
   *lpInboxFolder = lpTempFolder;

   //Always clean up your memory here!
   quit:
   MAPIFreeBuffer(lpbInbox);
   return hRes;
   }

   STDMETHODIMP OpenDefaultMessageStore(
   LPMAPISESSION lpMAPISession,
   LPMDB * lpMDB)
   {
   LPMAPITABLE pStoresTbl = NULL;
   LPSRowSet   pRow = NULL;
   static      SRestriction sres;
   SPropValue  spv;
   HRESULT     hRes;
   LPMDB       lpTempMDB = NULL;

   enum {EID, NAME, NUM_COLS};
   static SizedSPropTagArray(NUM_COLS,sptCols) = {NUM_COLS, PR_ENTRYID, PR_DISPLAY_NAME};

   *lpMDB = NULL;

   //Get the table of all the message stores available
   hRes = lpMAPISession -> GetMsgStoresTable(0, &pStoresTbl);
   if (FAILED(hRes)) goto quit;

   //Set up restriction for the default store
   sres.rt = RES_PROPERTY; //Comparing a property
   sres.res.resProperty.relop = RELOP_EQ; //Testing equality
   sres.res.resProperty.ulPropTag = PR_DEFAULT_STORE; //Tag to compare
   sres.res.resProperty.lpProp = &spv; //Prop tag and value to compare against

   spv.ulPropTag = PR_DEFAULT_STORE; //Tag type
   spv.Value.b   = TRUE; //Tag value

   //Convert the table to an array which can be stepped through
   //Only one message store should have PR_DEFAULT_STORE set to true, so only one will be returned
   hRes = HrQueryAllRows(
   pStoresTbl, //Table to query
   (LPSPropTagArray) &sptCols, //Which columns to get
   &sres, //Restriction to use
   NULL, //No sort order
   0, //Max number of rows (0 means no limit)
   &pRow); //Array to return
   if (FAILED(hRes)) goto quit;

   //Open the first returned (default) message store
   hRes = lpMAPISession->OpenMsgStore(
      NULL,//Window handle for dialogs
      pRow->aRow[0].lpProps[EID].Value.bin.cb,//size and...
      (LPENTRYID)pRow->aRow[0].lpProps[EID].Value.bin.lpb,//value of entry to open
      NULL,//Use default interface (IMsgStore) to open store
      MAPI_BEST_ACCESS,//Flags
      &lpTempMDB);//Pointer to place the store in
   if (FAILED(hRes)) goto quit;

   //Assign the out parameter
   *lpMDB = lpTempMDB;

   //Always clean up your memory here!
quit:
   FreeProws(pRow);
   UlRelease(pStoresTbl);
   if (FAILED(hRes))
   {
      HRESULT hr;
      LPMAPIERROR lpError; 
      hr = lpMAPISession->GetLastError(hRes,0,&lpError);
      if (!hr)
      {
         printf("%s\n%s\n",lpError->lpszError,lpError->lpszComponent);
         MAPIFreeBuffer(lpError);
      }
   }
   return hRes;
   }
				

関連情報

MAPI および EDK 関数を使用して、特定の条件に基づいてメッセージを取得するのには、マイクロソフト サポート技術、次の資料を参照してください。
200181? (http://support.microsoft.com/kb/200181/EN-US/ ) 特定の条件に基づいてメッセージを取得するには、方法

この資料は以下の製品について記述したものです。
  • Microsoft Office Outlook 2007
  • Microsoft Messaging Application Programming Interface
キーワード:?
kbFAQ kbfile kbhowto kbmsg kbmt KB239795 KbMtja
機械翻訳機械翻訳
重要: このサポート技術情報 (以下「KB」) は、翻訳者による翻訳の代わりに、マイクロソフト機械翻訳システムによって翻訳されたものです。マイクロソフトは、お客様に、マイクロソフトが提供している全ての KB を日本語でご利用いただけるように、翻訳者による翻訳 KB に加え機械翻訳 KB も提供しています。しかしながら、機械翻訳の品質は翻訳者による翻訳ほど十分ではありません。誤訳や、文法、言葉使い、その他、たとえば日本語を母国語としない方が日本語を話すときに間違えるようなミスを含んでいる可能性があります。マイクロソフトは、機械翻訳の品質、及び KB の内容の誤訳やお客様が KB を利用されたことによって生じた直接または間接的な問題や損害については、いかなる責任も負わないものとします。マイクロソフトは、機械翻訳システムの改善を継続的に行っています。
英語版 KB:239795? (http://support.microsoft.com/kb/239795/en-us/ )
"Microsoft Knowledge Baseに含まれている情報は、いかなる保証もない現状ベースで提供されるものです。Microsoft Corporation及びその関連会社は、市場性および特定の目的への適合性を含めて、明示的にも黙示的にも、一切の保証をいたしません。さらに、Microsoft Corporation及びその関連会社は、本文書に含まれている情報の使用及び使用結果につき、正確性、真実性等、いかなる表明・保証も行ないません。Microsoft Corporation、その関連会社及びこれらの権限ある代理人による口頭または書面による一切の情報提供またはアドバイスは、保証を意味するものではなく、かつ上記免責条項の範囲を狭めるものではありません。Microsoft Corporation、その関連会社 及びこれらの者の供給者は、直接的、間接的、偶発的、結果的損害、逸失利益、懲罰的損害、または特別損害を含む全ての損害に対して、状況のいかんを問わず一切責任を負いません。(Microsoft Corporation、その関連会社 またはこれらの者の供給者がかかる損害の発生可能性を了知している場合を含みます。) 結果的損害または偶発的損害に対する責任の免除または制限を認めていない地域においては、上記制限が適用されない場合があります。なお、本文書においては、文書の体裁上の都合により製品名の表記において商標登録表示、その他の商標表示を省略している場合がありますので、予めご了解ください。"