Outlook:对象引用未设置为对象的实例

时间:2011-10-21 19:05:11

标签: .net outlook vsto object-reference

我有一个outlook插件(基本推送邮件到我们的应用程序),它在一台机器上返回一个问题(在其他机器上正常工作)。它只是没有错误,所以我们进入SYSTEM菜单并将VSTO_SUPPRESSDISPLAYALERTS = 0转到屏幕。

当我然后运行同步时,我得到以下

Object reference not set to an instance of an object.


************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at MyAppOutlook.WaitingPopup.btnSyncOutlookInboxMail_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam,     IntPtr lparam)

我无法理解为什么它适用于大多数机器但不适用于此机器。该插件安装并显示在该机器的工具栏上,就像其他机器一样,它只是不起作用!

在安装插件之前,我已经检查并重新安装了以下内容:

  • .net3.5框架
  • 2007年主要互操作程序集

n.b。展望是2007年。

根据要求,该流程的代码如下

Outlook.MAPIFolder outlookSentFolder =     (Outlook.MAPIFolder)outlookObj.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);

for (int i = 1; i <= outlookSentFolder.Items.Count; i++)
{
    Outlook.MailItem mm = outlookSentFolder.Items[i] as Outlook.MailItem;
    if (mm != null)
    {
        try
        {
            DateTime StartDate = new DateTime(((Outlook.MailItem)outlookSentFolder.Items[i]).SentOn.Year, ((Outlook.MailItem)outlookSentFolder.Items[i]).SentOn.Month, ((Outlook.MailItem)outlookSentFolder.Items[i]).SentOn.Day, ((Outlook.MailItem)outlookSentFolder.Items[i]).SentOn.Hour, ((Outlook.MailItem)outlookSentFolder.Items[i]).SentOn.Minute, 0);

            if (StartDate > currentDate) // condition for current month mail sync
            {
                string attachs = "";
                for (int j = 0; j < ((Outlook.MailItem)outlookSentFolder.Items[i]).Attachments.Count; j++)
                {
                    attachs = ((Outlook.MailItem)outlookSentFolder.Items[i]).Attachments[j + 1].FileName.ToString() + "," + attachs;
                }

                ASCIIEncoding encoding = new ASCIIEncoding();
                string postData = "userDBName=" + subsDBName;
                postData += ("|MethodRequestName=addUpdateInboxMailFromOutlookToCRM");
                postData += ("|loginid=" + username);
                postData += ("|MailTo=" + ((Outlook.MailItem)outlookSentFolder.Items[i]).To);
                postData += ("|MailCC=" + ((Outlook.MailItem)outlookSentFolder.Items[i]).CC);
                postData += ("|MailFrom=" + ((Outlook.MailItem)outlookSentFolder.Items[i]).SenderEmailAddress);
                postData += ("|MailSubject=" + ((((((Outlook.MailItem)outlookSentFolder.Items[i]).Subject).Replace("|", "")).Replace("#", "")).Replace("<", "")).Replace(">", ""));
                postData += ("|MailBody=" + ((((((Outlook.MailItem)outlookSentFolder.Items[i]).Body).Replace("|", "")).Replace("#", "")).Replace("<", "")).Replace(">", ""));
                postData += ("|MailSentOn=" + StartDate.ToString("MM/dd/yyyy hh:mm:ss"));//((Outlook.MailItem)outlookSentFolder.Items[i]).SentOn.ToString());
                postData += ("|MailType=Sent");
                postData += ("|MailAttach=" + attachs);
                byte[] data = encoding.GetBytes(postData);

                // Prepare web request...
                HttpWebRequest myRequestSent = (HttpWebRequest)WebRequest.Create(reqURL + "Views/Subscription/OutlookDataService.aspx");
                myRequestSent.Method = "POST";
                myRequestSent.ContentType = "application/x-www-form-urlencoded";
                myRequestSent.ContentLength = data.Length;
                Stream newStream = myRequestSent.GetRequestStream();
                // Send the data.
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                WebResponse responseHtmlSent = myRequestSent.GetResponse();
                string xmlString = "";
                using (StreamReader r = new StreamReader(responseHtmlSent.GetResponseStream()))
                {
                    xmlString = xmlString + r.ReadToEnd();
                }
                responseHtmlSent.Close();
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Warning: " + ex.Message.ToString(), "Warning");
        }
    }
}

请大家帮忙,因为我们设法得到这个错误?但是,我的理解是这个错误通常意味着我们错误地引用了表,但是为什么它会在大多数其他机器上工作?

由于

1 个答案:

答案 0 :(得分:0)

您是否在处理此问题时将try ... catch块添加到代码中,或者它一直存在?

如果它一直存在并且你提到它只是没有工作没有错误那么errro必须在此之外。我会检查受影响的用户是否确实有一个已发送的邮件文件夹。