Load unread email from outlook into VS C# datagrid with button click

0

I'm trying to create an application that with a button click will populate the data grid with my unread emails from outlook. When I click the "Refresh" button the emails wont load and I get an error message box. Why is this happening? How do I fix this?

Unable to cast COM object type 'System._ComObject' to interface type 'Microsoft.Office.Interop.Outlook.Mailitem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

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

        private void SendButton_Click(object sender, EventArgs e)
        {
            try
            {
                Outlook.Application app = new Outlook.Application();
                Outlook.MailItem mail = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
                mail.To = TotextBox.Text;
                mail.Subject = SubjecttextBox.Text;
                mail.Body = MessagetextBox.Text;
                mail.Importance = Outlook.OlImportance.olImportanceNormal;
                ((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);
            }
        }

        DataTable dt;
        private void Refreshbutton_Click(object sender, EventArgs e)
        {
            try
            {
                Outlook.Application app = new Outlook.Application();
                Outlook.NameSpace ns = app.GetNamespace("MAPI");
                Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                ns.SendAndReceive(true);
                dt = new DataTable("Inbox");
                dt.Columns.Add("From", typeof(string));
                dt.Columns.Add("Date", typeof(string));
                dt.Columns.Add("Body", typeof(string));
                dt.Columns.Add("AtchName", typeof(string));
                dt.Columns.Add("Format", typeof(string));
                dataGridView1.DataSource = dt;

                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);
            }
        }
    }
}
c#
datagrid
outlook
asked on Stack Overflow Aug 14, 2017 by hrutk1ta • edited Nov 8, 2018 by phuzi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0