利用pstsdk进行解析pst文件

mac2026-08-15  6

pstsdk下载地址:https://archive.codeplex.com/?p=pstsdk

下载后,我们只需要取出pstsdk目录,并自己进行dll封装,实现提取邮件内容、主题、收件人信息,把附件保存在一个目录下。利用VS2015进行调用该dll。

下载后,把新建一个pst目录,把头文件剪贴到原本的pstsdk目录外边,新建pst目录结构如下:

要编译pstsdk库,需要boost库,并且是1.42以上的版本,在VS2010中新建一个工程(测试过在VS10更高的版本建立工程,但是都编译失败了),在工程添加boost的目录。

pstParser.h如下:

#ifndef PSTPARSER_H__ #define PSTPARSER_H__ #include<algorithm> #include<functional> #include<locale.h> #include <string> #include<iostream> #include<Windows.h> //解析pst文件 //参数1为pst文件, //参数2保存收件人、主题、内容的文件 //参数3为一个目录,附件如果为文件,则保存在该目录下 extern "C" _declspec(dllexport) void PstParser(const WCHAR* pstFile,const WCHAR *contentFile,const WCHAR *TempDir); typedef void(_cdecl *PstParserDef)(const WCHAR* pstFile,const WCHAR *contentFile,const WCHAR *TempDir); #endif

pstParser.cpp如下:

#include "pstParser.h" #include <pst.h> #include<ltp.h> #include<util.h> using namespace std; using namespace std::placeholders; void process_message(const pstsdk::message& m,wstring &content,wstring &TempDir); void Wstring2String(wstring inStr,string &outStr) { int size = WideCharToMultiByte(CP_ACP , NULL, inStr.c_str(), inStr.length(), NULL, 0, NULL, false); char *buff=(char*)malloc(sizeof(char)*(size+1)); if (!buff) { return; } memset(buff,0,sizeof(char)*(size+1)); WideCharToMultiByte(CP_ACP , NULL, inStr.c_str(), -1,buff , size, NULL, false); buff[size]=0; outStr=buff; free(buff); } void String2Wstring(string inStr,wstring &outStr) { int size=MultiByteToWideChar(CP_ACP ,NULL,inStr.c_str(),inStr.length(),NULL,0); wchar_t *buff=(wchar_t*)malloc(sizeof(wchar_t)*(size+1)); if (!buff) { return; } memset(buff,0,sizeof(wchar_t)*(size+1)); MultiByteToWideChar(CP_ACP ,NULL,inStr.c_str(),inStr.length(),buff,size); buff[size]=0; outStr=buff; free(buff); } //提取附件 void process_attachment(const pstsdk::attachment& a,wstring &content,wstring &TempDir) { if(a.is_message()) { process_message(a.open_as_message(),content,TempDir); } else { std::wstring wfilename = a.get_filename(); wstring dir=TempDir; dir.append(L"\\"); dir.append(wfilename); ofstream newfile(dir.c_str(), ios::out | ios::binary); newfile << a; newfile.close(); } } //收件人信息 void process_recipient(const pstsdk::recipient& r,wstring &content) { std::wstring recName=r.get_name(); if (recName.size()) { content.append(recName); } } //提取邮件 void process_message(const pstsdk::message& m,wstring &content,wstring &TempDir) { try { wstring body=m.get_body(); //获得邮件内容 if (body.size()) { content.append(body); } wstring subject=m.get_subject(); //邮件主题 if (subject.size()) { content.append(subject); } if (m.get_attachment_count()>0) { for_each(m.attachment_begin(),m.attachment_end(),bind(process_attachment,_1,ref(content),ref(TempDir))); } if (m.get_recipient_count()>0) { for_each(m.recipient_begin(),m.recipient_end(),bind(process_recipient,_1,ref(content))); } } catch(pstsdk::key_not_found<pstsdk::prop_id>&) { } } //遍历邮件目录 void process_folder(const pstsdk::folder& f,wstring &content,wstring &TempDir) { for_each(f.message_begin(), f.message_end(), bind(process_message, _1,ref(content),ref(TempDir))); for_each(f.sub_folder_begin(), f.sub_folder_end(), bind(process_folder, _1,ref(content),ref(TempDir))); } extern "C" _declspec(dllexport) void PstParser(const WCHAR* pstFile,const WCHAR *contentFile,const WCHAR *TempDir) { wstring wTempDir; wstring wdat; try { pstsdk::pst f(pstFile); process_folder(f.open_root_folder(),wdat,wTempDir); string dat; Wstring2String(wdat,dat); wstring filePath=TempDir; filePath.append(L"\\"); filePath.append(contentFile); ofstream cFile(filePath.c_str()); if (!cFile.is_open()) { return; } cFile<<dat; cFile.close(); } catch(...) { return; } }

 

最新回复(0)