Link to home
Start Free TrialLog in
Avatar of jokert
jokert

asked on

Unable to move an email w/ EWS

I am trying to simply move an email from the inbox to a user created folder called Processed.  When I execute the code below, the message dissappears from the inbox but never shows up in the Processed folder and in fact, never shows up anywhere.
private void Somefunction()
{
string exchangeWebServiceUrl = System.Configuration.ConfigurationSettings.AppSettings["ExchangeWebServiceUrl"];

                string emailDomain = System.Configuration.ConfigurationSettings.AppSettings["InboundEmailDomain"];
                string emailUserName = System.Configuration.ConfigurationSettings.AppSettings["InboundEmailUserName"];
                string emailPWD = System.Configuration.ConfigurationSettings.AppSettings["InboundEmailPWD"];

                string errorEmailFolderName = System.Configuration.ConfigurationSettings.AppSettings["ErrorsEmailFolder"];
                string processedEmailFolderName = System.Configuration.ConfigurationSettings.AppSettings["ProcessedEmailFolder"];

                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.Credentials = new WebCredentials(emailUserName, emailPWD, emailDomain);
                service.Url = new Uri(exchangeWebServiceUrl);

                // Set problem email folder.
                Folder errorEmailFolder = SetFolder(service, errorEmailFolderName);
                Folder processedEmailFolder = SetFolder(service, processedEmailFolderName);

                Folder f = Folder.Bind(service, WellKnownFolderName.Inbox);
                Folder p = Folder.Bind(service, processedEmailFolder.Id);
                Folder e = Folder.Bind(service, errorEmailFolder.Id);

                FindItemsResults<Item> items = f.FindItems(new ItemView(10));

                foreach (Item i in items)
                {
                    EmailMessage m = EmailMessage.Bind(service, i.Id);                    

                    if (m.Subject.Contains("Health Check Report"))
                    {
                        if (m.Attachments.Count > 0)
                        {
                            m.Move(p.Id);
                        }
                    }
                }
}

        private static Folder SetFolder(ExchangeService service, string folderName)
        {
            SearchFilter filter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, folderName);

            FindFoldersResults findResults = service.FindFolders(WellKnownFolderName.Root, filter, new FolderView(1));

            Folder searchFolder;

            if (findResults.Folders.Count > 0)
            {
                searchFolder = findResults.Folders[0];
            }
            else
            {
                searchFolder = new Folder(service);
                searchFolder.DisplayName = folderName;
                searchFolder.Save(WellKnownFolderName.Root);
            }

            return searchFolder;
        }
    }

Open in new window

Avatar of swapneel_d14
swapneel_d14

Use

P.Move(m) instead of  m.Move(p.Id);
Avatar of jokert

ASKER

P.Move(m) does not work.

Move() is expecting a folderID as a parameter so it does not accept the message as a parameter.
Or P.MoveHere()
Avatar of jokert

ASKER

There is no CopyHere or MoveHere function on any of the EWS items.  This is an exchange operation not a file operation.  The link you provided is for file copying...
Avatar of jokert

ASKER

As an update, I can move it to the Draft folder w/ no problem, but moving it to a folder that I created does not seem to work and then the email is lost.
Exchange service will not copy it on User's Folder.

ExchangeService  WebService  will not copy it on User's folder..
Avatar of jokert

ASKER

swapneel I have no idea what you just wrote, but it doesn't make any sense.

Here is what is going on after even more research.  I have manually created 2 folders via the web interface to exchange on this user account.  1 called Errors and 1 called Processed.  When I move the mail message to the Processed folder from the inbox it actually does move to the folder, however, the folder must be different than the one that I created via the web interface as if I look at the TotalCount of the Processed folder is shows the correct amount, but that folder has to be different than the one that I created via the web mail interface.  It appears that simply by searching for a folder using:

            SearchFilter filter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, folderName);

            FindFoldersResults findResults = service.FindFolders(WellKnownFolderName.Root, filter, new FolderView(1));

it automatically creates a virtual folder regardless of the name used and then stores the emails in there.  I am unable to find any of the emails in any of the folders and I have attempted to look thru the 2007 exchange admin tool on the server to find them and still nothing.  So I am now even more confused than before becasue I see via code that there is a folder called Processed and it contains emails and yet the Processed folder that is displayed on the web page is empty.
I am sorry for my above  text if it has confused you.

check that created folders "processedEmailFolderName" is not exists Under Deleted Items Folder.

And Try WellKnownFolderName.Inbox instead of WellKnownFolderName.Root in Search as well as In creation??

If you are only looking for the folder via web access, make sure that you refresh the folder list. Its not always automatic. Outlook on the other hand should show all folders if it is online.
ASKER CERTIFIED SOLUTION
Avatar of jokert
jokert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial