user16004320
user16004320

Reputation:

VSTO outlook add-in - Retrieve sender's name for reply

I am going through the walkthrough on how to create your first outlook addin using the ribbon designer: https://learn.microsoft.com/en-us/visualstudio/vsto/walkthrough-creating-your-first-vsto-add-in-for-outlook?view=vs-2019

My goal is to make reply templates that includes the sender's name in the greeting.

From researching the issue, I believe I need either MailItem.SenderEmailAddress or MailItem.Sender but when I tried to add it to the mailitem.htmlbody it doesn't show anything at all.

Here is the code I have for my test.

 private void Teser_button2_Click(object sender, RibbonControlEventArgs e)
        {
            {
                if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
                {
                    MailItem mi = Globals.ThisAddIn.Application.ActiveExplorer().ActiveInlineResponse;
                    if (mi != null)
                    {

                        mi.HTMLBody = mi.SenderEmailAddress + @"<BODY style=font-size:11pt;font-family:Calibri></BODY>Hello ENTERNAMEHERE,
<br><
<br>Here is a brief summary on everything worked on:
<br>
<br>Thank you for your time,
<br>
" + mi.HTMLBody;
                        mi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
                    }
                }
            }
        }

I tried changing mi.SenderEmailAddress to mi.Sender but I still get nothing.

Any ideas are appreciated!!

Updated

Here is code that worked for me for both the outlook explorer, and from a popup (Its not the best but it works):

 private void Domain_setup_Click_1(object sender, RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
            {
                MailItem mi = Globals.ThisAddIn.Application.ActiveExplorer().ActiveInlineResponse;
                Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
                object item = selection[1];
                if (mi != null && item is Outlook.MailItem mailItem)
                {
                    string senderName = mailItem.SenderName;
                    mi.HTMLBody = @"<BODY style=font-size:11pt;font-family:Calibri></BODY>Hello " + senderName + @",
<br>
<br>Here is the information for the new domain/user account:
<br>
<br>Computer username: ENTERDOMAINORUSERNAMEHERE
<br>Computer password: ENTERPASSWORD
<br>
<br>Thank you for your time,
<br>
" + mi.HTMLBody;
                }
            }
            if (Globals.ThisAddIn.Application.ActiveInspector() != null)
            {
                MailItem mi = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
                Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
                object item = selection[1];
                if (mi != null && item is Outlook.MailItem mailItem)
                {
                    string senderName = mailItem.SenderName;
                    mi.HTMLBody = @"<BODY style=font-size:11pt;font-family:Calibri></BODY>Hello " + senderName + @",
<br>
<br>Here is the information for the new domain/user account:
<br>
<br>Computer username: ENTERDOMAINORUSERNAMEHERE
<br>Computer password: ENTERPASSWORD
<br>
<br>Thank you for your time,
<br>
" + mi.HTMLBody;
                }
            }
        }

Upvotes: 1

Views: 420

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

You are accessing the sender name on the reply, not the message being replied to. The name should come from the first item in the Globals.ThisAddIn.Application.ActiveExplorer().Selection collection.

Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
if (selection.Count > 0)
{
  object item = selection[1];
  if (item is Outlook.MailItem mailItem)
  {
    string senderName = mailItem.SenderName;
  }
}

Also, you cannot and should not concatenate two HTML strings - the result will not be a valid HTML document, the two must be merged (e.g. you can insert your HTML after the end of the <body> tag in mi.HTMLBody).

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

The sender-related properties exist on the received items. So, what you actually need is to get the source item being replied to. To avoid unnecessary searching for the original email you can use several ways to retrieve the required information in Outlook:

  1. You can hook up to the Reply event of Outlook items, so when a user chooses to reply to an email you will get all the required information and paste to the message body. You may find the Implement a wrapper for inspectors and track item-level events in each inspector article helpful. An instance of the response object is passed as a parameter, so you can modify the message body with the sender-related information extracted from the event source.

  2. If the item is saved and was sent/received, you can use the GetConversation method to obtain a Conversation object that represents the conversation to which this item belongs. A conversation represents one or more items in one or more folders and stores. If you move an item in a conversation to the Deleted Items folder and subsequently enumerate the conversation by using the GetChildren, GetRootItems, or GetTable method, the item will not be included in the returned object.

Upvotes: 0

Related Questions