3

谁能告诉我为什么会出现以下错误?谷歌并没有提供太多帮助。抱歉,这有点模糊。该代码的目的是将所有未读邮件从 Outlook 中取出并放入 C# 表单。

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;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

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

        private void button1_Click(object sender, EventArgs e)
        {
            Outlook.MAPIFolder inbox = 
                this.Application.ActiveExplorer().Session.GetDefaultFolder
                (Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Items unreadItems = inbox.Items.Restrict("[Unread]=true");

            MessageBox.Show(
                string.Format("Unread items in Inbox = {0}", unreadItems.Count));
        }
    }
}

错误:“TestEmailGetter.Form1”不包含“Application”的定义,并且找不到接受“TestEmailGetter.Form1”类型的第一个参数的扩展方法“Application”(您是否缺少 using 指令或程序集引用?

此行发生错误:

this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

我不知道要添加什么来修复它。:-(

谢谢你的帮助!

4

2 回答 2

0

你的Form1班级还没有成员Application。如果要从 namespacethis引用类的静态成员,则必须不使用它。ApplicationMicrosoft.Office.Interop.Outlook

更新

使用Outlook.Application...

于 2013-09-16T21:55:40.190 回答
0

要修复错误,请执行以下操作。您必须先创建一个 OutlookApplication 对象,然后才能在独立应用程序中使用它。

Outlook.Application OutlookApplication = new Outlook.Application();
Outlook.Folder calFolder =
   OutlookApplication.Session.GetDefaultFolder(
       Outlook.OlDefaultFolders.olFolderCalendar)
       as Outlook.Folder;  
于 2016-03-25T18:47:09.190 回答