c# - 如何查找 MailItem.Display() 发送的邮件
问题描述
我有以下要求:
- 允许用户将电子邮件从 Outlook 拖放到数据网格
- 准备回复邮件,并显示它以便用户查看和发送
- 发送后,也获取发送邮件并放入datagrid
我正在工作的拖放
准备重播电子邮件并将其显示给我也在工作的用户,使用以下代码:
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
}
解决方案
您可以Items.ItemAdd
在“已发送邮件”文件夹中使用该事件。要检查这是否是您的消息,请在您创建和显示的消息上设置自定义属性。您可以使用MailItem.UserProperties.Add
,但这会强制以 TNEF 格式发送消息。为防止这种情况发生,您可以MailItem.PropertyAccessro.SetProperty
在不使用集合的情况下设置命名的 MAPI 属性UserProperties
。您可以设置测试用户属性并使用 OutlookSpy 查看其 DASL 名称(供 使用SetProperty
)(选择消息,单击 IMessage 按钮,选择您的自定义属性,查看 DASL 编辑框)。
推荐阅读
- swift - 在 SwiftUI 中子类化 TextField 并将状态传递给它
- javascript - 有没有办法将 className 传递给 react-markdown 中的组件?
- python - 重复向函数传递多个参数
- django - 静态文件夹中的 css 文件不适用于网页
- html - 为什么在使用背景图片时找不到我的封面图片:url
- python - 将相似的嵌套列表分组为一个列表
- javascript - 如何修复我的 api 项目的 429 请求错误?
- node.js - 使用会话文件存储随时间增加 CPU 峰值
- variables - Azure DevOps 变量输出到另一个作业导致空值
- symfony - Symfony5 服务:自动装配参数不起作用