Over a million developers have joined DZone.

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

DZone's Guide to

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

· ·
Free Resource
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.
//Sample Code for Downloading Messages from the Public Folders of Exchange Server


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)
        catch (Exception ex)

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

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())
		Catch ex As Exception
		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)
			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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}