Important alert: (current site time 7/16/2013 7:32:29 AM EDT)
 

article

Create & Download Email Messages from Public Folders of Exchange Server

Email
Submitted on: 12/29/2011 4:43:51 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2738
 
     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.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				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);
}
}
}
}
 
 [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
 
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/categories/.net-components/aspose.email-for-.net/default.aspx 
- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx 
 	
- More Technical Tips by Aspose.Email for .NET: http://www.aspose.com/documentation/.net-components/aspose.email-for-.net/knowledge-base.html
 
Contact Information
Aspose Pty Ltd, Suite 163, 
79 Longueville Road
Lane Cove, NSW, 2066 
Australia 
http://www.aspose.com/
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465 


Other 16 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.