"c# to move mail to pst" Code Answer

1

here is an example of how you would get the source folder (sentitems) and move them to a pst(archive).

using outlook = microsoft.office.interop.outlook; 

public void movemyemails()
    {
        //set up variables
        outlook.application oapp = null;
        outlook.mapifolder osource = null;
        outlook.mapifolder otarget = null;
        try
        {
            //instantiate variables
            oapp = new outlook.application();
            osource = oapp.session.getdefaultfolder(outlook.oldefaultfolders.olfoldersentmail);
            otarget = oapp.session.folders["archive"];
            //loop through the folders items
            for (int i = osource.items.count; i > 0; i--)
            {
                move the item
                osource.items[i].move(otarget);
            }
        }
        catch (exception e)
        {
            //handle exception
        }
        //release objects
        if (otarget != null)
        {
            system.runtime.interopservices.marshal.releasecomobject(otarget);
            gc.waitforpendingfinalizers();
            gc.collect();
        }
        if (osource != null)
        {
            system.runtime.interopservices.marshal.releasecomobject(osource);
            gc.waitforpendingfinalizers();
            gc.collect();
        }
        if (oapp != null)
        {
            system.runtime.interopservices.marshal.releasecomobject(oapp);
            gc.waitforpendingfinalizers();
            gc.collect();
        }

    }
By robev on June 1 2022

Answers related to “c# to move mail to pst”

Only authorized users can answer the Search term. Please sign in first, or register a free account.