共用方式為


在 Outlook) ,傳送指定帳戶 SMTP 位址的電子郵件 (

本主題說明如何建立電子郵件,並從 Microsoft Outlook 帳戶傳送電子郵件,並提供該帳戶的簡易郵件傳輸通訊協定 (SMTP) 位址。

| MVP 標誌

|Helmut Obertanner 提供下列程式碼範例。 Helmut 是 Microsoft 最有價值專家 ,具備 Microsoft Visual Studio 和 Microsoft Office Outlook 中 Microsoft Office 開發工具的專業知識。|

The following managed code samples are written in C# and Visual Basic. 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 元件參考 。 下列程式碼範例包含 SendEmailFromAccount 類別的 SampleGetAccountForEmailAddress 方法,實作為 Outlook 增益集專案的一部分。 每個專案都會新增 Outlook PIA 的參考,以 Microsoft.Office.Interop.Outlook 命名空間為基礎。 方法 SendEmailFromAccount 會接受做為受信任的 Application 物件的輸入引數,以及代表主旨、本文、以分號分隔之收件者清單,以及電子郵件帳戶 SMTP 位址的字串。 SendEmailFromAccount 會建立 MailItem 物件,並使用指定的引數初始化 ToSubjectBody 屬性。 若要尋找要從中傳送電子郵件的 Account 物件, SendEmailFromAccount 請呼叫 GetAccountForEmailAddress 方法,此方法會將指定的 SMTP 位址與目前設定檔之每個帳戶的 SmtpAddress 屬性相符。 相符的Account物件會傳回至 SendEmailFromAccount ,然後使用此 Account物件初始化MailItemSendUsingAccount屬性,並傳送MailItem。 下列為 C# 程式碼範例。

using System; 
using System.Text; 
using Outlook = Microsoft.Office.Interop.Outlook; 
 
namespace OutlookAddIn1 
{ 
    class Sample 
    { 
        public static void SendEmailFromAccount(Outlook.Application application, string subject, string body, string to, string smtpAddress) 
        { 
 
            // Create a new MailItem and set the To, Subject, and Body properties. 
            Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem); 
            newMail.To = to; 
            newMail.Subject = subject; 
            newMail.Body = body; 
 
            // Retrieve the account that has the specific SMTP address. 
            Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress); 
            // Use this account to send the email. 
            newMail.SendUsingAccount = account; 
            newMail.Send(); 
        } 
 
 
        public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress) 
        { 
 
            // Loop over the Accounts collection of the current Outlook session. 
            Outlook.Accounts accounts = application.Session.Accounts; 
            foreach (Outlook.Account account in accounts) 
            { 
                // When the email address matches, return the account. 
                if (account.SmtpAddress == smtpAddress) 
                { 
                    return account; 
                } 
            } 
            throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)); 
        } 
 
    } 
}

下列為 Visual Basic 程式碼範例。

Imports Outlook = Microsoft.Office.Interop.Outlook 
 
Namespace OutlookAddIn2 
    Class Sample 
         
        Shared Sub SendEmailFromAccount(ByVal application As Outlook.Application, _ 
            ByVal subject As String, ByVal body As String, ByVal recipients As String, ByVal smtpAddress As String) 
 
            ' Create a new MailItem and set the To, Subject and Body properties. 
            Dim newMail As Outlook.MailItem = DirectCast(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem) 
            newMail.To = recipients 
            newMail.Subject = subject 
            newMail.Body = body 
 
            ' Retrieve the account that has the specific SMTP address. 
            Dim account As Outlook.Account = GetAccountForEmailAddress(application, smtpAddress) 
            ' Use this account to send the email. 
            newMail.SendUsingAccount = account 
            newMail.Send() 
        End Sub 
 
        Shared Function GetAccountForEmailAddress(ByVal application As Outlook.Application, ByVal smtpAddress As String) As Outlook.Account 
 
            ' Loop over the Accounts collection of the current Outlook session. 
            Dim accounts As Outlook.Accounts = application.Session.Accounts 
            Dim account As Outlook.Account 
            For Each account In accounts 
                ' When the email address matches, return the account. 
                If account.SmtpAddress = smtpAddress Then 
                    Return account 
                End If 
            Next 
            Throw New System.Exception(String.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)) 
        End Function 
 
    End Class 
End Namespace

支援和意見反應

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