2
votes

In VSTO outlook 2013 addin I use this method to get the email address of the sender:

Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
Object currentItem = currentObject.CurrentItem;
MailItem message = currentItem as MailItem;
string senderMailAddress = message.SenderEmailAddress;

This always works great except in one case. I get a string that is their email address. But if the sender is in the same domain as the person using the addin, I get a string that is a bunch of random characters, backslashes, and the email address without the domain in all caps.

Example: If the sender is [email protected] and the person using addin is also [email protected] (same domain)

I'll get something like: 7GXaaJD\3x5FDd\PERSON rather than the normal email address string.

I read somewhere in the docs (I can't seem to find it again) that when the sender is in the same domain, it will return this string rather than the normal email address, I'm not sure why but how can I get it into a normal email address even when they both belong to the same domain. Or is there another way to circumvent this problem, as it makes no sense to me why it works the way to does. If anyone happens to know the reasoning behind this I'd love to hear it. But that's not my question.

EDIT:

Found this, https://msdn.microsoft.com/en-us/library/office/ff869674.aspx Refers to MailItem.SenderEmailType property. Which if EX is exchange server in same organization. So I can test to see if I will get this werid string or a normal email string (if the type is SMTP).

The example seems to answer my question, but it's in VB, and I'm having some trouble to convert it to C#. I see that I can get the AddressEntry from the MailItem object, and then use x = GetExchangeUser() and then do x.PrimarySMTPAdress to get the address. But I'm not sure if this is correct either.

If anyone has any information on SenderEmailType and dealing with exchange users in the same domain, any advice would help a lot.

Thanks.

1

1 Answers

7
votes

If SenderEmailType == "SMTP", just use SenderEmailAddress.

If SenderEmailType == "EX", use MailItem.Sender.GetExchangeUser().PrimarySmtpAddress. Be prepared to handle nulls and exceptions.