Access your Email within Outlook (Final Part) C#

Category: .NET Framework

Access your Email within Outlook - Part Three of Three

This is the Final Part of this article series. Read Part 2 here

Introduction

This is an introduction to Microsoft Outlook and its accessible properties and methods you can access through C#.  We will not be working with the entire set of properties and methods, just the properties that can be attributed to an email.  We will be showing you how to access your Outlook email through C# and how to display that information on a screen.

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

In this final article of this series we finalize the remaining coding and access the remaining properties needed to populate our screen.  We will also display a screen shot of the final solution with the data populated from the first email returned in the Outlook collection object.

Implementation

In this article we will be working with Microsoft Outlook and its accessible properties and methods.  We will be inserting a reference to the Outlook Namespace and accessing the Inbox to see if we can locate an email.  If we find an email we will then go ahead and display the first email we find, and even display the name of the first attachment in that email.

 

Sender Name

In order to return the Sender’s Name of an email we will need to cast the item within the MAPI Inbox folder to a MailItem.  Once it has been correctly casted you can then access the Sender’s Name property.

((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderName;

 

Sender Email Address

In order to return the Sender’s Email Address of an email we will need to cast the item within the MAPI Inbox folder to a MailItem.  Once it has been correctly casted you can then access the Sender’s Email Address property.

((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderEmailAddress;

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Email Creation date

In order to return the Creation Date of an email we will need to cast the item within the MAPI Inbox folder to a MailItem.  Once it has been correctly casted you can then access the Creation Date property.

((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).CreationTime.ToString();

You have now added all of the necessary property calls and have returned all of the information required to populate the application screen we have created.  For a visual representation of the final solution please refer to Fig. 5.

Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
namespace OutlookEmailAccess
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAccessEmail_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
if (myInbox.Items.Count > 0)
{
// Grab the Subject
lblSubject.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Subject;
//Grab the Attachment Name
if (((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Attachments.Count > 0)                
{
lblAttachmentName.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Attachments[1].FileName;
}
else
{
lblAttachmentName.Text = "No Attachment";
}
// Grab the Body
txtBody.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Body;
// Sender Name
lblSenderName.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderName;
// Sender Email
lblSenderEmail.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderEmailAddress;
// Creation date
lblCreationdate.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).CreationTime.ToString();
}
else
{
MessageBox.Show("There are no emails in your Inbox.");
}
}
}
}

Compile and execute the above code.  When the application starts you will be presented with a screen that will allow you to select a button to “Access Email” from your local installation of Outlook.  The results of that access test will be displayed in the fields on the screen.  Please see Fig. 5 for a visual representation of the screen we just discussed.

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Screen shot of the application [Fig. 5]:


[Click to see full-size]


What have we learned?

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

  • That we can access our Outlook Email from C#
  • How to change the Outlook Features needed for this example
  • How to add the Outlook Reference to your Project
  • How to call Outlook and return the Inbox Folder with Emails

This is the end of this article series. Read Part 1 here

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!