Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / ATL

OeLibrary 1.0

Rate me:
Please Sign up or sign in to vote.
4.91/5 (19 votes)
22 Aug 2013GPL33 min read 133.8K   2.5K   56  
Opensource Outlook Express automation library
// Folder.cpp : Implementation of CFolder
#include "stdafx.h"
#include "OELib.h"
#include "Folder.h"


CFolder *m_pClass=NULL;
//UINT nWindowMessage=::RegisterWindowMessage("THEOEAPPLICATIONHANDLEFORHANDLLING");
///Window Procedure for handling the Window
//                         LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

/////////////////////////////////////////////////////////////////////////////
// CMyHiddenWnd

//The Function handlling the OnCLose Message

/*********************************************************************
  Event  Name:OnClose(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  Last Modified:19 oct 2004
  Function Descrption: Closes the currently open window
  Interafce Support =NO

 *********************************************************************/


LRESULT CMyHiddenWnd::OnClose(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
	{
        /*Error Handlling Measure*/
        if(bHandled){}
		msg=lParam=wParam=NULL;
		/*Error handling Measure End*/

		PostQuitMessage(0);
		return 0;

	}


/*********************************************************************
  Event  Name:OnNewMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  Last Modified:19 oct 2004
  Function Descrption:Fire The event for NewMsg
  Interafce Support =NO

 *********************************************************************/
  
  LRESULT CMyHiddenWnd::OnNewMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
	{
	  /*Error Handlling Measure*/
        if(bHandled){}
		msg=lParam=NULL;
		/*Error handling Measure End*/


	   m_pClass->Fire_NewMsg((long)wParam);
	 // CString str;
	 // str.Format("%d %d",wParam,lParam);
	 // ::MessageBox(NULL,str,"Helloo",MB_OK); 
	  
//	  m_pClass->Fire_NewMsg();
	  /*		  
	  m_pClass->Fire_NewMsg();
	  ::PostMessage(HWND_BROADCAST,nWindowMessage,0,0);*/
	  
	  return 0;

	}

  
/*********************************************************************
  Event  Name:OnReadMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  Last Modified:19 oct 2004
  Function Descrption: FireEvent,if any message is marked read
  Interafce Support =NO
 *********************************************************************/

  LRESULT CMyHiddenWnd::OnReadMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  {
	  /*Error Handlling Measure*/
        if(bHandled){}
		msg=lParam=NULL;
		/*Error handling Measure End*/

	  /*CString str;
	  str.Format("%d %d",wParam,lParam);
	  ::MessageBox(NULL,str,"Helloo",MB_OK); */
	  m_pClass->Fire_MarkAsRead((long)wParam);
	//  m_pClass->Fire_NewMsg();
	  /*		  
	  m_pClass->Fire_NewMsg();
	  ::PostMessage(HWND_BROADCAST,nWindowMessage,0,0);*/
	  
	  return 0;
	  
  }
  
/*********************************************************************
  Event  Name:OnReadMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  Last Modified:19 oct 2004
  Function Descrption: FireEvent,if any message is marked unread
  Interafce Support =NO
 *********************************************************************/

  LRESULT CMyHiddenWnd::OnUnReadMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  {
	  /*Error Handlling Measure*/
        if(bHandled){}
		msg=lParam=NULL;
		/*Error handling Measure End*/

	  /*CString str;
	  str.Format("%d %d",wParam,lParam);
	  ::MessageBox(NULL,str,"Helloo",MB_OK); */
	  m_pClass->Fire_MarkAsUnRead((long)wParam);
	  //m_pClass->Fire_NewMsg();
	  /*		  
	  m_pClass->Fire_NewMsg();
	  ::PostMessage(HWND_BROADCAST,nWindowMessage,0,0);*/
	  
	  return 0;
	  
  }
  
 /*********************************************************************
  Event  Name:OnReadMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  Last Modified:19 oct 2004
  Function Descrption: FireEvent,if any message is deleted
  Interafce Support =NO
 *********************************************************************/

  LRESULT CMyHiddenWnd::OnDeleteMsg(UINT msg,WPARAM wParam,LPARAM lParam,BOOL &bHandled)
  {
	  /*Error Handlling Measure*/
        if(bHandled){}
		msg=lParam=NULL;
		/*Error handling Measure End*/

	  /*CString str;
	  str.Format("%d %d",wParam,lParam);
	  ::MessageBox(NULL,str,"Helloo",MB_OK); */
	  m_pClass->Fire_DeleteMsg((long)wParam);
	  //m_pClass->Fire_NewMsg();
	  /*		  
	  m_pClass->Fire_NewMsg();
	  ::PostMessage(HWND_BROADCAST,nWindowMessage,0,0);*/
	  
	  return 0;

  }
/////////////////////////////////////////////////////////////////////////////
// CFolder

STDMETHODIMP CFolder::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IMailFolder
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

void CFolder::InitializeFolder()
{
	HRESULT hr;
	
	//======================
	//saving Function from future recall
	//======================
	
	this->m_bInitializationCalled=TRUE;
	
	
	//=========================
	////--->IF the function is Speacial
	///========================
	if(this->m_bIsSpecial)
	{
		////MessageBox(NULL,"Yahoo i got the Props1",tmp,MB_OK);
		//the FolderID of Special Folder if is SpecialFOlder +4
		hr=this->pStoreNamespace->OpenSpecialFolder((SPECIALFOLDER)(int)(this->FolderID-4),0,&this->pStoreFolder);
	}
	else
	{
		//Open Local Folder
		////MessageBox(NULL,"Yahoo i got the Props2",tmp,MB_OK);
		hr=this->pStoreNamespace->OpenFolder(this->FolderID,0,&this->pStoreFolder);
	}



}




STDMETHODIMP CFolder::get_FOLDERID(long *pVal)
{

	//======================================
	//put the folder id into particular folder
	//======================================
	*pVal=this->FolderID;

	return S_OK;
}

STDMETHODIMP CFolder::get_IsSpecial(VARIANT_BOOL *pVal)
{
	//================
	//Return Speacial Folder status
	//================

	this->m_bIsSpecial?*pVal=VARIANT_TRUE:*pVal=VARIANT_FALSE;

	return S_OK;
}


STDMETHODIMP CFolder::get_SubFolderCount(short *pVal)
{
	
	//===========================
	//Declaring some useful variable
	//===========================
	HRESULT hr=S_FALSE;
	FOLDERPROPS prop;
	 
	prop.cbSize=sizeof(prop);

	HENUMSTORE__ *phLocalEnum=NULL;
	this->m_nFolderCount=0;

	
	//============================
	//now Enumerate All the folders
	//===========================
	if(this->pStoreNamespace)
	{

		hr=this->pStoreNamespace->GetFirstSubFolder(this->FolderID,&prop,&phLocalEnum);
		//==============
		//Increment Our Counter
		//===============
		this->m_nFolderCount++;

		while(hr==S_OK)
		{
			hr=this->pStoreNamespace->GetNextSubFolder(phLocalEnum,&prop);
			if(SUCCEEDED(hr))
			{
				this->m_nFolderCount++;
			}
			else
			{
				this->pStoreNamespace->GetSubFolderClose(phLocalEnum);
			}
			
		}

	}


	 
	//copy the total count and send it back 
	*pVal=--this->m_nFolderCount;
	return S_OK;

}

STDMETHODIMP CFolder::get_Name(BSTR *pVal)
{
	
	//==========================
	//this function return the name of folder
	//==========================
	
  	FOLDERPROPS prop;
	prop.cbSize=sizeof(prop);
	HRESULT hr=S_FALSE;

 	
	//================================
	//Initialize Content of this Folder
	//================================
	if(!this->m_bInitializationCalled)
		this->InitializeFolder ();

	
	if(this->pStoreFolder)
	{
		//==================
		//Get Local Folder Props
		//==================
		
		hr=this->pStoreFolder->GetFolderProps(0,&prop);
		
		if(SUCCEEDED(hr))
		{
#ifdef UNICODE
         *pVal=CComBSTR(prop.szName).Detach();
#else
			*pVal=T2BSTR(prop.szName);
#endif
			
			
		}
		else
		{
			CComBSTR tmp("");
			*pVal=tmp.Detach();

			return S_FALSE;
			
		}
	}
	  
	//::MessageBox(NULL,"1","3",MB_OK);
	return hr;
}

STDMETHODIMP CFolder::get_Messages(long *pVal)
{
	//================================
	//This Function Will return the Messgae count
	//================================
  	
	FOLDERPROPS prop;
	prop.cbSize=sizeof(prop);
	HRESULT hr;

 	if(!this->m_bInitializationCalled)
		this->InitializeFolder ();

	if(this->pStoreFolder)
	{
		
		hr=this->pStoreFolder->GetFolderProps(0,&prop);
		
		if(SUCCEEDED(hr))
		{
			
			*pVal=(short)prop.cMessage;
			return hr;
		}
	}
	
	*pVal=-1;
	
	return S_FALSE;
	
}

STDMETHODIMP CFolder::get_UnRead(long *pVal)
{
	FOLDERPROPS prop;
	prop.cbSize=sizeof(prop);
	HRESULT hr;
	if(!this->m_bInitializationCalled)
		this->InitializeFolder ();
	
	if(pStoreFolder)
	{
		hr=this->pStoreFolder->GetFolderProps(0,&prop);
		
		if(SUCCEEDED(hr))
		{
			*pVal=(short)prop.cUnread;
			return hr;
		}
		
	}
	
	//else return Error
	*pVal=-1;
	return S_FALSE;
}

STDMETHODIMP CFolder::SetNotification()
{
	//===================================
	///Now We going to setup notification event
	//====================================
	RECT rcPos={100,100,101,101};
	wnd.Create(NULL,rcPos,_T(""));
	wnd.ShowWindow(SW_HIDE);
	//=============================
	//setting class address to global data
	//=============================
	m_pClass=this;
	
	HRESULT hr=S_FALSE;
	
	if(!::IsWindow(this->wnd.m_hWnd))
	{
		if(!this->m_bInitializationCalled)
			this->InitializeFolder ();


		if(pStoreFolder)
			hr=pStoreFolder->RegisterNotification(0,this->wnd.m_hWnd);
		
			
			
	}

	if(FAILED(hr))
	{
		Error(::GetResStr(IDS_UNABLETOREGISTERNOTIFICATION));
		return hr;
	}

	return S_OK;

}



STDMETHODIMP CFolder::Add(BSTR newFolderName, IMailFolder **ppFolder)
{
	//=======================
	//Check for error Handling
	//========================
	*ppFolder=NULL;

	//============================
	//check is we really recived the string
	//============================
	if(::SysStringLen(newFolderName)==0)
		//=====================
		//if not,return failure
		//=====================
		return S_FALSE;


	//=======================================
	//Copy the New Foldername in local String
	//=======================================
	CString FolderName(newFolderName);
	//==========================
    //Declare Some Useful variable
	//===========================
	HRESULT hr=S_FALSE;
	STOREFOLDERID NewFolderID;
    //IMailFolder *pTmp=NULL;
	CComObject<CFolder>* pFolder=NULL;

	hr = CComObject<CFolder>::CreateInstance (&pFolder);
	
	


    //======================
	//Create New Folder Under current opened Folder
	//======================

	if(this->pStoreNamespace)
	{
	    hr=this->pStoreNamespace->CreateFolder(this->FolderID,FolderName,0,&NewFolderID);
		
		if(SUCCEEDED(hr))
		{
			//=============
			//Put FolderID
			//=============
			pFolder->FolderID=NewFolderID;
			
			//======================
			//Put Special Folder Type
			//======================
			pFolder->m_bIsSpecial=FALSE;
			
			
			//=======================
			//Put Serial Key Condition
			//=======================
			pFolder->m_bSerialKey=TRUE;
			
			//do Query Interface
			hr=pFolder->QueryInterface(__uuidof(IMailFolder),reinterpret_cast<LPVOID*>(ppFolder));
			
			
		}		
	}

	
	//===================
	//saving from error
	//===================
	if(FAILED(hr))
	   {
		   return S_FALSE;
	   }

	  //else Return Success
	return hr;

	
}

STDMETHODIMP CFolder::Delete()
{
	
	HRESULT hr=S_FALSE;
	


	if(this->pStoreNamespace)
	{
		//=======================
		//Order NameSpace for deleting the folder
		//========================

		hr=this->pStoreNamespace->DeleteFolder(this->FolderID,0);

		//=========================
		//Check if we are succeeded
		//=========================

		if(SUCCEEDED(hr))
		{
			this->FolderID=FOLDERID_ROOT;
			this->m_bIsSpecial=FALSE;
			
			return hr;
		}
		else
		{
			Error(GetResStr(IDS_SPECIALFOLDERACTION));
			return hr;
		}
	
	}


	return hr;
	
}

STDMETHODIMP CFolder::Move(IMailFolder *pParentFolder)
{

	//=====================
	//Parent folder ID
	//======================
	long parentFolderID;
	//=======================================================
	//Check wheather caller specifies the parent folder or not
	//=======================================================
	if(pParentFolder==NULL)
		return S_FALSE;

	pParentFolder->get_FOLDERID(&parentFolderID);

	//========================
	//our error handling Variable
	//=========================
	HRESULT hr=S_FALSE;


	if((pStoreNamespace)&&(pParentFolder))
	{
		hr=this->pStoreNamespace->MoveFolder(this->FolderID,parentFolderID,0);
		

		if(SUCCEEDED(hr))
		{
			//=========
			//success
			//===========
			return hr;
		}
		else
		{
			
			Error(GetResStr(IDS_SPECIALFOLDERACTION));
			//failure
			return hr;
		}
	}
	

	return S_OK;
}

STDMETHODIMP CFolder::Rename(BSTR bstNewName)
{
	
	//=====================================
	//this function will rename the folder
	//====================================
	HRESULT hr=S_FALSE;
	if(::SysStringLen(bstNewName)==0)
		return S_FALSE;

	//======================
   //Copy That in Local String
	//=======================
	CString FolderName(bstNewName);
	
	  if(this->pStoreNamespace)
	  {
		  if(SUCCEEDED(this->pStoreNamespace->RenameFolder(this->FolderID,0,FolderName)))
			  return S_OK;
		  else
		  {
			 Error(GetResStr(IDS_SPECIALFOLDERACTION));
			//failure
			return hr;
		  }

	  }
	

	return S_FALSE;

	
}


STDMETHODIMP CFolder::GetSpecialFolder(OESFOLDER sfFolder, IMailFolder **ppNewFolder)
{
	
	//=========================
	//Set HRESULT to S_FALSE
	//=========================
	HRESULT hr=S_FALSE;
	*ppNewFolder=NULL;
	

	 CComPtr<IStoreFolder> tmpFolder;
	//IMailFolder *pTmp=NULL;
	//Folder 		Class
	 CComObject<CFolder>* pFolder=NULL;


	 hr = CComObject<CFolder>::CreateInstance (&pFolder);
		
	 
	 FOLDERPROPS props;
	 //emptying the memory location
	 ZeroMemory(&props,sizeof(props));
	 //Allocating the size of memory to Props
     props.cbSize=sizeof(FOLDERPROPS);
	 
	 if(pStoreNamespace)
	 {
		 hr=this->pStoreNamespace->OpenSpecialFolder((SPECIALFOLDER)(int)sfFolder,0,&tmpFolder);//(SPECIALFOLDER)(int)sfFolder
 
		 if(SUCCEEDED(hr))
		 {
			 tmpFolder->GetFolderProps(0,&props);
			 
			 if(hr==S_OK)
			 {
				 
				 //Put FolderID
				 pFolder->FolderID=props.dwFolderId;
				 
				 //Put Special Folder Type
				 if(props.sfType==-1)
					 pFolder->m_bIsSpecial=FALSE;
				 else
					 pFolder->m_bIsSpecial=TRUE;
				 
				 //Put Serial Key Condition
				 pFolder->m_bSerialKey=TRUE;
				 
				 
				 hr=pFolder->QueryInterface(__uuidof(IMailFolder),reinterpret_cast<LPVOID*>(ppNewFolder));

			 
				 return S_OK;
			 }
			 else
			 {
				 *ppNewFolder=NULL;
				  return S_FALSE;
			 }
			 
		 }
	 }
	 
	 return hr;

	
}

STDMETHODIMP CFolder::GetFirstSubFolder(IMailFolder **ppNewFolder)
{
	HRESULT hr=S_FALSE;
	FOLDERPROPS prop;
	prop.cbSize=sizeof(prop);
    
	CComObject<CFolder>* pFolder=NULL;
	hr = CComObject<CFolder>::CreateInstance (&pFolder);

	*ppNewFolder=NULL;
	
	
	if(this->pStoreNamespace)
	{
	
		hr=this->pStoreNamespace->GetFirstSubFolder(this->FolderID,&prop,&this->phEnum);
		
		if(hr==S_OK)
		{
			//==============
			//Put FolderID
			//==============
			pFolder->FolderID=prop.dwFolderId;

			//===============
			//Put Special
			//================
			if(prop.sfType==-1)
				pFolder->m_bIsSpecial=FALSE;
			else
				pFolder->m_bIsSpecial=TRUE;

			pFolder->m_bSerialKey=TRUE;


            hr=pFolder->QueryInterface(__uuidof(IMailFolder),reinterpret_cast<LPVOID*>(ppNewFolder));

		
			
		}
		else
		{
			*ppNewFolder=NULL;
			return S_FALSE;
		}
		
	}

	return hr;


}

STDMETHODIMP CFolder::GetNextSubFolder(IMailFolder **ppNewFolder)
{
   HRESULT hr=S_FALSE;
   FOLDERPROPS prop;
   prop.cbSize=sizeof(prop);
   
   //Folder 		Class
   CComObject<CFolder>* pFolder=NULL;
   
   hr = CComObject<CFolder>::CreateInstance (&pFolder);
   *ppNewFolder=NULL;
		
	if(this->pStoreNamespace)
	{
		
		/*IMailFolder *pTmp=NULL;
		
		hr=::CoCreateInstance(__uuidof(Folder),NULL,CLSCTX_ALL,__uuidof(IMailFolder),reinterpret_cast<void**>(&pTmp));*/
	
		hr=this->pStoreNamespace->GetNextSubFolder(this->phEnum,&prop);
		
		if(hr==S_OK)
		{
			
			//Put FolderID
			pFolder->FolderID=prop.dwFolderId;
			
			//Put Special Folder Type
			if(prop.sfType==-1)
				pFolder->m_bIsSpecial=FALSE;
			else
				pFolder->m_bIsSpecial=TRUE;
			
			//Put Serial Key Condition
			pFolder->m_bSerialKey=TRUE;
			

             hr=pFolder->QueryInterface(__uuidof(IMailFolder),reinterpret_cast<LPVOID*>(ppNewFolder));
			 
			 
				//	*ret=1;
			 //pTmp->AddRef();
			 //pTmp->Release();
		}
		else
		{
			
			*ppNewFolder=NULL;
			
			//	*ret=0;
		
		}
			
		}

	return S_OK;
}

STDMETHODIMP CFolder::GetFolderByID(long lFolderID, IMailFolder **ppNewFolder)
{ 
	STOREFOLDERID NewFolderID=lFolderID;
	//Declare Some Useful variable
	//IMailFolder *pTmp=NULL;
	IStoreFolder *piStoreFolder=NULL;
	
	HRESULT hr=S_FALSE;
	
	//Folder 		Class
	CComObject<CFolder>* pFolder=NULL;
	hr = CComObject<CFolder>::CreateInstance (&pFolder);
	
	//FolderProps
	FOLDERPROPS prop;
	prop.cbSize=sizeof(prop);
	
	
	//error Saving Technique
	*ppNewFolder=NULL;

   //Check Whether we have Namespace Interface or not
   if(!this->pStoreNamespace)
	   return hr;

   //Open the particular folder
   hr=this->pStoreNamespace->OpenFolder(NewFolderID,0,&piStoreFolder);
   
      
   if(SUCCEEDED(hr))
   {
	   
	   this->pStoreNamespace->GetFolderProps(NewFolderID,0,&prop);
	   
	   //Put FolderID
	   pFolder->FolderID=prop.dwFolderId;
	   
	   //Put Special Folder Type
	   if(prop.sfType==-1)
		   pFolder->m_bIsSpecial=FALSE;
	   else
		   pFolder->m_bIsSpecial=TRUE;
	   
	   //Put Serial Key Condition
	   pFolder->m_bSerialKey=TRUE;
	   
	   
	   hr=pFolder->QueryInterface(__uuidof(IMailFolder),reinterpret_cast<LPVOID*>(ppNewFolder));
	   
	   
   }
   else
   {
	   *ppNewFolder=NULL;
   }

   
   //if we got an error
   if(FAILED(hr))
   {
	   return S_FALSE;
   }

	return hr;
}


DWORD CFolder::GetFolderIDbyName(LPCTSTR lpszFolderName, BOOL &bSpecial)
{
		CString FolderName(lpszFolderName);
    //	MessageBox(NULL,FolderName,"from com dll folder by id",MB_OK);
	

	//check wheather folder exists or not
	
	if(FolderName.CompareNoCase(_T("INBOX"))==0)
	{
		////MessageBox(NULL,FolderName,"from com dll",MB_OK);
		bSpecial=TRUE;
		return FOLDER_INBOX+4;
	}

	if(FolderName.CompareNoCase(_T("OUTBOX"))==0)
	{
		////MessageBox(NULL,FolderName,"from com dll",MB_OK);
		bSpecial=TRUE;
		return FOLDER_OUTBOX+4;
	}

	if(FolderName.CompareNoCase(_T("Deleted Items"))==0)
	{
		////MessageBox(NULL,FolderName,"from com dll",MB_OK);
		bSpecial=TRUE;
		return FOLDER_DELETED+4;
	}

	if(FolderName.CompareNoCase(_T("Sent Items"))==0)
	{
		////MessageBox(NULL,FolderName,"from com dll",MB_OK);
		bSpecial=TRUE;
		return FOLDER_SENT+4;
	}

	if(FolderName.CompareNoCase(_T("Drafts"))==0)
	{
		////MessageBox(NULL,FolderName,"from com dll",MB_OK);
		bSpecial=TRUE;
		return FOLDER_DRAFT+4;
	}

    //if it not speacial folder
	//then search for folder and check it is exist or not
	bSpecial=FALSE;

	//MessageBox(NULL,FolderName,"from com dll folder by id2",MB_OK);
    FOLDERPROPS *folderProp=new FOLDERPROPS;
	HENUMSTORE__ *LocalheNum;
	folderProp->cbSize=sizeof(FOLDERPROPS);
	HRESULT hr=S_FALSE;

	if(pStoreNamespace)
	{
		hr=pStoreNamespace->GetFirstSubFolder(this->FolderID,folderProp,&LocalheNum);
		
		CString err;
		err.Format(_T("%u"),hr);
		
	//	MessageBox(NULL,FolderName,err,MB_OK);
      
		if(folderProp->szName)
		{
				//MessageBox(NULL,folderProp->szName,"love oe",MB_OK);
			if(FolderName.CompareNoCase(folderProp->szName)==0)
			{
				//this->pStoreNamespace->GetSubFolderClose(LocalheNum);
			//	MessageBox(NULL,FolderName,"from com dll folder by id4",MB_OK);
				return folderProp->dwFolderId;
			}
		}
		
		if(SUCCEEDED(hr))
		{
			while(1)
			{
				hr=pStoreNamespace->GetNextSubFolder(LocalheNum,folderProp);
				
				if(hr==S_FALSE)
					break;
				
				if(FolderName.CompareNoCase(folderProp->szName)==0)
				{
					//this->pStoreNamespace->GetSubFolderClose(phEnum);
					//MessageBox(NULL,FolderName,"from com dll folder by id",MB_OK);
					return folderProp->dwFolderId;
				}
				
			}
		}
	}
	
	//MessageBox(NULL,FolderName,"if we find nothing in com dlll",MB_OK);

	return 10;

}

STDMETHODIMP CFolder::GetSubFolderbyName(BSTR FolderName, IMailFolder **ppNewFolder)
{

	//================
	//Error Saving Technique
	//================
	*ppNewFolder=NULL;
	
	if(::SysStringLen(FolderName)==0)
		return S_FALSE;
	
	HRESULT hr;
	
	//FolderClass
	CComObject<CFolder>* pFolder=NULL;
	hr = CComObject<CFolder>::CreateInstance (&pFolder);
	
	CString pszName(FolderName);
		
	STOREFOLDERID NewFolderID;
	BOOL bSpecial;
	
	//=============
	//Get FolderIS of Interface
	//=============
	NewFolderID=this->GetFolderIDbyName(pszName,bSpecial);

	 
	
	FOLDERPROPS props;
	//emptying the memory location
	ZeroMemory(&props,sizeof(props));
	//Allocating the size of memory to Props
	props.cbSize=sizeof(FOLDERPROPS);

	if((NewFolderID==10)||(!pStoreNamespace))
	{
		//==========================
		//The foldername is not valid
		//==========================
		return S_FALSE;
	}



	hr=this->pStoreNamespace->GetFolderProps(NewFolderID,0,&props);
         
	if(SUCCEEDED(hr))
	{
		//===============
		//Put FolderID
		//================
		pFolder->FolderID=props.dwFolderId;
		
		//======================
		//Put Special Folder Type
		//======================
		
		if(props.sfType==-1)
			pFolder->m_bIsSpecial=FALSE;
		else
			pFolder->m_bIsSpecial=TRUE;
		
		//========================
		//Put Serial Key Condition
		//========================
		pFolder->m_bSerialKey=TRUE;
		

		hr=pFolder->QueryInterface(__uuidof(IMailFolder),reinterpret_cast<LPVOID*>(ppNewFolder));
		
	}
	
	else
	{
		*ppNewFolder=NULL;
		hr=S_FALSE;
	}


	return hr;
}



BOOL CFolder::fnFillMessageData(CMailItem *pMessage, MESSAGEPROPS props)
{
	pMessage->m_dwMessageID=props.dwMessageId;
	pMessage->m_dwParentFolderID=this->FolderID;
	
	//Message Serial
	pMessage->m_bSerialKey=TRUE;
	
	//Message From
	CComBSTR tmp(props.pszDisplayFrom);
	pMessage->bstMessageFrom=tmp.Detach();
	//Message TO
	tmp=props.pszDisplayTo;
	pMessage->bstMessageTo=tmp.Detach();
	
	//Message Subject
	tmp=props.pszSubject;
	pMessage->bstMessageSubject=tmp.Detach();

	return TRUE;

}


STDMETHODIMP CFolder::GetFirstMessage(IMailItem **ppIMailItem)
{

#ifdef _MYDEBUG	
	OutputDebugString("CFolder::GetFirstMessage() entered");
#endif
	//IMailItem *pIMailItem;

	//===================
	//Declare Local Class varible
	//===================
	CComObject<CMailItem>* pMessage=NULL;
    *ppIMailItem=NULL;
	
	HRESULT hr=S_OK;

	
	 
	hr = CComObject<CMailItem>::CreateInstance (&pMessage);
	
	   
   if(FAILED(hr)) return S_FALSE;

#ifdef _MYDEBUG	
	OutputDebugString("Message Object Created");
#endif

   if(!this->m_bInitializationCalled)	   
	   this->InitializeFolder();

#ifdef _MYDEBUG	
	OutputDebugString("initialization called successful");
#endif

   
   if(SUCCEEDED(hr))
   {
	   
	   
	   MESSAGEPROPS prop;
	   prop.cbSize=sizeof(MESSAGEPROPS);
	   hr=this->pStoreFolder->GetFirstMessage(0,0,MESSAGEID_FIRST,&prop,&phEnumMessage);

#ifdef _MYDEBUG	
	OutputDebugString("First Message Call Successful");
	OutputDebugString(prop.pszDisplayTo);
	OutputDebugString(prop.pszDisplayFrom);
#endif

	   
	   
	   if(hr==S_OK)
	   {

		   //=============
		   //Fill Internal data Structure
		   //=============
		   this->fnFillMessageData(pMessage,prop);
		   
		   
		   //=================
		   //Free Message Props
		   //==================

		   this->pStoreFolder->FreeMessageProps(&prop);

		   
		   
		   
		   if(FAILED(pMessage->QueryInterface(__uuidof(IMailItem),reinterpret_cast<LPVOID*>(ppIMailItem))))
		   {
#ifdef _MYDEBUG	
	OutputDebugString("Query Interface failed");
#endif

			   hr=S_FALSE;
			   *ppIMailItem=NULL;
		   }
	   }
   }
	   
#ifdef _MYDEBUG	
	OutputDebugString("return successful");
#endif

	   //MessageBox(NULL,"Get Message cleared","10",MB_OK);
	   return S_FALSE;	
}

STDMETHODIMP CFolder::GetNextMessage(IMailItem **ppIMailItem)
{


   //IMailItem *pIMailItem;
	CComObject<CMailItem>* pMessage=NULL;
    *ppIMailItem=NULL;
	 
	HRESULT hr = CComObject<CMailItem>::CreateInstance (&pMessage);
	
	if(!pMessage) 
	{
		return S_FALSE;
	}

     

	if(SUCCEEDED(hr))
	{
		MESSAGEPROPS prop;
		ZeroMemory(&prop,sizeof(prop));
		prop.cbSize=sizeof(MESSAGEPROPS);
		hr=this->pStoreFolder->GetNextMessage(phEnumMessage,0,&prop);
		
		if(hr==S_OK)
		{
		
			
		   //=============
		   //Fill Internal data Structure
		   //=============
		   this->fnFillMessageData(pMessage,prop);
		   
		   
		   //=================
		   //Free Message Props
		   //================
			this->pStoreFolder->FreeMessageProps(&prop);

			if(FAILED(pMessage->QueryInterface(__uuidof(IMailItem),reinterpret_cast<LPVOID*>(ppIMailItem))))
			{
				*ppIMailItem=NULL;
				hr=S_FALSE;
			}
		}
		else
		{
			
			//alas we got last message close the message factory
			*ppIMailItem=NULL;
			this->pStoreFolder->GetMessageClose(this->phEnumMessage);		
			//return S_FALSE;
		}
		
	}
	
	
	
	return hr;
}

STDMETHODIMP CFolder::GetMessageByID(long MessageID, IMailItem **ppIMailItem)
{
	
	
	*ppIMailItem=NULL;
	
	//=======================
	//MEssage Prop Structure
	//=======================

	MESSAGEPROPS props;
	ZeroMemory(&props,sizeof(props));
	props.cbSize=sizeof(props);

	//=================
	//Message Inteface
	//=================
	// IMailItem *pIMailItem=NULL;
	//Com object for class module
	CComObject<CMailItem>* pMessage=NULL;
    
	
	HRESULT hr = CComObject<CMailItem>::CreateInstance (&pMessage);

	 if(FAILED(hr))
	 {
		 return S_FALSE;
	 }

	 if(!this->m_bInitializationCalled)
		 this->InitializeFolder();
	 


	 //=========================
	 //now try to open the Message
	 //===========================
	 
	 if(this->pStoreFolder)
	 {
		 
		 hr=this->pStoreFolder->GetMessageProps(MessageID,0,&props);
		 
		 if(SUCCEEDED(hr))
		 {
			 //=============
			 //Fill Internal data Structure
			 //=============
			 this->fnFillMessageData(pMessage,props);
			 
			 //Free Message Props
			 this->pStoreFolder->FreeMessageProps(&props);
			 
			 
			 
			 if(FAILED(pMessage->QueryInterface(__uuidof(IMailItem),reinterpret_cast<LPVOID*>(ppIMailItem))))
				 
			 {
				 //					MessageBox(NULL,"Get Message cleared","8",MB_OK);
				 hr=S_FALSE;
				 *ppIMailItem=NULL;
			 }
			 
			 
		 }
		 
		 
	 }

	return hr;
}

/*********************************************************************
  Method Name:SaveMessage(IMailItem **ppIMailItem)
  Last Modified: 28 Dec 2004
  Function Descrption: This Method Will Save the message
  Interafce Support =yes

 *********************************************************************/


STDMETHODIMP CFolder::CreateMessage(IMailItem **ppIMailItem)
{	//Saving the client from error
	*ppIMailItem=NULL;
	
	
	//Message Inteface
	IMailItem *pIMailItem=NULL;
	//Com object for class module
	CComObject<CMailItem>* pMessage=NULL;
    *ppIMailItem=NULL;
	 HRESULT hr = CComObject<CMailItem>::CreateInstance (&pMessage);

	 if(FAILED(hr))
	 {
		 return S_FALSE;
	 }

		if(!this->m_bInitializationCalled)
			this->InitializeFolder();
		
		//now try to open the Message
		if(this->pStoreFolder)
		{
	
			pMessage->m_dwMessageID=0;
			pMessage->m_dwParentFolderID=this->FolderID;
			//Message Serial
			pMessage->m_bSerialKey=TRUE;
			
			//Set Read Only To True
			pMessage->put_SetReadOnly(FALSE);

			//Check is this function is called by CloneMessage
			if(this->m_bDoClone==TRUE)
			{
				//if Yes
				pMessage->m_LocalMsgStruct=this->m_FolderMsgStruct;
				this->m_bDoClone=FALSE;
			}
		   
		   //MessageBox(NULL,"Get Message cleared","6",MB_OK);
		   
		   hr=pMessage->QueryInterface(__uuidof(IMailItem),reinterpret_cast<LPVOID*>(&pIMailItem));
		   
		   if(SUCCEEDED(hr))
		   {
			   //	MessageBox(NULL,"Get Message cleared","7",MB_OK);
			   *ppIMailItem=pIMailItem;
			   return S_OK;
			   
		   }
		   else
		   {
			   //					MessageBox(NULL,"Get Message cleared","8",MB_OK);
			   hr=S_FALSE;
			   *ppIMailItem=NULL;
		   }
		
		}

	return S_FALSE;
}

/*********************************************************************
  Method Name:SaveMessage(IMailItem **ppIMailItem)
  Last Modified: 28 Dec 2004
  Function Descrption: This Method Will Save the message
  Interafce Support =yes

 *********************************************************************/

STDMETHODIMP CFolder::SaveMessage(IMailItem **ppIMailItem)
{
//first Check is this legal message to Save
	BOOL bReadFlag;
	(*ppIMailItem)->get_SetReadOnly(&bReadFlag);
	if(bReadFlag==TRUE)
	{
		//i.e. This message Is set to Read Only
		Error(GetResStr(IDS_MAIL_READONLY));

		return E_FAIL;

	}
	
	
	
	
	IStream *newMail = NULL;
	MESSAGEID msgId;
	HRESULT hr;
	ULONG len;
	
	
	
	
	CString  msgSource;
	CString  DisplayTo;
	CString  DisplayFrom;
	CString  DisplaySubject;
	CString  DisplayBody;
	MsgPriority MailPriority;
	BOOL        bMailRead;

	BSTR pVal;

	//Get To
     (*ppIMailItem)->get_To(&pVal);

	 if(::SysStringLen(pVal)==0)
		 DisplayTo="";
	 else
		 DisplayTo=pVal;

	 
	 //Empty String
	 ::SysFreeString(pVal);
	 
	 //Get From
     (*ppIMailItem)->get_From(&pVal);

	 if(::SysStringLen(pVal)==0)
		 DisplayFrom="";
	 else
		 DisplayFrom=pVal;

	//Empty String
	 ::SysFreeString(pVal);
	 

	 //get Subject
     (*ppIMailItem)->get_Subject(&pVal);

	 if(::SysStringLen(pVal)==0)
		 DisplaySubject="";
	 else
		 DisplaySubject=pVal;

	 //Empty String
	 ::SysFreeString(pVal);
	 
	
	 //===========
	 //get Body
	 //===========
	 (*ppIMailItem)->Body(&pVal);

	 if(::SysStringLen(pVal)==0)
		 DisplayBody="";
	 else
		 DisplayBody=pVal;

	 //==============
	 //get Priority
	 //==============
	 (*ppIMailItem)->get_Priority(&MailPriority);

	 //==============
	 //Get Read State
	 //==============
	 VARIANT_BOOL varMailRead;
	(*ppIMailItem)->get_Read(&varMailRead);
	varMailRead==VARIANT_TRUE?bMailRead=TRUE:bMailRead=FALSE;
	 




	

    msgSource = _T("From: ");
	msgSource += DisplayFrom; 
	msgSource += _T("\r\n");
	msgSource += _T("To:");
	msgSource += DisplayTo; 
	msgSource += _T("\r\n");
	msgSource += _T("Subject:");
	msgSource += DisplaySubject;
	msgSource += _T("\r\n");
	
	switch(MailPriority)
	{
	case LOW: msgSource += _T("X-MSMail-Priority: Low");break;
	case NORMAL: msgSource += _T("X-MSMail-Priority: Normal");break;
	case HIGH: msgSource += _T("X-MSMail-Priority: High");break;
	}

	msgSource += _T("\r\n\r\n");
	msgSource += DisplayBody;

		if(!this->m_bInitializationCalled)
			this->InitializeFolder();
		
	
	hr = this->pStoreFolder->CreateStream(0, 0, &newMail, &msgId);
	if(FAILED(hr)) {
	//	MessageBox(_T("Cannot Create Stream."), _T("Demo Error"));
		return S_FALSE;
	}

	hr = newMail->Write((const TCHAR *) msgSource, msgSource.GetLength(), &len);
	if(FAILED(hr)) {
	//	MessageBox(_T("Cannot Write message."), _T("Demo Error"));
		newMail->Release();
		return S_FALSE;
	}

	hr = this->pStoreFolder->CommitStream(0, 0, 0, newMail, msgId, NULL);
	if(FAILED(hr)) {
	//	MessageBox(_T("Cannot Commit Stream."), _T("Demo Error"));
		newMail->Release();
		return S_FALSE;
	}

	newMail->Release();


	//Now it time to return the Interface Pointer
	(*ppIMailItem)->Release();
	*ppIMailItem=NULL;


	this->GetMessageByID(msgId,ppIMailItem);

	if(ppIMailItem)
		(*ppIMailItem)->put_Read(varMailRead);



	return S_OK;
}

/*********************************************************************
  Method Name:SaveMessage(IMailItem **ppIMailItem)
  Last Modified: 28 Dec 2004
  Function Descrption: This Method Will Save the message
  Interafce Support =yes

 *********************************************************************/

STDMETHODIMP CFolder::CloneMessageByID(long MsgID, IMailItem **ppIMailItem)
{
	
	IMailItem *pTmpMessage;
	//get Message
	this->GetMessageByID(MsgID,&pTmpMessage);

	if(pTmpMessage)
	{
		pTmpMessage->get_To(&this->m_FolderMsgStruct.To);
		pTmpMessage->get_From(&this->m_FolderMsgStruct.From);
		pTmpMessage->get_Subject(&this->m_FolderMsgStruct.Subject);
		pTmpMessage->Body(&this->m_FolderMsgStruct.Message);
		pTmpMessage->get_Read(&this->m_FolderMsgStruct.bRead);
		pTmpMessage->get_Priority(&this->m_FolderMsgStruct.MailPriority);

		this->m_bDoClone=TRUE;

		this->CreateMessage(ppIMailItem);
		
		if(ppIMailItem)
			return S_OK;
		else
		{
			ppIMailItem=NULL;
			return S_FALSE;
		}
	}
	ppIMailItem=NULL;

	return S_FALSE;
}
/*********************************************************************
  Method Name:CloneMessageByObject(IMailItem *pOldMessage, IMailItem **ppIMailItem)
  Last Modified: 28 Dec 2004
  Function Descrption: Clone the Given Message
  Interafce Support =yes

 *********************************************************************/

STDMETHODIMP CFolder::CloneMessageByObject(IMailItem *pOldMessage, IMailItem **ppIMailItem)
{
	*ppIMailItem=NULL;
	if(!pOldMessage)
	{
		return S_FALSE;
	}


	long msgId;

	pOldMessage->get_MessageID(&msgId);

    //Return Clone of Message
	this->CloneMessageByID(msgId,ppIMailItem);

	return S_OK;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
India India
He used to have biography here Smile | :) , but now he will hire someone (for free offcourse Big Grin | :-D ), Who writes his biography on his behalf Smile | :)

He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!

He started with Visual C++ then moved to C# then he become language agnostic, you give him task,tell him the language or platform, he we start immediately, if he knows the language otherwise he quickly learn it and start contributing productively

Last but not the least, For good 8 years he was Visual CPP MSMVP!

Comments and Discussions