2009-01-15 61 views

回答

2

MSDN的參考?如果是2007年,則可以使用web service APIFindItem方法將允許您訪問特定文件夾中的項目。

+0

你給的鏈接doens't存在。我似乎無法在其他地方找到它。它可以中止嗎? – 2012-04-26 18:57:37

0

再或者,如果它的2007,您可以使用PowerShell,在.NET應用程序

0

我已經在C#中使用Exchange Server 2010和Windows Service完成了此操作。我從收件箱中檢索郵件,訪問郵件數據,編輯電子郵件的主題(當前硬編碼),並將其從收件箱移至另一個文件夾Saved。我將結果顯示在控制檯應用程序中進行測試,直到需要部署爲止。爲了使它每x分鐘檢查一次,將一個exe任務/作業添加到Windows計劃任務。這裏是代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Exchange101; 
using Microsoft.Exchange.WebServices.Data; 

namespace Exchange101 
{  
    class Notifications 
    { 
     static ExchangeService service = Service.ConnectToService(UserDataFromConsole.GetUserData(), new TraceListener()); 

    static void Main(string[] args) 
    { 
     //SetStreamingNotifications(service); 
     RecieveMails(service); 

     Console.WriteLine("\r\n"); 
     Console.WriteLine("Press or select Enter..."); 
     Console.Read(); 
    } 

    static void RecieveMails(ExchangeService service) 
    { 
     // Create a view with a page size of 100. 
     ItemView view = new ItemView(10); 

     // Indicate that the base property will be the item identifier 
     view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties); 
     view.PropertySet.Add(ItemSchema.IsAssociated); 

     // Set the traversal to associated. (Shallow is the default option; other options are Associated and SoftDeleted.) 
     view.Traversal = ItemTraversal.Associated; 

     // Send the request to search the Inbox. 
     FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view); 

     // Output a list of the item classes for the associated items 
     foreach (Item item in findResults) 
     { 
      Console.WriteLine(item.ItemClass); 
     } 

     findResults = service.FindItems(
     WellKnownFolderName.Inbox, 
     new ItemView(10)); //10 is the number of mails to fetch 

     foreach (Item item in findResults.Items) 
     {   
      //this needs to be here to recieve the message body 
      MessageBody messageBody = new Microsoft.Exchange.WebServices.Data.MessageBody(); 
      List<Item> items = new List<Item>(); 
      if (findResults.Items.Count > 0) // Prevent the exception 
      { 
       foreach (Item item2 in findResults) 
       { 
        items.Add(item2); 
       } 
      } 
      service.LoadPropertiesForItems(items, PropertySet.FirstClassProperties); 

      messageBody = item.Body.ToString(); 

      Console.WriteLine("=========================================================================="); 
      Console.WriteLine("IsNew: " + item.IsNew); 
      Console.WriteLine("To: " + item.DisplayTo); 
      Console.WriteLine("Subject: " + item.Subject); 
      Console.WriteLine("Message Body: " + item.Body.ToString()); 
      Console.WriteLine("Date & Time Received: " + item.DateTimeReceived); 
      Console.WriteLine("HasAttachments: " + item.HasAttachments);    

      //this is just what I have to do later 
      //CreateNewWorkflowFromEmail(); 
      //if (WorkflowWasCreated) then move email to saved folder 

      //here I change the subject and move the mail to my custom folder "Saved" 
      Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot); 
      rootfolder.Load(); 

      foreach (Folder folder in rootfolder.FindFolders(new FolderView(100))) 
      { 
       // This IF limits what folder the program will seek 
       if (folder.DisplayName == "Saved") 
       { 
        var fid = folder.Id; 
        //Console.WriteLine(fid);       
        item.Load(); 
        item.Subject = ("WF1234567 - " + item.Subject); 
        item.Update(ConflictResolutionMode.AlwaysOverwrite); 
        item.Move(fid);        
        } 
       } 
     } 
    }