Outlook::_MailItemPtr FindEmail(IDispatchPtr spDispOutlook, IDispatchPtr spDispNameSpace, const std::tstring& subject) { if(spDispOutlook == 0) throw Workshare::ArgumentNullException(_T("spDispOutlook"), _T("Outlook Application pointer is null.")); if(spDispNameSpace == 0) throw Workshare::ArgumentNullException(_T("spDispNameSpace"), _T("MAPI NameSpace pointer is null.")); Outlook::_ApplicationPtr spOutlook = spDispOutlook; Outlook::_NameSpacePtr spNameSpace = spDispNameSpace; Outlook::MAPIFolderPtr spSentItems; HRESULT hr = spNameSpace->raw_GetDefaultFolder(Outlook::olFolderSentMail, &spSentItems); if(FAILED(hr)) throw Workshare::Com::ComException(_T("Failed to get Sent Items folder"), hr); if(spSentItems == 0) throw Workshare::NullReferenceException(_T("Failed to get Sent Items folder")); Outlook::_MailItemPtr spEmailItem; spEmailItem = FindEmailInFolder(spSentItems, subject); if(0 != spEmailItem) return spEmailItem; //Email might still be in the Outbox so look there Outlook::MAPIFolderPtr spOutbox; hr = spNameSpace->raw_GetDefaultFolder(Outlook::olFolderOutbox, &spOutbox); if(FAILED(hr)) throw Workshare::Com::ComException(_T("Failed to get Outbox folder"), hr); if(spOutbox == 0) throw Workshare::NullReferenceException(_T("Failed to get Outbox folder")); spEmailItem = FindEmailInFolder(spOutbox, subject); if(0 != spEmailItem) return spEmailItem; //Email might have been sent while we were lounging around in the Outbox so go back to Sent Items spEmailItem = FindEmailInFolder(spSentItems, subject); if(0 != spEmailItem) return spEmailItem; std::tostringstream errMsg; errMsg << _T("Failed to find email in Sent Items with Subject = \"") << subject << _T("\"") << std::ends; throw Workshare::Com::ComException(errMsg.str().c_str(), MAPI_E_NOT_FOUND); }