首页 > 解决方案 > 如何查找 MailItem.Display() 发送的邮件

问题描述

我有以下要求:

我正在工作的拖放
准备重播电子邮件并将其显示给我也在工作的用户,使用以下代码:

MailItem mail = GetMailBySubject(dateReceived, subject);
if (mail != null)
{
    MailItem mailReply = mail.ReplyAll();
    // add text and stuff to mailReply...
    mailReply.Display();
}

这将在 Outlook 中打开一个窗口,就好像用户在 Outlook 中单击了回复一样。

现在我坚持第三个要求,
在用户发送回复电子邮件后,我需要以某种方式在 Outlook 中找到这封电子邮件以将其添加到我的数据网格中。

但我不知道如何做到这一点。
我所拥有的只是用于准备回复的原始邮件。
有没有办法只用这个找到回复,或者这可能是一个完全错误的方法?
更困难的是我必须显示回复电子邮件非模态,所以当用户在 Outlook 中点击发送时我没有触发。

作为参考,这里是 GetMailBySubject 的代码

private MailItem GetMailBySubject(DateTime dateReceived, string subject)
{
    MailItem Result = null;

    Microsoft.Office.Interop.Outlook.Application OutlookIns = new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook.NameSpace olNamespace = OutlookIns.GetNamespace("MAPI");
    MAPIFolder myInbox = olNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    Items items = myInbox.Items;
    int count = items.Count;
    MailItem mail = null;
    int i = 1; //DO NOT START ON 0

    while ((i < count) && (Result == null))
    {
        if (items[i] is MailItem)
        {
            mail = (MailItem)items[i];
            if ((mail.ReceivedTime.ToString("yyyyMMdd hh:mm:ss") == dateReceived.ToString("yyyyMMdd hh:mm:ss")) && (mail.Subject == subject))
            {
                Result = mail;
            }
        }
        i++;
    }

    return Result;
}

编辑
我按照建议尝试了这段代码,但Items.ItemAdd事件没有触发。
所以我一定还是做错了什么,但我看不到

这是我现在的代码

MailItem mail = GetMailBySubject((DateTime)sentOn, msg.Subject);
if (mail != null)
{
    MailItem mailReply = mail.ReplyAll();
    mailReply.HTMLBody = GetDefaultReplyText() + Environment.NewLine + mailReply.HTMLBody;

    Guid guid = Guid.NewGuid();
    UserProperties mailUserProperties = null;
    UserProperty mailUserProperty = null;

    mailUserProperties = mailReply.UserProperties;
    mailUserProperty = mailUserProperties.Add("myproperty", OlUserPropertyType.olText);
    mailUserProperty.Value = guid.ToString(); ;
    // the code below gives error "The property cannot be parsed or has an invalid format"
    //mailReply.PropertyAccessor.SetProperty("myproperty", guid.ToString());

    mailReply.Display();
}


private MailItem GetMailBySubject(DateTime dateReceived, string subject)
{
    MailItem Result = null;

    Microsoft.Office.Interop.Outlook.Application OutlookIns = new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook.NameSpace olNamespace = OutlookIns.GetNamespace("MAPI");
    MAPIFolder myInbox = olNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    Items items = myInbox.Items;
    int count = items.Count;
    MailItem mail = null;
    int i = 1; //DO NOT START ON 0

    while ((i < count) && (Result == null))
    {
        if (items[i] is MailItem)
        {
            mail = (MailItem)items[i];
            if ((mail.ReceivedTime.ToString("yyyyMMdd hh:mm:ss") == dateReceived.ToString("yyyyMMdd hh:mm:ss")) && (mail.Subject == subject))
            {
                Result = mail;
                MAPIFolder mySent = olNamespace.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
                mySent.Items.ItemAdd += Items_SentMailItemAdd;
            }
        }
        i++;
    }
    return Result;
}

最后

private void Items_SentMailItemAdd(object Item)
{
    //throw new NotImplementedException();
    ; // this event is never fired
}

标签: c#winformsemailoutlook

解决方案


您可以Items.ItemAdd在“已发送邮件”文件夹中使用该事件。要检查这是否是您的消息,请在您创建和显示的消息上设置自定义属性。您可以使用MailItem.UserProperties.Add,但这会强制以 TNEF 格式发送消息。为防止这种情况发生,您可以MailItem.PropertyAccessro.SetProperty在不使用集合的情况下设置命名的 MAPI 属性UserProperties。您可以设置测试用户属性并使用 OutlookSpy 查看其 DASL 名称(供 使用SetProperty)(选择消息,单击 IMessage 按钮,选择您的自定义属性,查看 DASL 编辑框)。


推荐阅读