title: C# VSTO Outlook plugin - How can I get the email address of the sender of an outgoing email using Exchange?
body: <p>There are a few questions on this already, but I am facing a different issue. The solutions posted on other questions didn't work for me, I suspect the reason is that I am trying to get the senders email address of an outgoing email, rather than one that has been sent from someone else and is sitting in a mail folder.</p>
  <h3>What I am trying to do</h3>
  <p>Put simply, I am writing an Outlook plugin which hooks into the &quot;ItemSend&quot; event and runs a function to display the senders email (SMTP) address when they click &quot;Send&quot; on an email.</p>
  <h3>The issue</h3>
  <p>I am unable to get the SMTP address when an email is sent from an Exchange mailbox. Instead, <code>mail.SenderEmailAddresss</code> gives an X400 address, other methods I have found either give an exception, or don't return an email address at all.</p>
  <p><code>GetSenderSMTPAddress(mail)</code> gives blank output</p>
  <p><code>mail.SenderEmailAddresss</code> results in: <code>/o=Company Organisation/ou=Exchange Administrative Group (ABC123T)/cn=Recipients/cn=abc123-</code></p>
  <p><code>mail.Sender.Address</code> resulted in <code>Exception thrown: 'System.NullReferenceException' in OutlookTesting.dll</code></p>
  <h3>The code I have at the moment</h3>
  <pre><code>namespace OutlookTesting
  {
      public partial class ThisAddIn
      {
          private void ThisAddIn_Startup(object sender, System.EventArgs e)
          {
              this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
          }
  
          void Application_ItemSend(object Item, ref bool Cancel)
          {
              if (Item is Outlook.MailItem)
              {
                  Outlook.MailItem mail = (Outlook.MailItem)Item;
                  Debug.WriteLine(&quot;Using GetSenderSMTPAddress function: &quot; + GetSenderSMTPAddress(mail));
                  Debug.WriteLine(&quot;Using mail.SenderEmailAddresss: &quot; + mail.SenderEmailAddress);
                  Debug.WriteLine(&quot;Using mail.Sender.Address: &quot; + mail.Sender.Address);
              }
          }
  
          private string GetSenderSMTPAddress(Outlook.MailItem mail)
          {
              string PR_SMTP_ADDRESS =
                  @&quot;http://schemas.microsoft.com/mapi/proptag/0x39FE001E&quot;;
              if (mail == null)
              {
                  throw new ArgumentNullException();
              }
              if (mail.SenderEmailType == &quot;EX&quot;)
              {
                  Outlook.AddressEntry sender = mail.Sender;
                  if (sender != null)
                  {
                      //Now we have an AddressEntry representing the Sender
                      if (sender.AddressEntryUserType ==
                          Outlook.OlAddressEntryUserType.
                          olExchangeUserAddressEntry
                          || sender.AddressEntryUserType ==
                          Outlook.OlAddressEntryUserType.
                          olExchangeRemoteUserAddressEntry)
                      {
                          //Use the ExchangeUser object PrimarySMTPAddress
                          Outlook.ExchangeUser exchUser =
                              sender.GetExchangeUser();
                          if (exchUser != null)
                          {
                              return exchUser.PrimarySmtpAddress;
                          }
                          else
                          {
                              return null;
                          }
                      }
                      else
                      {
                          return sender.PropertyAccessor.GetProperty(
                              PR_SMTP_ADDRESS) as string;
                      }
                  }
                  else
                  {
                      return null;
                  }
              }
              else
              {
                  return mail.SenderEmailAddress;
              }
          }
          #region VSTO generated code
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InternalStartup()
          {
              this.Startup += new System.EventHandler(ThisAddIn_Startup);
          }
  
          #endregion
      }
  }
  </code></pre>
  <h3>Other things I have tried</h3>
  <p>I tried the solution here: <a href="https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-get-the-smtp-address-of-the-sender-of-a-mail-item" rel="noreferrer">https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-get-the-smtp-address-of-the-sender-of-a-mail-item</a>. This didn't work, and instead gave a NullReferenceException.</p>
  <p>I did also fall back to assuming the default mailbox was the senders email address, but this is a bad idea for those with multiple mailboxes.</p>
  <h3>Closing thoughts</h3>
  <p>I'm thinking that the only solution (other than a 3rd party plugin) would be to loop through each mailbox on the account, grab the X400 address (if exchange mailboxes) and put in an array with the email address. When an email is being sent, grab the X400 from the outgoing email, match it to the account's X400 and then I will have the email address. Not sure if this is possible or even a decent solution just yet though.</p>