Search code examples
c#emailoutlookoutlook-addinoutlook-redemption

Reliable way of getting sender's email address using Redemption


I have been getting the sender's email address in Outlook using the RDOMail.SenderEmailAddress property, but I have recently realised that under some cases for SMTP or IMAP email accounts, that this property is null occasionally, right after the email arrives the inbox.

There's a rather convoluted way to get the sender's email address using the Outlook Interop Library: https://msdn.microsoft.com/library/office/ff184624.aspx

However, this does not work with the Redemption RDOMail object because the RDOAddressEntry interface lacks the AddressEntryUserType and the GetExchangeUser method.

The documentation of the RDOAddressEntry object says the following about the SMTPAddress property:

String, read-only. Returns the SMTP address of the given user. If the address type is "SMTP", the returned value is the same as that returned by the Address property. If the address type is "EX", Redemption tries to retrieve the PR_SMTP_ADDRESS property, if unsuccessful, it retrieves the default SMTP address from the PR_EMS_AB_PROXY_ADDRESSES Extended MAPI property.

Looks like it might be a more reliable way to get the sender's address if I do this in my method:

if (rdoMail.SenderEmailAddress != null) return rdoMail.SenderEmailAddress;
if (rdoMail.Sender != null) return rdoMail.Sender.SMTPAddress;
return null;

Since I don't have a reliable way to test my theory, I'm seeking help here to see if anyone has more experience in dealing with this problem.

Thanks in advance.


Solution

  • I cannot image why SenderEmailAddress would be null, unless you have a partially downloaded item (IMAP4 specific). There is no reason to have AddressEntryUserType or GetExchangeUser method - all you need is the address type - if it is "EX", you have a GAL user, otherwise an SMTP address.

    To get the sender SMTP address, check the SenderEmailType property. If it is anything but "EX", just use the SenderEmailAddress property. If it is "EX", read the PidTagSenderSmtpAddress property using RDOMail.Fields["http://schemas.microsoft.com/mapi/proptag/0x5D01001F"]. If you get back null, check if RDOMail.Sender is not null and read the RDOMail.Sender.SMTPAddress property.