簡體   English   中英

使用.net從Exchange服務器獲取收件箱郵件

[英]Get inbox message from Exchange server using .net

誰知道怎么做?

我需要創建一個連接到Exchange服務器並在x分鍾內下載消息的服務...

謝謝!

您可能想要使用WebDAV。 這是一篇關於這個主題的好文章

此外,這是Exchange存儲上的MSDN參考

您使用的是哪個版本的Exchange Server? 如果是2007年,您可以使用Web服務API FindItem方法將允許您訪問特定文件夾中的項目。

如果您對使用第三方庫感興趣,請訪問http://www.aspose.com/documentation/.net-components/aspose.network-for-.net/managing-emails-on-exchange-server.html Aspose.Network支持從Exchange Server收件箱訪問電子郵件,並以eml或msg格式保存到文件。

我在C#中使用Exchange Server 2010和Windows服務完成了此操作。 我從收件箱中檢索郵件,訪問郵件數據,編輯電子郵件的主題(當前是硬編碼的)並將其從收件箱移動到另一個文件夾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);                            
                    }
                }
        }
    }

或者,如果它的2007年你可以使用在.net應用程序中托管的powershell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM