How to Send and Receive email in Microsoft Outlook using C#

By FoxLearn 8/25/2024 3:20:25 AM   13.51K
To send and receive emails in C# using Microsoft Outlook, you can use the Microsoft.Office.Interop.Outlook library, which provides a way to interact with Outlook programmatically.

How to Send and Receive email in C# using Microsoft Outlook

Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "OutlookMail" and then click OK

Drag and drop the Label, TextBox, DataGridView, WebBrower, Button controls from the Visual Studio Toolbox onto your form designer, then you can layout your form as shown below.

c# send mail outlook

Ensure that Microsoft Outlook is installed on your system.

Add a reference to the Microsoft Outlook XX.0 Object Library in your C# project. You can do this through:

  • Right-click on your project in Solution Explorer.
  • Select Add > Reference.
  • Go to the COM tab.
  • Search for Microsoft Outlook XX.0 Object Library and add it.

Here's a simple example of how to send an email using the Microsoft.Office.Interop.Outlook namespace.

using System;
using System.Data;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookMail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //Using COM to send email
                // Create a new instance of Outlook application
                Outlook._Application _app = new Outlook.Application();
                // Create a new mail item
                Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
                // Set the properties of the mail item
                mail.To = txtTo.Text;
                mail.Subject = txtSubject.Text;
                mail.Body = txtMessage.Text;
                mail.Importance = Outlook.OlImportance.olImportanceNormal;
                // Send the email
                ((Outlook._MailItem)mail).Send();
                MessageBox.Show("Your message has been successfully sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

Receiving emails is more complex, as it involves accessing the Outlook inbox and processing the items. Here’s a basic example of how to access and read emails from the inbox.

DataTable dt;
private void btnReceive_Click(object sender, EventArgs e)
{
    try
    {
        // Create a new instance of Outlook application
        Outlook._Application _app = new Outlook.Application();
        // Get the MAPI namespace
        Outlook._NameSpace _ns = _app.GetNamespace("MAPI");
        // Get the inbox folder
        Outlook.MAPIFolder inbox = _ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
        _ns.SendAndReceive(true);
        dt = new DataTable("Inbox");
        dt.Columns.Add("Subject", typeof(string));
        dt.Columns.Add("Sender", typeof(string));
        dt.Columns.Add("Body", typeof(string));
        dt.Columns.Add("Date", typeof(string));
        dataGrid.DataSource = dt;
        // Get the items in the inbox, Iterate through the items
        foreach (Outlook.MailItem item in inbox.Items)
            dt.Rows.Add(new object[] { item.Subject, item.SenderName, item.HTMLBody, item.SentOn.ToLongDateString() + " " + item.SentOn.ToLongTimeString() });
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //Display email as html
    if (e.RowIndex < dt.Rows.Count && e.RowIndex >= 0)
        webBrowser.DocumentText = dt.Rows[e.RowIndex]["Body"].ToString();
}

VIDEO TUTORIAL