Hi,
I'm getting an exception while tried to retrive an outlook contact using asp.net in a console application.
The exception is as follows:
An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication1.exe,Additional information: Object reference not set to an instance of an object
..
Can anybody help me pls....
my code is like this..
using System;
using System.Reflection; // to use Missing.Value
//TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
//using Outlook = Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
public class Class1
{
public static int Main(string[] args)
{
try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Get the NameSpace information.
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using a dialog box to choose the profile.
oNS.Logon(Missing.Value, Missing.Value, true, true);
Console.WriteLine("searching folder");
// Alternate logon method that uses a specific profile.
// TODO: If you use this logon method,
// change the profile name to an appropriate value.
oNS.Logon("YourValidProfile", Missing.Value, false, true);
// Get the default Contacts folder.
Outlook.MAPIFolder oContacts = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts);
// Get the Items collection from the folder.
Outlook.Items oItems = (Outlook.Items)oContacts.Items;
int itemcount = oItems.Count;
// Get the first contact item in the Items collection.
Outlook.ContactItem oCt = (Outlook.ContactItem)oItems.GetFirst();
// Output some common properties.
Console.WriteLine(oCt.FullName);
Console.WriteLine(oCt.Title);
Console.WriteLine(oCt.Birthday);
Console.WriteLine(oCt.CompanyName);
Console.WriteLine(oCt.Department);
Console.WriteLine(oCt.Body);
Console.WriteLine(oCt.FileAs);
Console.WriteLine(oCt.Email1Address);
Console.WriteLine(oCt.BusinessHomePage);
Console.WriteLine(oCt.MailingAddress);
Console.WriteLine(oCt.BusinessAddress);
Console.WriteLine(oCt.OfficeLocation);
Console.WriteLine(oCt.Subject);
Console.WriteLine(oCt.JobTitle);
// Display the contact.
oCt.Display(true);
// Log off.
oNS.Logoff();
// Explicitly release objects.
oCt = null;
oItems = null;
oContacts = null;
oNS = null;
oApp = null;
}
//Simple error handling.
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
//Default return value.
return 0;
}
}
}