【问题标题】:c# Outlook Add-in get Sent date after email is in Sent mailboxc# Outlook Add-in 获取电子邮件在已发送邮箱后的发送日期
【发布时间】:2016-07-19 17:36:29
【问题描述】:
出于商业目的,我制作了一个 Outlook 插件(Outlook 2013 和 2016 VSTO 插件),以将电子邮件详细信息保存到我们的数据库中。该加载项在撰写新电子邮件时启动,但在发送电子邮件时关闭。
电子邮件的发送日期仅在电子邮件移动到已发送邮箱后添加。有没有办法使用我当前的加载项(或其他加载项)在关闭后获取该发送日期,而无需让用户等待它被移动到已发送邮箱?
我知道它可以在 VBA 中轻松完成,但我希望最好使用插件,以便可以轻松地将其加载给所有使用 Exchange 服务器的用户。
【问题讨论】:
标签:
c#
email
outlook
outlook-addin
【解决方案1】:
该日期不会是今天的日期/时间(现在)或接近它的日期吗?
您可以在 VBA 中执行的所有操作都可以在 COM 插件中执行 - 订阅 Sent Items 文件夹中的 Items.ItemAdd 事件并检索事件触发的日期。
【解决方案2】:
感谢 Dmitry 的回复。它让我走上了正确的道路。当新项目添加到已发送邮箱时,我使用现有的 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);
}
}