Get inbox message from Exchange server using .net

余生长醉 提交于 2019-12-05 01:53:21

问题


Anybody knows how to do this?

I need to create a service that will connect to the exchange server and download the messages ever x minutes...

thanks!


回答1:


You'll probably want to use WebDAV. Here's a good article on the subject

Also, here's the MSDN reference on the Exchange Store




回答2:


Which version of Exchange Server are you using? If it's 2007, you could use the web service API. The FindItem method will let you access items in a specific folder.




回答3:


Or again if its 2007 you can use powershell, hosted in a .net app




回答4:


Please visit http://www.aspose.com/documentation/.net-components/aspose.network-for-.net/managing-emails-on-exchange-server.html, if you are interested in using 3rd party libraries. Aspose.Network supports accessing emails from Exchange Server Inbox and save to file in eml or msg format.




回答5:


I have done this using Exchange Server 2010 and Windows Service in C#. I retrieve the mails from the inbox, access the mail data, edit the subject of the email(currently hard coded) and move it to another folder, Saved, from the Inbox. I display results in a console app for testing purposes until I need to deploy this. To make it check every x minutes, add an exe task/job to Windows Scheduled tasks. Here is the code:

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


来源:https://stackoverflow.com/questions/447413/get-inbox-message-from-exchange-server-using-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!