Home > Code > C# > Download Email Messages from Public Folders of Exchange Server in .NET

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

by sher azam   on Nov 13, 2013   Category: C#   |  Views: 1945    |  Points: 25   |  Gold 

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

Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

More about Aspose.Email for .NET

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx

Post Code  |  Code Snippet Home

User Responses

No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts