Some time ago to get Outlook accounts and account info (e.g. Email address, SMTP address) i was use Outlook.Accounts
entity, but Outlook.Accounts
caches data and doesn't support events like Add/Remove. Here I was offered to switch to Outlook.Stores
(Outlook.Store
) entity, but I don’t understand how I can get the Email address from Outlook.Store at least.
CodePudding user response:
Generally, stores do not have an intrinsic identity - imagine a standalone PST store: there is no user identity. Or you can have multiple POP3/SMTP accounts delivering to the same PST store - you now have multiple identities associated with the store.
Or imagine having a PF store - it is accessible to multiple users without having its own identity.
Only Exchange stores have a notion of an owner. You can go from an Exchange store to an email account by looping through the Namespace.Accounts
collection and comparing (using Namespace.CompareEntryIDs
) the entry id of your store in question and the store exposed by the Account.DeliveryStore
property.
If using Redemption is an option (I am its author), it exposes the Exchange mailbox owner directly through the RDOExchangeMailboxStore.Owner
property (returns RDOAddressEntry
object).
CodePudding user response:
If the store is associated with any account configured in Outlook you can use the following code which iterates over all accounts configured and finds the required one where you may ask for an email address:
Outlook.Account GetAccountForFolder(Outlook.Folder folder)
{
// Obtain the store on which the folder resides.
Outlook.Store store = folder.Store;
// Enumerate the accounts defined for the session.
foreach (Outlook.Account account in Application.Session.Accounts)
{
// Match the DefaultStore.StoreID of the account
// with the Store.StoreID for the currect folder.
if (account.DeliveryStore.StoreID == store.StoreID)
{
// Return the account whose default delivery store
// matches the store of the given folder.
return account;
}
}
// No account matches, so return null.
return null;
}
The Account.SmtpAddress property returns a string representing the Simple Mail Transfer Protocol (SMTP) address for the Account
. The purpose of SmtpAddress
and Account.UserName
is to provide an account-based context to determine identity. If the account does not have an SMTP address, SmtpAddress
returns an empty string.