none
Trying to retrieve attachment from the mail item failing using Outlook Object Model on MFC C++ RRS feed

  • Question

  • Hi,

    I'm trying to retrieve Attachments of a email being sent. I get the error "0x80010100 System call failed" Or i get the error "Exception encountered". It fails when i use  the PropertyAccessor to get the "PR_ATTACH_DATA_BIN".  It seems to work for office 2010 documents but not for 2007 office documents. Any help would be appriciated.

    Here is the snippet of my code that gets the attachments 

    hr=OLEMethod(DISPATCH_PROPERTYGET, &result, pMail, L"Subject", 0);
    		if (FAILED(hr)) return FALSE;
    		subject = result.bstrVal;
    
    		IDispatch *attchments;
    		hr=OLEMethod(DISPATCH_PROPERTYGET, &result, pMail, L"Attachments", 0);
    		if (FAILED(hr)) return FALSE;
    		attchments = result.pdispVal;
    
    		hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchments, L"Count", 0);
    		if (FAILED(hr)) return FALSE;
    		int numattch = result.intVal;
    
    		for (int i=1; i<=numattch; i++)
    		{
    			FilePostStruct filePost;
    
    			VARIANT varCount;
    			varCount.vt = VT_I4;
    			varCount.llVal=i;
    			IDispatch *attchment;
    			hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchments, L"Item", 1, varCount);
    			if (FAILED(hr)) return FALSE;
    			attchment = result.pdispVal;
    
    			hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchment, L"FileName", 0);
    			if (FAILED(hr)) return FALSE;
    			CString fName = result.bstrVal;
    
    			hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchment, L"Size", 0);
    			if (FAILED(hr)) return FALSE;
    			int dwAttachSize = result.intVal;
    
    			IDispatch *propAssessor;
    			hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchment, L"PropertyAccessor", 0);
    			if (FAILED(hr)) return FALSE;
    			static BYTE parms[] = VTS_BSTR ;
    			propAssessor = result.pdispVal;
    			const CString PR_ATTACH_DATA_BIN = L"http://schemas.microsoft.com/mapi/proptag/0x37010102";
    
    			COleVariant objVariant(PR_ATTACH_DATA_BIN);
    			// FAILS in THIS LIINE!!!!!!!!!!!!
    			hr =OLEMethod(DISPATCH_METHOD, &result, propAssessor, L"GetProperty",1,objVariant.Detach() );// FAILS in THIS LIINE
    			//_____FAAAAAAAAAAAAAAAAAAAIIIIIIILLLLLLS
    			//return result;
    			//	CPropertyAccessor propaccess = propAssessor;
    			if (FAILED(hr)) return FALSE;
    
    			VARIANT vdata =result;
    			UCHAR *pbuff = NULL;
    			pbuff = (UCHAR*) vdata.parray[0].pvData;
    			//std::string data(reinterpret_cast<char const*> (pbuff),dwAttachSize);
    			
    			filePost.filename.assign(fName);
    			filePost.data.assign(pbuff,pbuff+dwAttachSize);
    			std::string filename = CStringA(fName);
    			
    			filePost.id = GetDocumentID(filename);
    			filePost.URL = L"Outlook - Exchange";
    			email.attachments.push_back(filePost.id);
    			EMAIL_RESULT::EMAIL_RESULT res;
    			Engine::getInstance().AnalyzeEmailDocument(CstrUserName,&filePost,res);
    
    			if (attchment) attchment->Release();
    		}
    		//	TCHAR *cTo = new TCHAR(to.StringLength);
    		if (attchments) attchments->Release();
    Thanks

    Monday, March 31, 2014 6:35 AM

Answers

All replies

  • PropertyAccessor cannot be used to open large string or binary properties, such as PR_ATTACH_DATA_BIN.

    You can either use Extended MAPI (IAttach::OpenProperty(PR_ATTACH_DATA_BIN, IID_IStream, ...)) or Redemption to retrieve PR_ATTACH_DATA_BIN.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!




    Monday, March 31, 2014 7:01 AM
  • Hi Dimitry,

    Thanks for the reply. Is there any workaround for this using OOM? 

    Monday, March 31, 2014 8:09 AM
  • Hi Dimitry,

    I am trying your method But i can't seem to get the Iattach interface

    hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchment, L"FileName", 0);
    			if (FAILED(hr)) return FALSE;
    			CString fName = result.bstrVal;
    
    			hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchment, L"Size", 0);
    			if (FAILED(hr)) return FALSE;
    			int dwAttachSize = result.intVal;
    			
    			hr=OLEMethod(DISPATCH_PROPERTYGET, &result, attchment, L"MAPIOBJECT", 0);
    			if (FAILED(hr)) return FALSE;
    
    			IAttach *att=NULL;
    
    			hr= result.pdispVal->QueryInterface(IID_Iattach1,(void**)&att); //FAILS HERE - returns "E-FAIL"
    Note* i have already called "MAPIInitialize"

    Please help. Thnaks


    Monday, March 31, 2014 8:47 AM
  • Hi Vidur,

    The Outlook object model doesn't provide anything for opening attachments on the fly at runtime. If you want to use OOM only, you need to Save the attachment on disk and then read it from there. The PropertyAccessor class has a number of well-known limitations. For example, big properties (more than 4 or 16 Kb) are not supported. Read more in the Don't stumble over a stone working with the PropertyAccessor and StorageItem classes in Outlook 2007 article.

    • Marked as answer by Vidur Sharma Friday, April 4, 2014 2:54 AM
    Monday, March 31, 2014 9:30 AM
  • Hello Vidur,

    Please take a look at the Opening an Attachment page in MSDN. It provides all the required steps explained in depth.

    Monday, March 31, 2014 9:33 AM
  • Is IID_Iattach1 correctly defined? Are you using multiple threads?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, March 31, 2014 1:57 PM
  • Hi Dmitry,

    Yeah its defined correctly  "DEFINE_OLEGUID(IID_Iattach1, 0x00020308, 0, 0);"

    And yes i am using Multiple threads

    Tuesday, April 1, 2014 2:19 AM
  • Hi Vidur,

    Did you initialize the MAPI subsystem for working correctly in another threads?

    Please refer to the MAPI Multithreading Rules article for more information.

    Tuesday, April 1, 2014 12:28 PM