⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faxmail2.cpp

📁 自动传真系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include "stdafx.h"
#include "FaxSrv.h"

#include "FaxSrvDoc.h"
#include "FaxSrvView.h"
#include "faxmail.h"


#include "faxsend.h"
#include "faxrecv.h"
#include "faxerror.h"
#include "faxuser.h"
#include "faxbill.h"
#include "paide.h"

#include "debug\msado26.tlh"
#include "debug\cdosys.tlh"
extern CString MailPath;
extern CString FaxDocPath;
extern CString FaxMail;

//extern CString m_dsn,m_uid,m_pwd;
extern CString m_DSN;
/////////////////////////邮件处理线程函数
//////////////////////
////////////////
// CFaxSrvView message handlers
//#import "D:\Program Files\Microsoft Office\OFFICE11\msoutl.olb" no_namespace    raw_interfaces_only
#include "msoutl.tlh"

UINT __cdecl MyFaxRecvByIp( LPVOID pParam )
{
	CFaxSrvView * pView=(CFaxSrvView*)pParam;

	WIN32_FIND_DATA mailinfind;
	HANDLE hFind;

////////////////smtp接口指针
	IDropDirectoryPtr pDropDir;
	IMessagesPtr pMsgs ;
    IUnknown* pUnk ;
    IEnumVARIANTPtr pEnum ;
////////////////////
	_MailItemPtr  pMail;
	_ApplicationPtr pApp;
	_NameSpacePtr pNameSpace;
	RecipientPtr  pRecipenr;
	_FoldersPtr   pFolders;
	MAPIFolderPtr pMAPI;
	_ItemsPtr  pItems;
	AttachmentsPtr  pAtts;
	AttachmentPtr pAtt;


	CoInitialize(NULL);

    pApp.CreateInstance("Outlook.Application");
	BSTR bsType=SysAllocString(L"MAPI");
	pApp->GetNamespace (
        bsType,
        &pNameSpace );
	pNameSpace->GetDefaultFolder(olFolderInbox,&pMAPI);
	pMAPI->get_Items(&pItems);
	pItems->GetFirst((IDispatch **)&pMail);
	BSTR  bsSub;
//	pMail->Display();
	pMail->get_To(&bsSub);

	_bstr_t bsSub2(bsSub);
	AfxMessageBox((char*)(bsSub2));



	IConfigurationPtr pConf;
	pConf.CreateInstance(L"CDO.Configuration");
	pConf->Load(cdoOutlookExpress,_bstr_t(""));
	FieldsPtr  pf1;	
	pConf->get_Fields(&(pf1));
	FieldPtr pf2;
	pf1->get_Item(_variant_t(cdoSendUsingMethod),&pf2);
	pf2->put_Value(_variant_t((long)cdoSendUsingPort));
	//	 pf1->get_Item(_variant_t(cdoSendUsingMethod),&pf2);
	//	 pf2->get_Value();
	pf1->Update();

	CoCreateInstance(
			__uuidof(DropDirectory),
			NULL,
			CLSCTX_SERVER,
			__uuidof(IDropDirectory),
			(void**)&pDropDir);

	CDatabase db;
	db.OpenEx(m_DSN);//"DSN=faxdata"UID=sa;PWD=;
	CFaxSend faxSend(&db);
 	CFaxBill faxBill(&db);
 	CFaxRecv faxRecv(&db);
 	CFaxError faxError(&db);
 	CFaxUser faxUser(&db);
///////////////////
//////
	BOOL bDBException=false;
    while(1)
	{
		try
		{

			if(bDBException) 
			{
				db.Close();
				db.Open(m_DSN);
				bDBException=false;
			}

			Sleep(1100);
		
			///////////修改邮件扩展名为.eml
			hFind=FindFirstFile(MailPath+"*.in",&mailinfind);
			if(hFind!=INVALID_HANDLE_VALUE)
			{
				do
				{
					CString fi=mailinfind.cFileName;
					int index1=fi.Find(".",0);
					if(index1!=-1)
						fi=fi.Left(index1);
					MoveFile(MailPath+mailinfind.cFileName,MailPath+fi+".eml");
				}while(FindNextFile(hFind,&mailinfind));
				FindClose(hFind);

			}
			///////////////打开DropDir对象,获取接收的邮件,提取内容,分离FAX文件

			pDropDir->GetMessages(_bstr_t(MailPath),&pMsgs);
			long count = 0;
			pMsgs->get_Count(&count);  
			for(int iMsg=1;iMsg<=count;iMsg++) 
			{
			//				ULONG cFetched = 0;
				VARIANT varMsg;
				VariantInit(&varMsg);
				// 
				IMessage* pMsg = NULL;
				pMsgs->get_Item(iMsg,&pMsg);

				IBodyPartsPtr bodys1;
				IBodyPartPtr   body1;
				pMsg->get_Attachments(&bodys1);
				long iBody;
				bodys1->get_Count(&iBody);
				
				BSTR pFrom,pTo;
				pMsg->get_From(&pFrom);
				_bstr_t sub("");
				
				BSTR subject;  ///提取电话号码,主题
				pMsg->get_Subject(&subject);
				sub=subject;
				CString PhoneTo=(char*)(sub);
				BOOL bPhoneTo=false;///号码空
				for(int iii=0;iii<PhoneTo.GetLength();iii++)
				{
					char cccc;
					cccc=PhoneTo[iii];
					bPhoneTo=(cccc==','||(cccc>='0'&&cccc<='9'));
					if(!bPhoneTo)break;
				}

				sub=pFrom;
				CString strtemp1;
					strtemp1=(char*)(sub);
			

				if(iBody==0)
				{	
					//	pMsg->get_To(&pTo);	
					pMsg->put_To(pFrom);
					pMsg->put_From(NULL);
					pMsg->putref_Configuration (pConf);
					
					sub="传真发送失败:未见传真附件";
					pMsg->put_Subject(sub.operator wchar_t *());
					pMsg->Send();
					pMsg->Release();
					pMsgs->Delete(iMsg);

					sub=pFrom;
				//	AfxMessageBox(sub.operator const char *());
					continue;
				}   
				else if(!bPhoneTo)
				{
					pMsg->put_To(pFrom);
					pMsg->put_From(NULL);
					pMsg->putref_Configuration (pConf);					
					sub="传真发送失败:电话号码(";
					sub+=LPCTSTR(PhoneTo);
					sub+=")错误";
					pMsg->put_Subject(sub.operator wchar_t *());
					pMsg->Send();
					pMsg->Release();
					pMsgs->Delete(iMsg);

					sub=pFrom;
				//	AfxMessageBox(sub.operator const char *());
					continue;
				}
				else if(strtemp1.Find(FaxMail)>=0) ///////注意改掉
				{/////如果自己发送的邮件或没有传真附件,直接删除
					pMsg->Release();
					pMsgs->Delete(iMsg);
					continue;
				}

				for(int i=0;i<iBody;i++)
				{
					bodys1->get_Item(i+1,&body1);
					BSTR bodyname;
					body1->get_FileName(&bodyname);
					_bstr_t bodyname2(bodyname);
					CString bodyname3=(char*)(bodyname2);

					////////////检索可以输出的文件类型
					bodyname3.MakeLower();
					BOOL bDOC=bodyname3.Find(".doc")==bodyname3.GetLength()-4;
					BOOL bXLS=bodyname3.Find(".xls")==bodyname3.GetLength()-4;
					BOOL bGIF=bodyname3.Find(".gif")==bodyname3.GetLength()-4;
					BOOL bJPG=bodyname3.Find(".jpg")==bodyname3.GetLength()-4;
					BOOL bBMP=bodyname3.Find(".bmp")==bodyname3.GetLength()-4;
					BOOL bBFX=bodyname3.Find(".bfx")==bodyname3.GetLength()-4;
					BOOL bTIF=bodyname3.Find(".tif")==bodyname3.GetLength()-4;
					if(bDOC||bXLS||bGIF||bBMP||bJPG||bBFX||bTIF)
					{
						//////保存传真附件并登记数据库索引信息
						///////////查找from字段中是否包含usename\usermail
						faxUser.m_strFilter.Format("PATINDEX('%%'+usermail+'%%' ,'%s')<>0",
							sub.operator char *());
						//	"usermail='%s' or username='%s'",sub,
						if(faxUser.IsOpen())
							faxUser.Requery();
						else 
							faxUser.Open();
						if(!faxUser.IsEOF())
						{

							CTime t1=time(NULL);
							CString path1;

							path1.Format("%s\\Send\\%sS%04d%02d%02d%d%d%d",
									 FaxDocPath,faxUser.m_userid,
									t1.GetYear(),t1.GetMonth(),t1.GetDay(),
									t1.GetHour(),t1.GetMinute(),t1.GetSecond());
							CString xn="";

							if(bDOC) xn=".doc";
							else if (bXLS) xn=".xls";
							else if (bGIF) xn=".gif";
							else if (bJPG) xn=".jpg";
							else if (bBMP) xn=".bmp";
							else if (bTIF) xn=".tif";
							else if (bBFX) xn=".bfx";


							body1->SaveToFile(_bstr_t(path1+xn));//bodyname3

	///////////////////
	////////调用虚拟打印进行转换
							if(!bBFX)  ///如果是bfx格式,不用转换
							{
								fnSetPrintFile((char*)(LPCTSTR(path1+".bfx")));	

								//调用外壳程序打印
								int  ret=(int)ShellExecute(pView->m_hWnd,"print",path1+xn, NULL, NULL, SW_HIDE);
								////ret <=32 系统错误
							//	ret=GetLastError();  
								CTime T1,T2;
								T1=time(NULL);
								while(ret>32&&fnIsPrintOver()!=1)  //等待打印结束
								{

									T2=time(NULL);
									int lll=(T2-T1).GetTotalSeconds();
									if(lll>40)
										 break;
								    Sleep(100);

								}
							}
								WIN32_FIND_DATA find2;
								HANDLE hFind2;
							hFind2=FindFirstFile(path1+".bfx",&find2);
							if(hFind2!=INVALID_HANDLE_VALUE)
							{
								//////////////////
								FindClose(hFind2);
								DWORD len=0;
								CFile file1;//如果传真文件巨大,可能是文件转换错误,建议用户分开发送
								if(file1.Open(path1+"bfx",CFile::modeRead))
								{
									len=file1.GetLength();
									file1.Close();
								}
								else;

								if(bBFX||len<2000000)
								{
									if(faxSend.IsOpen())
										faxSend.Requery();							
									else
										faxSend.Open();
								
									faxSend.AddNew();
										faxSend.m_userid=faxUser.m_userid;
										faxSend.m_faxdoc=(path1+".bfx");//bodyname3
										faxSend.m_faxto=LPCTSTR(PhoneTo);;
										faxSend.m_recvtime=time(NULL);
										faxSend.m_sendtime=time(NULL);
										faxSend.m_state="0";
										faxSend.m_callcount=0;
										faxSend.m_errorid="ok";
									faxSend.Update();
								//	pMsg->Release();
								//	pMsgs->Delete(iMsg);
								}
								else
								{
									DeleteFile(path1+".bfx");
									BSTR pFrom,pTo;
									pMsg->get_From(&pFrom);
								//	pMsg->get_To(&pTo);
									_bstr_t sub("");							

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -