Home > Code > C# > How to Fetch Email Messages from an Exchange Server Mailbox inside .NET Apps

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

by sher azam   on Oct 28, 2015   Category: C#   |  Views: 736    |  Points: 25   |  Gold 


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<ExchangeMessageInfoCollection> pages = new List<ExchangeMessageInfoCollection>();

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.

- 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