简体   繁体   中英

C# Outlook ; After creating folder can't move emails

My application is supposed to send some emails to some destination. After that operation I would like to automatically move sent mails to specific folder ( based on the document type that is in the mail attachment ). If the folder doesn't exist then the program has to create it and then move the mail to the newly created folder. The issue is that after I create a new folder and succcesfully move the mail to it for the first time, then when i sent anothe mails that are supposed to be moved to the said folder the program doesn't see the folder. In fact the Folders method doesn't return any folders at all. frankly, im out of ideas whats wrong.

when checking in the debugger it says that parentFolder.Folders "Enumeration yielded no results"

I am not sure if I should do anything more after creating the folder in the method createFolder ( ie. something like, update folders list... )

here is my code:

    public void moveEmails(string itemType, Boolean itemSent, Outlook.MailItem objMail)
    {
        Outlook.MAPIFolder folderParent = objMail.Parent as Outlook.MAPIFolder;
        Outlook.Folders folders;
        Boolean notMoved = true;

        objMail.UserProperties.Add("TransferredBy", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
        objMail.UserProperties["TransferredBy"].Value = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        objMail.Save();


        if (folderParent.Name != "Inbox")
            folderParent = digForInbox(folderParent);

        folders = folderParent.Folders;

        if (!itemSent)
            itemType = "NOT DELIVERED";

        foreach (Outlook.MAPIFolder folder in folders)
        {
            if (folder.Name == itemType)
            {
                objMail.Move(folder);
                notMoved = false;
            }
        }
        if (notMoved)
            createFolder(itemType,objMail, folderParent);
    }

    public void createFolder(string itemType, Outlook.MailItem objMail, Outlook.MAPIFolder folderParent)
    {
        Outlook.MAPIFolder folderNew;
        folderNew = folderParent.Folders.Add( itemType, Outlook.OlDefaultFolders.olFolderInbox ) as Outlook.MAPIFolder;
        objMail.Move(folderNew);
    }
    private Outlook.MAPIFolder digForInbox(Outlook.MAPIFolder folder)
    {
        Boolean isNotInbox = true;
        while(isNotInbox)
        {
            if(folder.Name != "Inbox")
            {
                folder = folder.Parent as Outlook.MAPIFolder;
            }
            else
            {
                isNotInbox = false;
            }
        }
        return folder;
    }

I have found the answer to my question:

https://social.msdn.microsoft.com/forums/windows/en-us/180c000c-524a-45dd-88fe-88b470be3597/accessing-subfolders-within-shared-mailbox?forum=outlookdev

the issue was similar to the one in the link. I didnt imagine that because my mailboxes are mainly shared ones that would affect it in any other way than performance (due to connecting to the exchange server )

Posting this as an answer

I'd suggest using the SaveSentMessageFolder property of the MailItem class. It allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent. Also you may find the following articles helpful:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM