共用方式為


傳送已給予指定帳戶 SMTP 地址的電子郵件

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

範例

注意事項

Helmut Obertanner 提供下列程式代碼範例。 Helmut 的專業知識適用於 Visual Studio 和 Outlook 的 Office 開發人員工具。

下列程式代碼範例包含 Sample 類別的 SendEmailFromAccount 和 GetAccountForEmailAddress 方法,實作為 Outlook 載入宏專案的一部分。 每個項目都會新增以 Microsoft.Office.Interop.Outlook 命名空間為基礎的 Outlook 主要 Interop 元件參考。 SendEmailFromAccount 方法會接受 作為受信任 的 Application 對象的輸入自變數,以及代表主旨、本文、以分號分隔之收件者清單,以及電子郵件帳戶 SMTP 位址的字元串。 SendEmailFromAccount 會建立 MailItem 物件,並使用指定的自變數初始化 ToSubjectBody 屬性。 為了尋找要從中傳送電子郵件的 Account 物件,SendEmailFromAccount 會呼叫 GetAccountForEmailAddress 方法,此方法會比對指定的 SMTP 位址與目前配置檔中每個帳戶的 SmtpAddress 屬性。 相符的 Account 物件會傳回 SendEmailFromAccount,然後使用此 Account 物件初始化 MailItemSendUsingAccount 属性,並傳送 MailItem

以下是 Visual Basic 程式代碼範例,後面接著 C# 程式代碼範例。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
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
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));
        }

    }
}

另請參閱