How to get email id when clicking on Outlook Desktop custom button c# Visual Studio
Ryan Brown
0
Reputation points
I want to do something really simple but can't find out how to do it. I want a custom button in the ribbon menu of an email item in Outlook desktop (and web also) and when clicked it get the email id and then I post that to my custom API. My api then downloads the email and goes from there.
- I created an Outlook VSTO Add-in Project in Visual Studio 2022.
- I then created a Ribbon with a button on it.
- I created the click event by double-clicking on my button and then I run the code. The click event signature is private void btnTrackInCollections_Click(object sender, RibbonControlEventArgs e)
- The closest I have gotten is the below code, but the entry id is not in the format I am expecting so I don't know what Id it is. ID's I am used to start with something like "AAMkAD" but the entry id was shorter and also all numeric digits.
// Get the application object
Application application = Globals.ThisAddIn.Application;
// Get the active window
var activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
Microsoft.Office.Interop.Outlook.Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();
if (explorer != null)
{
// Get the selected items
var selection = explorer.Selection;
// Iterate through the selection to find the email
foreach (object item in selection)
{
// Check if the item is a MailItem (email)
if (item is Microsoft.Office.Interop.Outlook.MailItem mailItem)
{
// You have the email object
string subject = mailItem.Subject;
string body = mailItem.Body;
string id = mailItem.ConversationID;
string id1 = mailItem.EntryID;
string id2 = mailItem.ReceivedByEntryID;
string id3 = mailItem.ReceivedOnBehalfOfEntryID;
break; // Exit the loop after finding the first email
}
}
}
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,366 questions