How to get email id when clicking on Outlook Desktop custom button c# Visual Studio

Ryan Brown 0 Reputation points
2025-05-27T19:53:28.4833333+00:00

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.

  1. I created an Outlook VSTO Add-in Project in Visual Studio 2022.
  2. I then created a Ribbon with a button on it.
  3. 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)
  4. 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
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hornblower409-4652 315 Reputation points
    2025-05-28T04:09:45.2433333+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.