共用方式為


使用 Database Mail

在 SMO 中,Database Mail 子系統是由 Mail 屬性所參考的 SqlMail 物件表示。藉由使用 SMO SqlMail 物件,您可以設定 Database Mail 子系統,並且管理設定檔和郵件帳戶。SMO SqlMail 物件屬於 Server 物件,代表郵件帳戶的範圍是伺服器層級。

範例

如果要使用所提供的任何程式碼範例,您必須選擇用於建立應用程式的程式設計環境、程式設計範本和程式設計語言。如需詳細資訊,請參閱<如何:在 Visual Studio .NET 中建立 Visual Basic SMO 專案>或<如何:在 Visual Studio .NET 中建立 Visual C# SMO 專案>。

如果程式使用 SQL Server Database Mail,則您必須包含 Imports 陳述式來限定 Mail 命名空間。將陳述式插入至其他 Imports 陳述式之後、在應用程式中的任何宣告之前,例如:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Mail

使用 Visual Basic 建立 Database Mail 帳戶

此程式碼範例示範如何在 SMO 中建立電子郵件帳戶。Database Mail 是由 SqlMail 物件表示,且由 Server 物件的 Mail 屬性參考。SMO 可用於以程式設計的方式設定 Database Mail,但不能用於傳送或處理已收到的電子郵件。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server()
'Define the Database Mail service with a SqlMail object variable and reference it using the Server Mail property.
Dim sm As SqlMail
sm = srv.Mail
'Define and create a mail account by supplying the Database Mail service, name, description, display name, and email address arguments in the constructor.
Dim a As MailAccount
a = New MailAccount(sm, "AdventureWorks Administrator", "AdventureWorks Automated Mailer", "Mail account for administrative e-mail.", "dba@Adventure-Works.com")
a.Create()

使用 Visual C# 建立 Database Mail 帳戶

此程式碼範例示範如何在 SMO 中建立電子郵件帳戶。Database Mail 是由 SqlMail 物件表示,且由 Server 物件的 Mail 屬性參考。SMO 可用於以程式設計的方式設定 Database Mail,但不能用於傳送或處理已收到的電子郵件。

{ 

//Connect to the local, default instance of SQL Server.

   Server srv = default(Server); 
   srv = new Server(); 
   //Define the Database Mail service with a SqlMail object variable 
   //and reference it using the Server Mail property. 
   SqlMail sm = default(SqlMail); 
   sm = srv.Mail; 
   //Define and create a mail account by supplying the Database Mail
   //service, name, description, display name, and email address
   //arguments in the constructor. 
   MailAccount a = default(MailAccount); 
   a = new MailAccount(sm, "AdventureWorks Administrator", "AdventureWorks Automated Mailer", "Mail account for administrative e-mail.", "dba@Adventure-Works.com"); 
   a.Create(); 
}