Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Outlook2003 AddIN / Plugin Moving Items(Mail)

0.00/5 (No votes)
9 Jul 2007 1  
Whenever new mail is coming to inbox AddIN will move that mail to specific folder inside inbox.

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;    

    //Start a MAPI Session

    // Taking session for doing activity on outlook

    spApplication ->get_Session(&olNs);

    olNs->GetDefaultFolder(olFolderInbox,&spInboxFolder);
            
    spInboxFolder->get_Folders(&spFolderMove);
        
    CComVariant varFolderType1("IPF.Note");
        
    BSTR bstr_temp1(OLESTR("Hold_By_chaitanya"));  //Name of folder

        
    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;    

    //Start a MAPI Session

    Outlook::_Application *spOutlookApp; 
    ATLASSERT(spOutlookApp);

    spOutlookApp = m_spApp; 

    // Taking session for doing activity on outlook

    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) //strcmpi(lpszText1,lpszText2) != 0)

    {
        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);
    
//    Now count is changed and it contains unreaded mail count

    spUnReadedItems->get_Count(&count);
        
    spUnReadedItems->GetFirst(&pDisp);
            
    // Initialzing spMail so that i will not corrupt

    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here