none
C# Getting sender's name blank! RRS feed

  • Question

  • Building VSTO 2010 using C#. I have a window inspector that gets called when the forward button is pressed in Outlook. I need to get the sender's name for that email when the forward button is pressed for that email. But I am getting a blank for the sender's name.

    Can someone help me and tell me what I am doing wrong? Thanks in advance.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
                olInspectors = this.Application.Inspectors;
                olInspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Forward_Message_Inspector);
    }
    void Forward_Message_Inspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
           Outlook.MailItem objMessage = Inspector.CurrentItem as Outlook.MailItem;
           MessageBox.Show(objMessage.SenderName);
    }

    The Message box is showing blank for the Sender Name!

     

    Tuesday, November 11, 2014 10:14 PM

Answers

  • The only place where you can retrieve that information is when the MailItem.Reply/ReplyAll/Forward events fire on the original message.

    You will need to track Explorer.SelectionChange on each explorer, for each item in the Explorer.Selection collection, set up event sinks to track the MailItem.Reply/ReplyAll/Forward events.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, November 12, 2014 2:03 PM
  • Hi POLZ,

    Based on the description, you want to get the sender of the origmal message after you forward it. We can get the sender via the mail item, here is the code for your reference:

    Public WithEvents myItem As Outlook.MailItem
    
    Public Sub Initialize_Handler()
        Set myItem = Application.ActiveInspector.CurrentItem
    End Sub
    
    Private Sub myItem_Forward(ByVal Forward As Object, Cancel As Boolean)
    MsgBox myItem.Sender
       
    End Sub
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 20, 2014 10:02 AM
    Moderator

All replies

  • All sender related properties are set only after the message is actually sent and moved to the Sent Items folder.

    Use the MailItem.SendUsingAccount property. If it is null, you can assume the default account will be used - use Namespace.Accounts collection. Given an instance of the Account object, you can use Account.CurrentUser to figure out the name and address.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, November 12, 2014 12:12 AM
  • Hi Dmitry,

    I did a bad job explaining my problem, sorry about that. Let me try again:

    Lets say I have an email From: Polz Dgl (polz_dgl@live.com). When I try to forward this email and click the forward button, I need to get the name (Polz Dgl) into a variable, before actually sending the email. Is that possible?

    Wednesday, November 12, 2014 1:07 PM
  • It looks like you are interested in the Sender* properties of the MailItem class.
    Wednesday, November 12, 2014 1:45 PM
  • The only place where you can retrieve that information is when the MailItem.Reply/ReplyAll/Forward events fire on the original message.

    You will need to track Explorer.SelectionChange on each explorer, for each item in the Explorer.Selection collection, set up event sinks to track the MailItem.Reply/ReplyAll/Forward events.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, November 12, 2014 2:03 PM
  • Hi POLZ,

    Based on the description, you want to get the sender of the origmal message after you forward it. We can get the sender via the mail item, here is the code for your reference:

    Public WithEvents myItem As Outlook.MailItem
    
    Public Sub Initialize_Handler()
        Set myItem = Application.ActiveInspector.CurrentItem
    End Sub
    
    Private Sub myItem_Forward(ByVal Forward As Object, Cancel As Boolean)
    MsgBox myItem.Sender
       
    End Sub
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 20, 2014 10:02 AM
    Moderator