Search code examples
c#outlookmailitem

how to copy mailitem in outlook c#


I am writing an outlook add-in, and I need when receiving an email, it goes to default inbox folder as normal, but it make a copy of that incoming email and put it into another folder(say Junk folder as example). but I encounter the exception "Cannot move the items." all the time when i move the copy of the email. any ideas?

private void items_ItemAdd(object Item)
 {


    Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)
    this.Application.ActiveExplorer().Session.GetDefaultFolder
    (Outlook.OlDefaultFolders.olFolderInbox);


    // the incoming email
    Outlook.MailItem mail = (Outlook.MailItem)Item;
    //make a copy of it but error occurs
    Outlook.MailItem cItem = mail.copy();
    //
    cItem = (Outlook.MailItem)cItem.Move((Outlook.MAPIFolder)
    this.Application.ActiveExplorer().Session.GetDefaultFolder
    (Outlook.OlDefaultFolders.olFolderJunk));

Solution

  • You are in the ItemAdd event handler. Try to store the item's entry id in a variable or a list and start a timer (use the one from the Forms namespace). When the time fires, you will be out of the event handler and should be able to call MailItem.Move.