How do you write an Outlook add-in that displays the sender's email address in the message box every time a user views an item?

This is part of a larger project I'm working on, but for now I’m just trying to find a way to get the sender’s email address each time a user clicks on an email item in the inbox and its contents (the actual body of the email message) is displayed in the adjacent panel .

I tried writing code inside the ItemLoad event handler procedure, but even the MSDN website says that the Item object passed as an argument is not initialized by its properties, so calling (Item as MailItem) .SenderEmailAddress will not work.

Can someone tell me how to do this? (I am using Outlook 2007)

Below, by the way, does not work:

 public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { //this code runs applicationObject = (Outlook.Application)application; this.applicationObject.Startup += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_StartupEventHandler(applicationObject_Startup); } void applicationObject_Startup() { //this code runs this.applicationObject.Explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer); } void Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer) { //This code does not run Explorer.SelectionChange += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Explorer_SelectionChange); } void Explorer_SelectionChange() { //This code does not run //do something } 
+4
source share
2 answers

I found a way to do this:

  public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { applicationObject = (Outlook.Application)application; this.applicationObject.ActiveExplorer().SelectionChange += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Explorer_SelectionChange); } void Explorer_SelectionChange() { if (applicationObject.ActiveExplorer().Selection.Count == 1) { Outlook.MailItem item = applicationObject.ActiveExplorer().Selection[1] as Outlook.MailItem; if (item != null) { string address = item.SenderEmailAddress; //do something } } } 
0
source

I have not done this for a while, but I assume that you will need to use the SelectionChange event on the Explorer object that displays the main window.

In the Application.Startup event handler, you need to get Explorers and add a handler for the NewExplorer event , which will fire when the user opens a new window.

From there you can connect the SelectionChange event of the new Explorer object , and in the handler of this event you can get the selected elements using the Selection property . Then you can get the sender email address for each selected item.

0
source

Source: https://habr.com/ru/post/1446709/


All Articles