none
POP Outlook RRS feed

  • Question

  • 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 webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {

            }

            private void btnSend_Click(object sender, EventArgs e)
            {
                try
                {
                    Outlook._Application _app = new Outlook.Application();
                    Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
                    mail.To = TextTo.Text;
                    mail.Subject = TextSubjet.Text;
                    mail.Body = TextMessage.Text;
                    mail.Importance = Outlook.OlImportance.olImportanceNormal;
                    ((Outlook._MailItem)mail).Send();
                    MessageBox.Show("Sucesso","Message",MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex) {

                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            DataTable dt;
            private void btnRecieve_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("Subject", typeof(string));
                    dt.Columns.Add("Sender", typeof(string));
                    dt.Columns.Add("Body", typeof(string));
                    dt.Columns.Add("Date", typeof(string));
                    dataGridView.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); }
            }

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

    Erro:

    Não é possível associar o objecto COM do tipo 'System.__ComObject' ao tipo de interface 'Microsoft.Office.Interop.Outlook.MailItem'. Esta operação falhou porque a chamada QueryInterface no componente COM para a interface com o IID '{00063034-0000-0000-C000-000000000046}' falhou com o seguinte erro: Esta interface não é suportada (Excepção de HRESULT: 0x80004002 (E_NOINTERFACE)).

    Friday, February 26, 2016 12:00 PM

Answers

  • Hello,

    I suppose the following line of code fires the exception specified:

     foreach (Outlook.MailItem item in inbox.Items)

    Be aware, a folder can contain different type of objects: PostItem, NoteItem, DocumentItem and etc. So, you need to check the underlying type first. You can use the is operator available in C#:

    if (selObject is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem =
                        (selObject as Outlook.MailItem);
                    itemMessage = "The item is an e-mail message." +
                        " The subject is " + mailItem.Subject + ".";
                    mailItem.Display(false);
                }
                else if (selObject is Outlook.ContactItem)
                {
                    Outlook.ContactItem contactItem =
                        (selObject as Outlook.ContactItem);
                    itemMessage = "The item is a contact." +
                        " The full name is " + contactItem.Subject + ".";
                    contactItem.Display(false);
                }
                else if (selObject is Outlook.AppointmentItem)
                {
                    Outlook.AppointmentItem apptItem =
                        (selObject as Outlook.AppointmentItem);
                    itemMessage = "The item is an appointment." +
                        " The subject is " + apptItem.Subject + ".";
                }
                else if (selObject is Outlook.TaskItem)
                {
                    Outlook.TaskItem taskItem =
                        (selObject as Outlook.TaskItem);
                    itemMessage = "The item is a task. The body is "
                        + taskItem.Body + ".";
                }
                else if (selObject is Outlook.MeetingItem)
                {
                    Outlook.MeetingItem meetingItem =
                        (selObject as Outlook.MeetingItem);
                    itemMessage = "The item is a meeting item. " +
                         "The subject is " + meetingItem.Subject + ".";
                }

    Read more about that in the How to: Programmatically Determine the Current Outlook Item article.

    Saturday, February 27, 2016 2:06 PM