Outlook添加投掷错误我不明白

时间:2015-06-08 16:57:31

标签: email outlook

我试图编写一个outlook-addin,用于从用户的收件箱中读取约会,并在会议开始前72小时生成提醒电子邮件。我通过拼凑来自SO和网络的片段来学习如何做到这一点,但我遇到了一些错误。我不确定我是否理解了甚至会问正确的问题,所以请原谅我的笨手笨脚。

所以,当我试图欺骗" Sender"使用下面的代码,我收到以下错误:

"Generating server: MBX080-W3-CO-1.exch080.serverpod.net
josh.alcorn@crmpoint.net
Remote Server returned '550 5.6.2 STOREDRV.Submit; subscription not found'
Original message headers:
Received: from MBX080-W3-CO-1.exch080.serverpod.net ([10.224.117.52]) by
 MBX080-W3-CO-1.exch080.serverpod.net ([169.254.1.30]) with mapi id
 15.00.1044.021; Mon, 8 Jun 2015 08:28:25 -0700
MIME-Version: 1.0
Content-Type: text/plain
Date: Mon, 8 Jun 2015 08:28:25 -0700
X-MS-Exchange-Transport-FromEntityHeader: Hosted
Message-ID:
    <b8690284ffb04af794a676b8efdee58d@MBX080-W3-CO-1.exch080.serverpod.net>
Subject: This is the subject"

当我删除发件人代码时,它似乎正在为我的两个测试约会正确触发,但在第三个上抛出nullreferenceexception。我似乎无法弄清楚如何将调试器与Outlook结合使用,因此我无法获得有关该错误的更好信息。至于这个问题,任何关于如何实时调试Outlook 2013插件的建议将非常受欢迎。我已经尝试了所有我熟悉的技巧。

using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;

//http://stackoverflow.com/questions/6425776/outlook-2010-how-to-get-a-list-of-all-appointments-including-recurrences
//https://www.add-in-express.com/creating-addins-blog/2013/06/10/outlook-calendar-appointment-meeting-items/
//http://www.sperrysoftware.com/outlook/email-reminders.asp
//myEmailAddress = this.ActiveExplorer().Session.CurrentUser.EmailAddress;
//Application.Session.CurrentUser.AddressEntry.Address
//https://www.add-in-express.com/creating-addins-blog/2013/06/10/outlook-calendar-appointment-meeting-items/#enumerate
//http://www.scrubly.com/blog/how-to-outlook/how-to-install-enable-and-disable-outlook-2013-add-ins/
//http://stackoverflow.com/questions/5472493/making-vsto-add-in-installable
//https://msdn.microsoft.com/en-us/library/cc442767.aspx#Download

namespace OutlookAddIn1
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        doStuff();
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    //https://msdn.microsoft.com/en-us/library/ms268866.aspx

    private void doStuff()
    {
        Thread.Sleep(120000); //120 seconds

        DateTime firstRun = DateTime.Now; //So we can check every 24 hours? Maybe once initially as well.
        DateTime lastRun = DateTime.Now;//.AddHours(1); //We're going to compare this to firstRun
        bool whileTrue = true;
        //int test = 0;
        try
        {
            while (whileTrue)
            {
                if (whileTrue == true)//(firstRun > lastRun.AddDays(1))
                {
                    Outlook.MAPIFolder calendarFolder =     Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalenda    r);
                    Outlook.Items outlookCalendarItems =        calendarFolder.Items;
                    outlookCalendarItems.IncludeRecurrences = false; //was true

                    List<Outlook.AppointmentItem> lst = new List<Outlook.AppointmentItem>();

                    foreach (Outlook.AppointmentItem item in outlookCalendarItems)
                    {
                        lst.Add(item);
                        //We can probably just handle logic in here without the second for loop that comes next
                    }

                    foreach (Outlook.AppointmentItem x in lst)
                    {
                        //http://stackoverflow.com/questions/16329581/getting-system-runtime-interopservices-comexception-error-in-server-side-generat
                        DateTime startDate = DateTime.Now.AddDays(1);
                        DateTime endDate = DateTime.Now.AddDays(5);
                        DateTime apptDate = x.Start;


                        if (x.Subject.ToLower().Contains("telos"))
                        {
                            MessageBox.Show("X: " + x.Start + "XYZ: " + x.Subject);

                            if (x.Start > startDate && x.Start < endDate)
                            {
                                Outlook.MailItem mailItem = (Outlook.MailItem)
                                    this.Application.CreateItem(Outlook.OlItemType.olMailItem);
                                //http://stackoverflow.com/questions/11223462/how-to-send-a-mail-using-microsoft-office-interop-outlook-mailitem-by-specifying
                                //Outlook.Recipient recipient = this.Application.Session.CreateRecipient("someone@example.com");
                                //mailItem.Sender = recipient.AddressEntry;
                                Outlook.Recipient recipTo =
                                    mailItem.Recipients.Add("someone@example.com");
                                recipTo.Type = (int)Outlook.OlMailRecipientType.olTo;
                                mailItem.Sender = (Outlook.AddressEntry)recipTo;
                                //Outlook.Account account = new Account();// Application.Session.Accounts["MyOtherAccount"];

                                //mailItem.SendUsingAccount = account;

                                mailItem.Subject = "This is the subject";
                                mailItem.To = Application.Session.CurrentUser.AddressEntry.Address; //"someone@example.com";
                                mailItem.Body = "This is the message.";
                                mailItem.Importance = Outlook.OlImportance.olImportanceLow;
                                mailItem.Display(false);
                                ((Outlook._MailItem)mailItem).Send();
                                //mailItem.Send();
                                /*
                                //Here we generate the email
                                Outlook.Application app = new Outlook.Application();
                                Microsoft.Office.Interop.Outlook.MailItem email = app.CreateItem((OlItemType.olMailItem));

                                Outlook.Recipient recipient = app.Session.CreateRecipient("someone@example.com");
                                email.Sender = recipient.AddressEntry;
                                email.Display(false);
                                email.Subject = "You have a new appointment";
                                email.To = Application.Session.CurrentUser.AddressEntry.Address; //Current email address.
                                email.Body = "This email was automatically generated to remind you have an upcoming appointment on: " + x.Start.ToString();
                                ((Outlook._MailItem)email).Send();
                                //email.Send();
                                //((Outlook._MailItem)mailItem).Send();
                                app.Quit();
                                 * */
                            }
                        }
                    }

                    lastRun = DateTime.Now;
                    whileTrue = false;
                }
                else
                {
                }
            }
        }
        catch (System.Exception e) //Microsoft.Office.Interop.Outlook.Exception e
        {
            MessageBox.Show(e.ToString());
        }



    }

    public void createContact()
    {
        Outlook.ContactItem newContact = (Outlook.ContactItem)
            this.Application.CreateItem(Outlook.OlItemType.olContactItem);
        try
        {
            newContact.FirstName = "Person";
            newContact.LastName = "LastName";
            newContact.Email1Address = "someone@example.com";
            newContact.Save();
            newContact.Display(true);
        }
        catch
        {
            MessageBox.Show("The new contact was not saved.");
        }
    }


    #endregion
}
}

1 个答案:

答案 0 :(得分:0)

在Startup事件处理程序中运行一个耗时的任务是个好主意。事实是,Outlook会测量加载加载项所需的时间,并可能会自动禁用它(如果需要花费很多时间)。有关详细信息,请参阅MSDN中Performance criteria for keeping add-ins enabled文章中的What's new for Outlook 2013 developers部分。

另外,我建议创建一个日志文件并在日志中写入任何操作。因此,您将了解运行时会发生什么以及哪行代码会产生错误。