c#Outlook加载项获取电子邮件位于“已发送邮箱”后的“发送日期”

时间:2016-07-18 13:23:45

标签: c# email outlook outlook-addin

我出于业务目的制作了Outlook加载项(Outlook 2013和2016 VSTO加载项),以便将电子邮件详细信息保存到我们的数据库中。加载项在组成新电子邮件时启动,但在发送电子邮件时关闭。

电子邮件的发送日期仅在电子邮件移至“已发送”邮箱后添加。是否有一种方法可以使用我当前的加载项(或其他加载项)来关闭它后发送日期,而不让用户等待它被移动到发送的邮箱?

我知道它可以在VBA中轻松完成,但我希望最好使用一个外接程序,以便可以使用Exchange服务器轻松加载到所有用户。

2 个答案:

答案 0 :(得分:0)

那个日期不是今天的日期/时间(现在)或接近它的日期吗?

您可以在COM插件中执行VBA中所做的一切 - 订阅“已发送邮件”文件夹中的Items.ItemAdd事件,并检索事件触发时的日期。

答案 1 :(得分:0)

感谢德米特里的回复。它让我走上了正确的道路。将新项目添加到“已发送”邮箱时,我使用现有的VSTO添加项进行了触发。我不知道在“ThisAddIn_Startup”方法中插入它时重新激活加载项,现在这是有意义的。

我关注this示例。

这是我的代码:

Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder Sent_items;
Outlook.Items items;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    outlookNameSpace = this.Application.GetNamespace("MAPI");
    Sent_items = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);

    items = Sent_items.Items;
    items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}

void items_ItemAdd(object Item)
{
    Outlook.MailItem mail = (Outlook.MailItem)Item;       

    string strMailItemNumber_filter = mail.UserProperties["MailItemNumber"].Value;

    if ((Item != null) && (!string.IsNullOrWhiteSpace(strMailItemNumber_filter)))
    {
        if (mail.MessageClass == "IPM.Note" &&
                   mail.UserProperties["MailItemNumber"].Value.ToUpper().Contains(strMailItemNumber_filter.ToUpper())) //Instead of subject use other mail property
        {
            //Write 'Sent date' to DB
            System.Windows.Forms.MessageBox.Show("Sent date is: "+ mail.SentOn.ToString()+ " MailNr = "+strMailItemNumber_filter);

        }
    }

}

我必须创建一个新的邮件用户定义属性,以匹配我发送的电子邮件,以便在发送的邮箱中找到正确的电子邮件:

        private void AddUserProperty(Outlook.MailItem mail)
    {
        Outlook.UserProperties mailUserProperties = null;
        Outlook.UserProperty mailUserProperty = null;
        try
        {
            mailUserProperties = mail.UserProperties;
            mailUserProperty = mailUserProperties.Add("MailItemNrProperty", Outlook.OlUserPropertyType.olText, false, 1);
            // Where 1 is OlFormatText (introduced in Outlook 2007)
            mail.UserProperties["MailItemNumber"].Value = "Any value as string...";
            mail.Save();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        finally
        {
            if (mailUserProperty != null) Marshal.ReleaseComObject(mailUserProperty);
            if (mailUserProperties != null) Marshal.ReleaseComObject(mailUserProperties);
        }
    }