In the previous article, we talked about how to set an attachment for a message. Next we will continue to talk about the attachment. Using the previous example, we will take a look at how to obtain the attachment information for a message. The following are two help functions: bool mapihelp_saveattachfile (lpattach pattach, lpctstr szfile): Read the content of a single attachment file and save it to the specified position pattach: attachment object szfile: save the file name bool mapihelp_getattachment (iMessage * PMSG, lpctstr szfilepath). function: obtain all the attachments of a message and save it to the specified directory PMSG: Target message object szfilepath: the specific implementation is as follows: bool mapihelp_saveattachfile (lpattach pattach, lpctstr szfile) {If (null = pattach | null = szfile) return false; handle hfil E = invalid_handle_value; istream * pstmattachment = NULL; char * pbuffer = NULL; int I = 0; DWORD dwwrite = 0; bool Bret = false; ulong ulread = 0; // open the attachment and obtain the istream object, which is used to obtain the file content. According to msdn, only the pr_attach_data_bin attribute is supported here. If (failed (pattach-> openproperty (pr_attach_data_bin, null, stgm_read, mapi_modify, reinterpret_cast <iunknown **> (& pstmattachment) {goto exit ;} // create the target file hfile =: createfile (szfile, generic_write, 0, null, open_always, file_attribute_normal, null); If (invalid_handle_value = hfile) {goto exit ;} // buffer for file copy pbuffer = new char [4096]; If (null = pbuffer) {goto exit;} // copy the attachment content while (succeed Ed (pstmattachment-> Read (pbuffer, 4096, & ulread) {If (ulread <= 0) break;: writefile (hfile, pbuffer, ulread, & dwwrite, null);} BRET = true; Exit: If (invalid_handle_value! = Hfile) {: closehandle (hfile);} If (null! = Pbuffer) {Delete [] pbuffer;} If (null! = Pstmattachment) {pstmattachment-> release ();} return Bret;} bool mapihelp_getattachment (iMessage * PMSG, lpctstr szfilepath) {If (null = PMSG | null = szfilepath) return false; lpmapitable pattachtbl = NULL; srowset * psrs = NULL; lpattach pattach = NULL; long lattachnum = 0; bool Bret = false; // obtain the attachment list if (failed (PMSG-> getattachmenttable (0, & pattachtbl) {goto exit;} // are you familiar with the subsequent query process? While (succeeded (pattachtbl-> queryrows (1, 0, & psrs) {// even if the query returns a successful result, the number of records may be 0, you need to exclude this case if (null = psrs | psrs-> crows! = 1) {break;} tchar szfile [max_path]; // traverse all attributes to find the attachment ID and name for (INT I = 0; I <(INT) (psrs-> Arow [0]. cvalues); ++ I) {If (pr_attach_num = psrs-> Arow [0]. lpprops [I]. ulproptag) {// locate the attachment ID and open the attachment object if (failed (PMSG-> openattach (psrs-> Arow [0]. lpprops [I]. value. l, null, mapi_best_access, & pattach) {goto exit;} lattachnum = psrs-> Arow [0]. lpprops [I]. value. l;} else if (pr_attach_filename = psrs-> Arow [0]. lpprop S [I]. ulproptag) {// obtain the attachment name and generate the Save path _ stprintf (szfile, _ T ("% S % s"), szfilepath, psrs-> Arow [0]. lpprops [I]. value. lpszw) ;}}if (pattach) {// Save the mapihelp_saveattachfile (pattach, szfile); pattach-> release (); pattach = NULL;} freeprows (psrs ); psrs = NULL;} BRET = true; Exit: If (null! = Psrs) {freeprows (psrs);} If (null! = Pattach) {pattach-> release ();} If (null! = Pattachtbl) {pattachtbl-> release ();} return Bret;} It is very easy to call the external call. You only need to obtain the iMessage object and then call mapihelp_getattachment. Now we have finished talking about the content of the attachment. We welcome you to make a brick. Bored customer (blog.csdn.net/yzx0023) yzx0023@gmail.com 2006-7-26