所以,为了启发那些帮助过我的人,以及任何可能有类似问题并发生在这个线程上的人,这就是我最终要做的......
我放弃了纯 MAPI 路线,决定转而使用 Outlook PIA。所以现在我的代码如下所示:
#define nul Reflection::Missing::Value
#include "stdafx.h"
using namespace System;
using namespace Microsoft::Office::Interop;
int main(array<System::String ^> ^args)
{
// Create the outlook object
Outlook::Application^ olApp = gcnew Outlook::Application();
// Get an instance of the MAPI namespace via the outlook object
Outlook::NameSpace^ olNs = olApp->GetNamespace("MAPI");
// Log this instance into the MAPI interface
// Note that this will cause the user to be prompted for which profile
// to use. At least, it prompts me, and I only have one profile anyway. Whatever.
// Change the first argument to the name of the profile you want it to use (String)
// to bypass this prompt. I personally have mine set to "Outlook".
olNs->Logon(nul, nul, true, true);
// Get my Inbox
Outlook::Folder^ defFolder = safe_cast<Outlook::Folder^>(olNs->GetDefaultFolder(Outlook::OlDefaultFolders::olFolderInbox));
// Get all of the items in the folder (messages, meeting notices, etc.)
Outlook::Items^ fItems = defFolder->Items;
// Sort them according to when they were received, descending order (most recent first)
fItems->Sort("[ReceivedTime]", true);
// Show me the folder name, and how many items are in it
printf("\n%s: %d\n", defFolder->Name, fItems->Count);
// Make an array of _MailItems to hold the messages.
// Note that this is a _MailItem array, so it will only hold email messages.
// Other item types (in my case, meeting notices) cannot be added to this array.
array<Outlook::_MailItem^>^ mail = gcnew array<Outlook::_MailItem^>(fItems->Count);
int itemNum = 1;
int offset = 0;
// If there's anything in my Inbox, do the following...
if(fItems->Count)
{
// Try to grab the first email with "fItems->GetFirst()", and increment itemNum.
try
{
mail[itemNum++] = safe_cast<Outlook::_MailItem^>(fItems->GetFirst());
}
// If it threw an exception, it's probably because the item isn't a _MailItem type.
// Since nothing got assigned to mail[1], reset itemNum to 1
catch(Exception^ eResult)
{
itemNum = 1;
}
// Ok, now use "fItems->GetNext()" to grab the rest of the messages
for(; itemNum <= (fItems->Count-offset); itemNum++)
{
try
{
mail[itemNum] = safe_cast<Outlook::_MailItem^>(fItems->GetNext());
}
// If it puked, then nothing got assigned to mail[itemNum]. On the next iteration of
// this for-loop, itemNum will be incremented, which means that *this* particular index
// of the mail array would be left empty. To prevent this, decrement itemNum.
// Also, if itemNum ever gets decremented, then that will ultimately cause this loop
// to iterate more times than fItems->Count, causing an out-of-bounds error. To prevent
// that, increment "offset" each time you decrement "itemNum" to compensate for the
// offset.
catch(Exception^ eResult)
{
itemNum--;
offset++;
}
}
// Show me the money!
for(int i=1; i <= (fItems->Count-offset); i++)
printf("%d - %s\n", i, mail[i]->Subject);
}
olNs->Logoff();
return 0;
}
现在我知道如何进入,我可以继续完成我的项目了!谢谢各位的帮助!
干杯! d