Reputation: 31
I am trying to create Outlook Addin using C#. Customizing Application_ItemSend
event of the Send
button.
I am unable to get from/sender email address. I am getting following things as null:
Outlook.MailItem.SenderEmailType
Outlook.MailItem.Sender
(as this is null not able to get PrimarySmtpAddress)Outlook.MailItem.SenderEmailAddress
Any pointers what is wrong? Is it my outlook account is incorrectly configured?
Any help appreciated. Thanks in advance.
Below is the code to fetch address which i am using:
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;
}
}
Upvotes: 3
Views: 4383
Reputation: 66306
Sender related properties are only set after the message is actually sent. Try to use the Items.ItemAdd
event on the Sent Items folder (retrieved using Namespace.GetDefaultFolder
).
If SendUsingAccount
property is not set, you can assume the default account is being used - use the first account from the Namespace.Accounts
collection and retrieve the Account.SmtpAddress
property.
Upvotes: 3
Reputation: 1332
Short answer: those items aren't populated until it's actually sent (this hook is when you are just about to send the command to the exchange server to send the email). Use the "SendUsingAccount" field instead, as it will have all that information (in addition to the information you can find in the user's mailbox/account objects).
I'm pretty sure the reason why is that those field are not filled in until dynamic rules and policies are applied server-side.
Upvotes: 0