【问题标题】:VBA Outlook: How to get the smtp address of the selected folder accountVBA Outlook:如何获取所选文件夹帐户的 smtp 地址
【发布时间】:2026-01-25 11:05:01
【问题描述】:

我的 Outlook 中有不同的帐户。 根据当前选择的文件夹,我想找到对应账户的smtp Email地址。 (文件夹名无济于事)

我知道如何获取帐户的 smtp 电子邮件地址: (olApp.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress)

我知道如何获取当前选定的文件夹甚至它的商店名称: (olApp.ActiveExplorer.CurrentFolder.store.DisplayName)

但我找不到如何链接这两个信息...

有什么想法吗?

谢谢 :)

【问题讨论】:

    标签: vba email outlook directory


    【解决方案1】:
    Private Sub storeAddress_from_DisplayName()
    
    Dim storeDisplayName As String
    Dim storeSMTPAddress As String
    
    Dim storeRecipient As Recipient
    
    ' DisplayName and PrimarySmtpAddress can be the same
    
    storeDisplayName = ActiveExplorer.CurrentFolder.Store.DisplayName
    Debug.Print " storeDisplayName: " & storeDisplayName
    
    Set storeRecipient = Session.CreateRecipient(storeDisplayName)
    
    If storeRecipient.AddressEntry.Type = "EX" Then
        storeSMTPAddress = storeRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
        Debug.Print " storeSMTPAddress: " & storeSMTPAddress
    End If
    
    End Sub
    

    【讨论】:

    • 请记住,如果有同名的联系人(我至少有几个),名称解析将失败。另请记住,在旧版本的 Outlook 中,商店名称可能带有“邮箱”前缀。这不是我在生产代码中使用的东西。
    【解决方案2】:

    理论上,您可以解析 EX 商店条目 id 以提取 EX 地址,然后使用它来构建 GAL 条目 id,您可以使用它来调用 Namespace.GetAddressEntryFromID。您可以在 MFCMAPI source code 中查看如何解析存储条目(在 C++ 中)。

    如果使用Redemption 是一个选项,它会公开RDOExchangeMailboxStore.Owner 属性(返回RDOAddressEntry 对象,该对象又会公开SMTPSAddress 属性):

    Set MySession = CreateObject("Redemption.RDOSession")
    MySession.MAPIOBJECT = Application.Session.MAPIOBJECT
    set Store = MySession.GetStoreFromID(Application.ActiveExplorer.CurrentFolder.StoreID)
    MsgBox Store.Owner.SmtpAddress
    

    【讨论】: