private void btnReadMail_Click(object sender, EventArgs e)
{
try
{
try
{
ItemNo = Convert.ToInt32(txtItemNo.Text);
}
catch(FormatException ex2)
{
// MessageBox.Show();
MessageBox.Show(ex2.Message + “\nEnter Valid Number greater than zero”, “Outlook Reader”, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Check whether there is an running Outlook process
if(Process.GetProcessesByName(“OUTLOOK”).Count() > 0)
{
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
myApp = System.Runtime.InteropServices.Marshal.GetActiveObject(“Outlook.Application”) as Microsoft.Office.Interop.Outlook.Application;
}
else
{
//if not, creating a new application instance
myApp = new Microsoft.Office.Interop.Outlook.Application();
}
mapiNameSpace = myApp.GetNamespace(“MAPI”);
//selecting Inbox folder
myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
mapiNameSpace.SendAndReceive(false); //performs SendRecieve Operation without showing Progress Dialog
if(myInbox.Items.Count > 0 && ItemNo <= myInbox.Items.Count) //if checking mailcount starts
{
string subject = string.Empty;
string attachments = string.Empty;
string body = string.Empty;
string senderName = string.Empty;
string senderEmail = string.Empty;
string recepients = string.Empty;
string creationdate = string.Empty;
bool isMailItem = true;
Microsoft.Office.Interop.Outlook.MailItem MyOutlookItem = null;
try
{
//if the item is not a mail Item Application will throw COM exception
MyOutlookItem = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[ItemNo]);
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message + “\nThere Item “ + ItemNo + ” is not a Mail Item”, “Outlook Reader”, MessageBoxButtons.OK, MessageBoxIcon.Warning);
isMailItem = false;
}
if(isMailItem)
{
// Retrieving the Subject
if (MyOutlookItem.Subject != null)
{
subject = MyOutlookItem.Subject;
//while working with database, trying to use the values in sql query,
//replace ‘ with ” or some symbol, to avoid sql exceptions
subject = subject.Replace(”’, ‘”‘);
}
//Retrieving the Attachment Name
if (MyOutlookItem.Attachments.Count > 0)
{
lblAttachCount.Text = MyOutlookItem.Attachments.Count.ToString();
for (int j = 1; j <= MyOutlookItem.Attachments.Count; j++)
{
attachments += MyOutlookItem.Attachments[j].FileName + “; “;
}
}
else
{
lblAttachCount.Text = MyOutlookItem.Attachments.Count.ToString();
attachments = “No Attachments found”;
}
if (MyOutlookItem.Recipients.Count > 0)
{
lblReciCount.Text = MyOutlookItem.Recipients.Count.ToString();
for (int j = 1; j <= MyOutlookItem.Recipients.Count; j++)
{
recepients += MyOutlookItem.Recipients[j].Name + “< “ + MyOutlookItem.Recipients[j].Address + ” >; “;
}
}
attachments = attachments.Replace(”’, ‘”‘);
// retrieving the Body
body = MyOutlookItem.Body;
body = body.Replace(”’, ‘”‘);
// Sender Name
senderName = MyOutlookItem.SenderName;
senderName = senderName.Replace(”’, ‘”‘);
// Sender Email
senderEmail = MyOutlookItem.SenderEmailAddress;
senderEmail = senderEmail.Replace(”’, ‘”‘);
// Creation date
creationdate = MyOutlookItem.CreationTime.ToString();
txtCreateDate.Text = creationdate;
txtSenderEmail.Text = senderEmail;
txtSenderName.Text = senderName;
txtRecipients.Text = recepients;
txtSubject.Text = subject;
txtAttachments.Text = attachments;
txtBody.Text = body;
myApp = null;
}
}
elseif (ItemNo > myInbox.Items.Count) //else checking mailcount
{
MessageBox.Show(“There are only “ + myInbox.Items.Count + ” items in your Inbox.”, “Outlook Reader”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(“There are no items in your Inbox.”, “Outlook Reader”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch(System.Exception ex4)
{
MessageBox.Show(ex4.Message, “Outlook Reader:Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}