Outlook Programming : How to get the SMTP Address of the Sender of a Mail Item using Outlook Object Model?
Recently I was assisting an developer who used Outlook Object Model (OOM) API and tried to get the SMTP address of the Sender of a given mail item.
In order to get the values, he first made the following OOM call – it worked fine for him for couple of mail items, but fails to get the SMTP value as given below:
1: mailItem.Recipients[i].Address
It returned the value as,
/O=MFC2013/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=B370134F8FFD4CF3A0023F27B6B61F7D-ADMINISTRATOR
In this scenario, to determine the SMTP address for a mail item, you can use the SenderEmailAddress property of the MailItem object. However, if the sender is internal to your organization, SenderEmailAddress does not return an SMTP address, and you must use the PropertyAccessor object to return the sender’s SMTP address (adding the related C#.Net code for your reference).
1: private string GetSMTPAddress(Outlook.MailItem mail)
2: {
3: string PR_SMTP_ADDRESS = @"https://schemas.microsoft.com/mapi/proptag/0x39FE001E";
4: if (mail.SenderEmailType == "EX")
5: {
6: Outlook.AddressEntry sender =
7: mail.Sender;
8: if (sender != null)
9: {
10: //Now we have an AddressEntry representing the Sender
11: if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
12: || sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
13: {
14: //Use the ExchangeUser object PrimarySMTPAddress
15: Outlook.ExchangeUser exchUser = sender.GetExchangeUser();
16: if (exchUser != null)
17: {
18: return exchUser.PrimarySmtpAddress;
19: }
20: else
21: {
22: return null;
23: }
24: }
25: else
26: {
27: return sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS) as string;
28: }
29: }
30: else
31: {
32: return null;
33: }
34: }
35: else
36: {
37: return mail.SenderEmailAddress;
38: }
39: }
This will help you to move ahead and get the correct SMTP address:
Happy Programming!!
Comments
- Anonymous
August 12, 2013
Does not work for Outlook 2007 because MailItem.Sender does not exist.