共用方式為


取得收件者的Email位址

本主題說明如何取得 Recipients 集合中每個收件者的 SMTP 位址。

程式碼範例 GetSMTPAddressForRecipients 中的 方法 會採用 MailItem 作為輸入引數,然後顯示該訊息項目之每個收件者的 SMTP 位址。 方法會先擷取 Recipients 集合,代表為訊息項目指定的收件者集合。 針對該Recipients集合中的每個Recipient,方法接著會取得對應至該Recipient物件的PropertyAccessor物件,並使用PropertyAccessor取得 MAPI 屬性 https://schemas.microsoft.com/mapi/proptag/0x39FE001E 的值,該值會對應至收件者的 SMTP 位址。

本主題包含兩個程式碼範例。 下列程式碼範例是以 Microsoft Visual Basic for Applications (VBA) 所撰寫。

Sub GetSMTPAddressForRecipients(mail As Outlook.MailItem) 
    Dim recips As Outlook.Recipients 
    Dim recip As Outlook.Recipient 
    Dim pa As Outlook.PropertyAccessor 
    Const PR_SMTP_ADDRESS As String = _ 
        "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" 
    Set recips = mail.Recipients 
    For Each recip In recips 
        Set pa = recip.PropertyAccessor 
        Debug.Print recip.name & " SMTP=" _ 
           & pa.GetProperty(PR_SMTP_ADDRESS) 
    Next 
End Sub

The following managed code is written in C#. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio. 您應該使用 Office Developer Tools for Visual Studio) ,在 Outlook 增益集 (類別中使用下列程式碼 ThisAddIn 。 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱 MSDN 上的歡迎使用 Outlook 主要 Interop 元件參考

private void GetSMTPAddressForRecipients(Outlook.MailItem mail) 
{ 
    const string PR_SMTP_ADDRESS = 
        "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"; 
    Outlook.Recipients recips = mail.Recipients; 
    foreach (Outlook.Recipient recip in recips) 
    { 
        Outlook.PropertyAccessor pa = recip.PropertyAccessor; 
        string smtpAddress = 
            pa.GetProperty(PR_SMTP_ADDRESS).ToString(); 
        Debug.WriteLine(recip.Name + " SMTP=" + smtpAddress); 
    } 
} 

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應