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

Create & Download Email Messages from Public Folders of Exchange Server

by Sherazam   on Dec 29, 2011   Category: C#   |  Views: 60    |  Points: 25   |  Starter



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


Like this code? Bookmark and Share:

  User Responses | Post Code
 No Response found!
  Recent Posts
* Setting focus to textbox control using JavaScript (240) by Narain_Siddharth on Nov 13 2010 11:25AM
* Sending Mails with Attachements using Asp.Net (280) by Narain_Siddharth on Nov 13 2010 11:48AM
* Disable the Submit button when insert or update a record using JavaScript (198) by Narain_Siddharth on Nov 13 2010 12:31PM
* String comparisons using C#.NET string.compareTo (193) by Thamilselvanj on Nov 14 2010 11:16AM
* String.Split using C# (202) by Thamilselvanj on Nov 14 2010 11:23AM
  Submit feedback about this code snippet
Please sign in to post feedback