最近在研究邮件各种邮件客户端的邮件读取。(此文任然有一些细节问题。要想得到更精确的邮件读取可以mail给我:Bisny@qq.com)
刚开始采用了对邮件的分析,用正则直接去匹配出数据。但是老是出岔子。而且很不稳定。最近终于找到了稳定的方法了。
采用微软官方的动态链接库解决这个问题。就显得很方便了。
首先我们建立一个MFC Dll 项目。。然后在头文件中加入:
#import <cdosys.dll> no_namespace raw_interfaces_only
#import “C:\Program Files\Common Files\System\ado\msado15.dll” no_namespace raw_interfaces_only
这是我电脑中的DLl路径。各个版本的操作系统dll的路径可能不一样。
最好在C盘搜索一下。
然后编译。会报错起冲突。
说某些枚举已经声明了。这时候打开头文件stdafx.h。
把以下注释掉。在不影响其它功能的情况下。
//#include <afxext.h> // MFC 扩展
//
//
//#ifndef _AFX_NO_OLE_SUPPORT
//#include <afxole.h> // MFC OLE 类
//#include <afxodlgs.h> // MFC OLE 对话框类
//#include <afxdisp.h> // MFC 自动化类
//#endif // _AFX_NO_OLE_SUPPORT
//
//#ifndef _AFX_NO_DB_SUPPORT
//#include <afxdb.h> // MFC ODBC 数据库类
//#endif // _AFX_NO_DB_SUPPORT
//
//#ifndef _AFX_NO_DAO_SUPPORT
//#include <afxdao.h> // MFC DAO 数据库类
//#endif // _AFX_NO_DAO_SUPPORT
//
//#ifndef _AFX_NO_OLE_SUPPORT
//#include <afxdtctl.h> // MFC 对 Internet Explorer 4 公共控件的支持
//#endif
//#ifndef _AFX_NO_AFXCMN_SUPPORT
//#include <afxcmn.h> // MFC 对 Windows 公共控件的支持
//#endif // _AFX_NO_AFXCMN_SUPPORT
在编译。会产生两个.tlh类型的头文件。cdosys.tlh和msado15.tlh。
然后添加函数:
IMessagePtr Load_Message_from_File(_bstr_t path)
{_StreamPtr pStm(__uuidof(Stream));
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
try {
pStm->Open(
//pStm->raw_Open(
varOptional,
adModeUnknown,
adOpenStreamUnspecified,
NULL,
NULL);
pStm->LoadFromFile(path);
}
catch(_com_error e)
{
throw e;
}IMessagePtr iMsg(__uuidof(Message));
IDataSourcePtr iDsrc;
iDsrc = iMsg;try {
iDsrc->OpenObject(pStm,_bstr_t(“_Stream”));
}
catch(_com_error e)
{
throw e;
}
return iMsg;
}
然后在需要引用的地方添加以下代码:
IMessagePtr imsgptr=Load_Message_from_File(Pace.GetBuffer());
BSTR bsfrom;
DATE OD;imsgptr->get_From(&bsfrom);//获得发件人
_bstr_t bfrom(bsfrom);
CString Cfrom=bfrom;//赋给CString
imsgptr->get_To(&bsfrom);//获得收件人_bstr_t bto(bsfrom);
CString Cto=bto;//赋给CStringimsgptr->get_Subject(&bsfrom);
_bstr_t Bsub(bsfrom);//获得邮件标题
CString CSub=Bsub;//赋给CStringimsgptr->get_TextBody(&bsfrom);
_bstr_t Btb(bsfrom);//获得邮件正文
CString Ctb=Btb;//赋给CStringimsgptr->get_ReceivedTime(&OD);//获得收发时间
_variant_t GetMaliTime;
GetMaliTime.ChangeType(VT_DATE);
GetMaliTime=OD;
_bstr_t Btime(GetMaliTime);
CString Cdate=Btime;//赋值给CString
嗯..然后文件就算读取完成了。。
好多邮件客户端都不会直接采用EML格式的文件。。只要找到数据文件。就会发现好多EML格式的信息储存在数据文件里面。只需要把这些信息一条一条的读取出来。就能完成邮件的读取。。我的博客欢迎关注:http://www.2swr.com/