• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1835
  • Last Modified:

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

0
jokert
Asked:
jokert
  • 5
  • 5
1 Solution
 
swapneel_d14Commented:
Use

P.Move(m) instead of  m.Move(p.Id);
0
 
jokertAuthor Commented:
P.Move(m) does not work.

Move() is expecting a folderID as a parameter so it does not accept the message as a parameter.
0
 
swapneel_d14Commented:
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
swapneel_d14Commented:
Or P.MoveHere()
0
 
jokertAuthor Commented:
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...
0
 
jokertAuthor Commented:
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.
0
 
swapneel_d14Commented:
Exchange service will not copy it on User's Folder.

ExchangeService  WebService  will not copy it on User's folder..
0
 
jokertAuthor Commented:
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.
0
 
swapneel_d14Commented:
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??

0
 
Stacy SpearPresident/Principal ConsultantCommented:
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.
0
 
jokertAuthor Commented:
The only way I have been successful with this is to create Public folders in exchange.  Once I created Public folders, I was able to access them, however, I am still not able to access user created folders.  I'm closing this.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now