Home > Code > C# > Create & Download Email Messages from Public Folders of Exchange Server

Create & Download Email Messages from Public Folders of Exchange Server

by sher azam   on Dec 29, 2011   Category: C#   |  Views: 968    |  Points: 25   |  Gold 


This technical tip shows how to download Messages from Public Folders of Exchange Server. Microsoft Exchange Server provides the facility to create public folders and post messages in it. You can use ExchangeWebServiceClient class of Aspose.Email to connect to the Exchange Server and read/download messages and posts from Exchange public folders. Below is the sample source code to read all public folders/sub-folders and list/download messages inside these folders. This example will only work with MS Exchange Server 2007 or above due to EWS support.

[C#]

class Program
{
static string mailboxURI = "https://ex2010/ews/exchange.asmx"; // EWS
static string username = "administrator";
static string password = "pwd";
static string domain = "ex2010.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);
ExchangeWebServiceClient client = new ExchangeWebServiceClient(mailboxURI, credential);

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


To Test the code Download the following component for Free: http://www.aspose.com/categories/.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