DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 93 posts at DZone. View Full User Profile

Download Email Messages from Public Folders of Exchange Server in .NET

11.13.2013
| 3542 views |
  • submit to reddit
//Sample Code for Downloading Messages from the Public Folders of Exchange Server

//[C#]

class Program
{
    static string mailboxURI = "https://exchange/ews/exchange.asmx"; // EWS
    static string username = "administrator";
    static string password = "pwd";
    static string domain = "ex2013.local";


    static void Main(string[] args)
    {
        try
        {
            ReadPublicFolders();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    private static void ReadPublicFolders()
    {
        NetworkCredential credential = new NetworkCredential(username, password, domain);
        IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);

        ExchangeFolderInfoCollection folders = client.ListPublicFolders();
        foreach (ExchangeFolderInfo publicFolder in folders)
        {
            Console.WriteLine("Name: " + publicFolder.DisplayName);
            Console.WriteLine("Subfolders count: " + publicFolder.ChildFolderCount);
            ListMessagesFromSubFolder(publicFolder, client);

        }
    }

    private static void ListMessagesFromSubFolder(ExchangeFolderInfo publicFolder, ExchangeWebServiceClient client)
    {
        Console.WriteLine("Folder Name: " + publicFolder.DisplayName);
        ExchangeMessageInfoCollection msgInfoCollection = client.ListMessagesFromPublicFolder(publicFolder);
        foreach (ExchangeMessageInfo messageInfo in msgInfoCollection)
        {
            MailMessage msg = client.FetchMessage(messageInfo.UniqueUri);
            Console.WriteLine(msg.Subject);
            msg.Save(msg.Subject + ".msg", MailMessageSaveType.OutlookMessageFormat);
        }

        // Call this method recursively for any subfolders
        if (publicFolder.ChildFolderCount > 0)
        {
            ExchangeFolderInfoCollection subfolders = client.ListSubFolders(publicFolder);
            foreach (ExchangeFolderInfo subfolder in subfolders)
            {
                ListMessagesFromSubFolder(subfolder, client);
            }
        }
    }
}
 
//[VB.NET]

Class Program
	Private Shared mailboxURI As String = "https://ex2010/ews/exchange.asmx" ' EWS
	Private Shared username As String = "administrator"
	Private Shared password As String = "pwd"
	Private Shared domain As String = "ex2010.local"


	Shared Sub Main(ByVal args As String())
		Try
			ReadPublicFolders()
		Catch ex As Exception
			Console.WriteLine(ex.Message)
		End Try
	End Sub

	Private Shared Sub ReadPublicFolders()
		Dim credential As NetworkCredential = New NetworkCredential(username, password, domain)
		Dim client As ExchangeWebServiceClient = New ExchangeWebServiceClient(mailboxURI, credential)

		Dim folders As ExchangeFolderInfoCollection = client.ListPublicFolders()
		For Each publicFolder As ExchangeFolderInfo In folders
			Console.WriteLine("Name: " & publicFolder.DisplayName)
			Console.WriteLine("Subfolders count: " & publicFolder.ChildFolderCount)
			ListMessagesFromSubFolder(publicFolder, client)

		Next publicFolder
	End Sub

	Private Shared Sub ListMessagesFromSubFolder(ByVal publicFolder As ExchangeFolderInfo, ByVal client As ExchangeWebServiceClient)
		Console.WriteLine("Folder Name: " & publicFolder.DisplayName)
		Dim msgInfoCollection As ExchangeMessageInfoCollection = client.ListMessagesFromPublicFolder(publicFolder)
		For Each messageInfo As ExchangeMessageInfo In msgInfoCollection
			Dim msg As MailMessage = client.FetchMessage(messageInfo.UniqueUri)
			Console.WriteLine(msg.Subject)
			msg.Save(msg.Subject & ".msg", MailMessageSaveType.OutlookMessageFormat)
		Next messageInfo

		' Call this method recursively for any subfolders
		If publicFolder.ChildFolderCount > 0 Then
			Dim subfolders As ExchangeFolderInfoCollection = client.ListSubFolders(publicFolder)
			For Each subfolder As ExchangeFolderInfo In subfolders
				ListMessagesFromSubFolder(subfolder, client)
			Next subfolder
		End If
	End Sub
End Class
This technical tip explains how developers can download messages from the Public Folders of Exchange Server using Aspose.Email for .NET. Microsoft Exchange Server lets users create public folders and post messages in them. To do this through your application, use Aspose.Email's ExchangeWebServiceClient class to connect to the Exchange Server and read and download messages and posts from public folders.  The sample source code below reads all public folders, and subfolders, and lists and downloads any messages found in these folders. This example only works with Microsoft Exchange Server 2007 or above since only these support EWS.