In this migration tip, we will connect to the Microsoft Exchange Server mailbox and get the list of messages from Inbox folder. First, we will use Microsoft Office Interop to get list of messages and then we will use classes in Aspose.Network.Exchange namespace using both C# and VB.NET.
Using Microsoft Office Interop
To use Office Automation objects for Microsoft Outlook, you are required to add references to Microsoft Office and Microsoft Office Interop for Outlook libraries in the project. Microsoft Office Outlook must also be installed on the machine, where the following code will run.
[C#]
// create Application class and get namespace
Outlook.Application outlook = new Outlook.ApplicationClass();
Outlook.NameSpace ns = outlook.GetNamespace("Mapi");
object _missing = Type.Missing;
ns.Logon(_missing, _missing, false, true);
// get Inbox information in objec of type MAPIFolder
Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
// unread emails
int unread = inbox.UnReadItemCount;
// display the subject of emails in the Inbox folder
foreach (Outlook.MailItem mail in inbox.Items)
{
Console.WriteLine(Wmail.Subject);
}
[VB.NET]
' create Application class and get namespace
Dim outlook As Outlook.Application = New Outlook.ApplicationClass()
Dim ns As Outlook.NameSpace = outlook.GetNamespace("Mapi")
Dim _missing As Object = Type.Missing
ns.Logon(_missing, _missing, False, True)
' get Inbox information in objec of type MAPIFolder
Dim inbox As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
' unread emails
Dim unread As Integer = inbox.UnReadItemCount
' display the subject of emails in the Inbox folder
For Each mail As Outlook.MailItem In inbox.Items
Console.WriteLine(Wmail.Subject)
Next mail
Using Aspose.Network
The following code snippet does the same thing using Aspose.Network library. However, in this case Microsoft Outlook does not need to be installed on the machine where this code will run. And a reference to Aspose.Network.dll is required in order to build and run the project successfully.
[C#]
// Create instance of ExchangeClient class by giving credentials
ExchangeClient client = new ExchangeClient("http://MachineName/exchange/Username",
"username", "password", "domain");
// Call ListMessages method to list messages info from Inbox
ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri);
// loop through the collection to display the basic information
foreach (ExchangeMessageInfo msgInfo in msgCollection)
{
Console.WriteLine("Subject: " + msgInfo.Subject);
Console.WriteLine("From: " + msgInfo.From.ToString());
Console.WriteLine("To: " + msgInfo.To.ToString());
Console.WriteLine("Message ID: " + msgInfo.MessageId);
Console.WriteLine("Unique URI: " + msgInfo.UniqueUri);
Console.WriteLine("==================================");
}
[VB.NET]
' Create instance of ExchangeClient class by giving credentials
Dim client As ExchangeClient = New ExchangeClient("http://MachineName/exchange/Username", "username", "password", "domain")
' Call ListMessages method to list messages info from Inbox
Dim msgCollection As ExchangeMessageInfoCollection = client.ListMessages(client.MailboxInfo.InboxUri)
' loop through the collection to display the basic information
For Each msgInfo As ExchangeMessageInfo In msgCollection
Console.WriteLine("Subject: " & msgInfo.Subject)
Console.WriteLine("From: " & msgInfo.From.ToString())
Console.WriteLine("To: " & msgInfo.To.ToString())
Console.WriteLine("Message ID: " & msgInfo.MessageId)
Console.WriteLine("Unique URI: " & msgInfo.UniqueUri)
Console.WriteLine("==================================")
Next msgInfo