Introduction
For this article you should have basic knowledge of outlook AddIN.If you are beginner then go through my first basic article which will help you lot First artical Link .Outlook AddIN is moving unread marked item(mail)of inbox to some specific folder inside Oulook. AddIN is also moving mail when receiving new mail it is directly moving item to specific folder in outlook unknowingly of user. Moving item(mail) is very easy in VB but it is bit difficult in vc++ that's why I have added this article.
Using the code
You need to register DLL using regsvr32 "dll full path" command. If DLL is registered successfully then start outlook. Whenever you will be receiving new mail in inbox all unread marked mail will be move to folder "Hide_By_Chaitanya" this folder is created by program on OnStartupComplete. And then program is catching new mail event. Whenever new mail event is occurred it is calling OnNewMail method. This method is containing code for moving unread marked mail from Inbox to Hide_By_Chaitanya folder.
There are two main part of this code.
1. OnStartupComplete
Outlook::_Application *spApplication;
spApplication = m_spApp;
CComPtr <Outlook::_NameSpace> olNs;
Outlook::MAPIFolder *spMapiFolderMove;
Outlook::_Folders *spFolderMove;
CComPtr <Outlook::MAPIFolder> spInboxFolder;
spApplication ->get_Session(&olNs);
olNs->GetDefaultFolder(olFolderInbox,&spInboxFolder);
spInboxFolder->get_Folders(&spFolderMove);
CComVariant varFolderType1("IPF.Note");
BSTR bstr_temp1(OLESTR("Hold_By_chaitanya"));
spFolderMove->Add(bstr_temp1,varFolderType1,&spMapiFolderMove);
This much part of code is for creating new folder inside Inbox folder in outlook.
2. OnNewMail
IDispatch *pDisp;
IDispatch *pDisptemp;
CComPtr <Outlook::_NameSpace> olNs;
CComPtr<Outlook::_Items> spItems;
Outlook::_Items *spUnReadedItems;
Outlook::_MailItem *spMailItem;
Outlook::MAPIFolder *spMapiFolderMove;
Outlook::_Folders *spFolderMove;
CComPtr <Outlook::MAPIFolder> spInboxFolder;
Outlook::_Application *spOutlookApp;
ATLASSERT(spOutlookApp);
spOutlookApp = m_spApp;
spOutlookApp->get_Session(&olNs);
long count;
spMapiFolderMove = NULL;
olNs->GetDefaultFolder(olFolderInbox,&spInboxFolder);
spInboxFolder->get_Folders(&spFolderMove);
spFolderMove->GetFirst(&spMapiFolderMove);
spFolderMove->get_Count(&count);
BSTR bstrCompare(OLESTR("Hold_By_chaitanya"));
BSTR bstrCompare2;
spMapiFolderMove->get_Name(&bstrCompare2);
char* lpszText1 = _com_util::ConvertBSTRToString(bstrCompare);
char* lpszText2 = _com_util::ConvertBSTRToString(bstrCompare2);
HRESULT hr = NULL;
int i=0;
while(i<count)
{
lpszText2 = _com_util::ConvertBSTRToString(bstrCompare2);
if(strcmpi(lpszText1,lpszText2) == 0)
break;
spFolderMove->GetNext(&spMapiFolderMove);
spMapiFolderMove->get_Name(&bstrCompare2);
i++;
}
spInboxFolder->get_Items(&spItems);
_bstr_t bstrUnReadCondition(OLESTR("[UnRead] = true"));
spItems->Restrict(bstrUnReadCondition,&spUnReadedItems);
spUnReadedItems->get_Count(&count);
spUnReadedItems->GetFirst(&pDisp);
spMailItem = NULL;
pDisp = NULL;
CComVariant tempInt(1);
for(i=0;i<count;i++)
{
tempInt.lVal = i+1;
spUnReadedItems->Item(tempInt,&pDisp);
if(pDisp != NULL)
{
pDisp->QueryInterface(IID__MailItem,(void**)&spMailItem);
if(spMailItem != NULL)
{
spMailItem->Move(spMapiFolderMove,&pDisptemp);
}
}
}
This part of code is for moving unread marked mail from inbox to our created folder. You can also change folder name by changing name inside BSTR variable.
Points of Interest
You should search outlookspy on the net and install it this can help you to understand few things about outlook.
History
If you are facing any problem in understanding you need to go to basic of creating outlook AddIN. I have not included here basic steps of crating AddIN because of insufficient time. I can suggest you to go through my first article First artical Link where I have included basic thing and different links which can help you understanding basic of creation of outlook AddIN.