[英]How to add an event handler for reading or opening any mail in Outlook? VSTO
The idea of my plugin is that when I click (read) or double click (open) on any email in my Outlook explorer inbox, a windows form is displayed.我的插件的想法是,当我在 Outlook 资源管理器收件箱中单击(阅读)或双击(打开)任何电子邮件时,会显示一个 Windows 窗体。 I understand that I must put the event handler in the ThisAddIn_Startup method to make it work while Outlook is open.
我知道我必须将事件处理程序放在 ThisAddIn_Startup 方法中才能使其在 Outlook 打开时工作。 I have tried the following:
我尝试了以下方法:
Outlook.MailItem mailItem = new Outlook.MailItem();
mailItem.Open += new Microsoft.Office.Interop.Outlook.ItemEvents_10_OpenEventHandler(ClickSobreCorreo);
mailItem.Read += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReadEventHandler(ClickSobreCorreo);
Outlook.AppointmentItem Cita = new Outlook.AppointmentItem();
Cita.Open += new Microsoft.Office.Interop.Outlook.ItemEvents_10_OpenEventHandler(ClickSobreCorreo);
Cita.Read += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReadEventHandler(ClickSobreCorreo);
Outlook.MeetingItem Reunion = new Outlook.MeetingItem();
Reunion.Open += new Microsoft.Office.Interop.Outlook.ItemEvents_10_OpenEventHandler(ClickSobreCorreo);
Reunion.Read += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReadEventHandler(ClickSobreCorreo);
But I get an error:但我收到一个错误:
System.Runtime.InteropServices.COMException (0x80040154): Class not registered (Exception from HRESULT: 0x80040154
System.Runtime.InteropServices.COMException (0x80040154): 类未注册(来自 HRESULT 的异常:0x80040154
Then I tried this that I found on the web but only a random mail works (it is not even the first mail) and the read event only executes once and no more.然后我尝试了我在网上找到的这个,但只有随机邮件有效(它甚至不是第一封邮件)并且读取事件只执行一次,不再执行。
Outlook.MailItem mailItem;
private void ThisApplication_Startup (object sender, EventArgs e) {
Outlook.NameSpace ns = this.Session;
Outlook.MAPIFolder inbox =
ns.GetDefaultFolder (
Outlook.OlDefaultFolders.olFolderInbox);
foreach (object o in inbox.Items) {
mailItem = o as Outlook.MailItem;
if (mailItem != null) {
break;
}
}
if (mailItem == null) {
MessageBox.Show ("Couldn't find a mail item to connect to.");
} else {
MessageBox.Show (String.Format (
"Connected to the mail item with subject {0}.",
mailItem.Subject);
mailItem.Read += new Outlook.ItemEvents_10_ReadEventHandler (
MailItem_Read);
mailItem.Open += new Outlook.ItemEvents_10_OpenEventHandler (
MailItem_Open);
mailItem.Write += new Outlook.ItemEvents_10_WriteEventHandler (
MailItem_Write);
((Outlook.ItemEvents_10_Event) mailItem).Close += new Outlook.ItemEvents_10_CloseEventHandler (
MailItem_Close);
}
}
}
void MailItem_Read() {
MessageBox.Show("Read");
}
void MailItem_Open(ref bool cancel) {
MessageBox.Show("Open");
}
void MailItem_Write(ref bool cancel) {
MessageBox.Show("Write");
}
void MailItem_Close(ref bool cancel) {
MessageBox.Show("Close");
}
What should I do?我该怎么办? Do I have to go through all the emails and assign the event handler to them?
我是否必须浏览所有电子邮件并将事件处理程序分配给它们? If so, how would you do with the incoming emails, should I add an event handler for the incoming emails and in the method add the event handler to open and read the email?
如果是这样,您将如何处理传入的电子邮件,我是否应该为传入的电子邮件添加一个事件处理程序,并在方法中添加事件处理程序以打开和阅读电子邮件?
Do not use " new
" with Outlook.MailItem
- that object is not creatable, Outlook.Application
is, but you get an instance of that object (the trusted version) from Outlook anyway.不要对
Outlook.MailItem
使用“ new
”——该对象不可创建, Outlook.Application
是可创建的,但无论如何您都可以从 Outlook 获得该对象的实例(受信任的版本)。
What I suspect you need to to track the Exploer.SelectionChange
event ( Explorer
can be received from Applica5tion.ActiveExplorer
and Application.Explorers.NewExplorer
) and set up event handlers on the selected items (Explorer.Selection).我怀疑您需要跟踪
Exploer.SelectionChange
事件(可以从Applica5tion.ActiveExplorer
和Application.Explorers.NewExplorer
接收Explorer
)并在所选项目(Explorer.Selection)上设置事件处理程序。
Make sure to keep the objects raising the events ( Explorer
and Explorers
) referenced so they are not garbage collected.确保引用引发事件(
Explorer
和Explorers
)的对象,以便它们不会被垃圾收集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.