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.
What I am trying to do
Put simply, I am writing an Outlook plugin which hooks into the "ItemSend" event and runs a function to display the senders email (SMTP) address when they click "Send" on an email.
The issue
I am unable to get the SMTP address when an email is sent from an Exchange mailbox. Instead, mail.SenderEmailAddresss
gives an X400 address, other methods I have found either give an exception, or don't return an email address at all.
GetSenderSMTPAddress(mail)
gives blank output
mail.SenderEmailAddresss
results in: /o=Company Organisation/ou=Exchange Administrative Group (ABC123T)/cn=Recipients/cn=abc123-
mail.Sender.Address
resulted in Exception thrown: 'System.NullReferenceException' in OutlookTesting.dll
The code I have at the moment
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("Using GetSenderSMTPAddress function: " + GetSenderSMTPAddress(mail));
Debug.WriteLine("Using mail.SenderEmailAddresss: " + mail.SenderEmailAddress);
Debug.WriteLine("Using mail.Sender.Address: " + mail.Sender.Address);
}
}
private string GetSenderSMTPAddress(Outlook.MailItem mail)
{
string PR_SMTP_ADDRESS =
@"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
if (mail == null)
{
throw new ArgumentNullException();
}
if (mail.SenderEmailType == "EX")
{
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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
}
#endregion
}
}
Other things I have tried
I tried the solution here: https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-get-the-smtp-address-of-the-sender-of-a-mail-item. This didn't work, and instead gave a NullReferenceException.
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.
Closing thoughts
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.
Environment.Username
to obtain the user name of the logged on individual and then using this information to get the sender email address from Exchange? - joeschwa