article

How to Fetch Email Messages from an Exchange Server Mailbox inside .NET Apps

Email
Submitted on: 10/28/2015 7:40:45 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 3610
 
     This technical tip explains how .NET developers can Fetch Email Messages from an Exchange Server Mailbox inside .NET Apps. Listing Messages on an Exchange Server used the listMessages() method to get a list of messages from an Exchange Server mailbox. The listMessage() method gets basic information about messages, for example, the subject, the message ID, from and to. To get the complete message details, Aspose.Email.Exchange provides the ExchangeClient.fetchMessage() method. This method accepts the message URI as a parameter and returns am instance of the Aspose.Emai.Mail.MailMessage class. The MailMessage class then provides message details like body, headers and attachments.Find out more about the MailMessage API, or find out how to manage emails with the MailMessage class.

 
				This technical tip explains how .NET developers can Fetch Email Messages from an Exchange Server Mailbox inside .NET Apps. Listing Messages on an Exchange Server used the listMessages() method to get a list of messages from an Exchange Server mailbox. The listMessage() method gets basic information about messages, for example, the subject, the message ID, from and to. To get the complete message details, Aspose.Email.Exchange provides the ExchangeClient.fetchMessage() method. This method accepts the message URI as a parameter and returns am instance of the Aspose.Emai.Mail.MailMessage class. The MailMessage class then provides message details like body, headers and attachments.Find out more about the MailMessage API, or find out how to manage emails with the MailMessage class. To fetch messages from Exchange Server Mailbox:
•	Create an instance of type ExchangeClient.
•	Specify the server name, user name, password and domain.
•	Call ListMessages to get the ExchangeMessageInfoCollection.
•	Loop through the ExchangeMessageInfoCollection collection to get ExchangeMessageInfo.UniqueURI values.
•	Call ExchangeClient.fetch() and pass ExchangeMessageInfo.getUniqueURI() as parameter.
The following code snippets connect to the Exchange Server mailbox and fetches all the messages.
//[Java Code Sample]
// Create instance of ExchangeClient class by giving credentials
ExchangeClient client = new ExchangeClient("http://ex07sp1/exchange/Administrator","username", "password", "domain");
// Call ListMessages method to list messages info from Inbox
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri());
// Loop through the collection to get Message URI
for (ExchangeMessageInfo msgInfo : msgCollection)
{
String strMessageURI = msgInfo.getUniqueUri();
// Now get the message details using FetchMessage()
MailMessage msg = client.fetchMessage(strMessageURI);
// Display message details
System.out.println("Subject: " + msg.getSubject());
//Console.WriteLine("HTML Body: " + msg.HtmlBody);
// How many attachments
System.out.println("Number of attachments: " + msg.getAttachments().size());
// List the attachments
for (Attachment att : msg.getAttachments())
{
System.out.println("Attachment Name: " + att.getName());
}
}
//Below are code snippets that fetches messages using EWS:
//[C# Code Sample]
// Create instance of ExchangeWebServiceClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Call ListMessages method to list messages info from Inbox
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri());
// Loop through the collection to get Message URI
for (ExchangeMessageInfo msgInfo : msgCollection)
{
String strMessageURI = msgInfo.getUniqueUri();
// Now get the message details using FetchMessage()
MailMessage msg = client.fetchMessage(strMessageURI);
// Display message details
System.out.println("Subject: " + msg.getSubject());
//Console.WriteLine("HTML Body: " + msg.HtmlBody);
// How many attachments
System.out.println("Number of attachments: " + msg.getAttachments().size());
// List the attachments
for (Attachment att : msg.getAttachments())
{
System.out.println("Attachment Name: " + att.getName());
}
}
//Enumerating Messages with Paging in EWS
 
//[C# Code Sample]
// Create instance of ExchangeWebServiceClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Call ListMessages method to list messages info from Inbox
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri());
try
{
	int itemsPerPage = 5;
	//////////////////////////////////////////////////////
	List pages = new List();
	ExchangeMessageInfoCollection pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage);
	pages.Add(pagedMessageInfoCol);
	while (!pagedMessageInfoCol.LastPage)
	{
		pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1);
		pages.Add(pagedMessageInfoCol);
	}
	//////////////////////////////////////////////////////
	pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage);
	while (!pagedMessageInfoCol.LastPage)
	{
	client.ListMessages(client.MailboxInfo.InboxUri, pagedMessageInfoCol, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1);
	}
}
finally
{
}
//[VB.NET Code Sample]
' Create instance of ExchangeWebServiceClient class by giving credentials
Dim client As IEWSClient = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain")
' Call ListMessages method to list messages info from Inbox
Dim msgCollection As ExchangeMessageInfoCollection = client.listMessages(client.getMailboxInfo().getInboxUri())
Try
	Dim itemsPerPage As Integer = 5
	'''///////////////////////////////////////////////////
	Dim pages As New List(Of ExchangeMessageInfoCollection)()
	Dim pagedMessageInfoCol As ExchangeMessageInfoCollection = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage)
	pages.Add(pagedMessageInfoCol)
	While Not pagedMessageInfoCol.LastPage
		pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1)
		pages.Add(pagedMessageInfoCol)
	End While
	'''///////////////////////////////////////////////////
	pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage)
	While Not pagedMessageInfoCol.LastPage
		client.ListMessages(client.MailboxInfo.InboxUri, pagedMessageInfoCol, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1)
	End While
Finally
End Try
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.


Other 44 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.