使用.net从Exchange服务器获取收件箱邮件

时间:2009-01-15 16:18:16

标签: .net email exchange-server

有人知道怎么做吗?

我需要创建一个连接到Exchange服务器的服务,并在x分钟内下载消息......

谢谢!

5 个答案:

答案 0 :(得分:2)

您可能想要使用WebDAV。这是关于主题的a good article

此外,这是Exchange Store

上的MSDN参考

答案 1 :(得分:2)

您使用的是哪个版本的Exchange Server?如果是2007年,您可以使用web service APIFindItem方法可让您访问特定文件夹中的项目。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果您对使用第三方库感兴趣,请访问http://www.aspose.com/documentation/.net-components/aspose.network-for-.net/managing-emails-on-exchange-server.html。 Aspose.Network支持从Exchange Server收件箱访问电子邮件,并以eml或msg格式保存到文件。

答案 4 :(得分:0)

我在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);                            
                    }
                }
        }
    }