次の方法で共有


データベース メールの使用

SMO では、Mail プロパティによって参照される SqlMail オブジェクトで、データベース メール サブシステムを表現します。SMO SqlMail オブジェクトを使用すると、データベース メール サブシステムを構成して、プロファイルおよびメール アカウントを管理することができます。SMO SqlMail オブジェクトは Server オブジェクトに属しています。これは、メール アカウントのスコープがサーバー レベルであることを意味しています。

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。詳細については、「Visual Studio .NET で Visual Basic SMO プロジェクトを作成する方法」または「Visual Studio .NET で Visual C# SMO プロジェクトを作成する方法」を参照してください。

プログラムで SQL Server データベース メールを使用する場合、Mail 名前空間を修飾する Imports ステートメントを含める必要があります。アプリケーションの宣言の前、かつ他の Imports ステートメントの後に、次のようにステートメントを挿入します。

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Mail

Visual Basic によるデータベース メール アカウントの作成

このコード例では、SMO で電子メール アカウントを作成する方法を示します。データベース メールは SqlMail オブジェクトで表現され、Server オブジェクトの Mail プロパティによって参照されます。SMO は、プログラムでデータベース メールを構成するために使用することはできますが、電子メールを送信したり、受信した電子メールを処理したりするために使用することはできません。

VB.NET

'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, "Adventure Works Administrator", "Adventure Works Automated Mailer", "Mail account for administrative e-mail.", "dba@Adventure-Works.com")
a.Create()

Visual C# によるデータベース メール アカウントの作成

このコード例では、SMO で電子メール アカウントを作成する方法を示します。データベース メールは SqlMail オブジェクトで表現され、Server オブジェクトの Mail プロパティによって参照されます。SMO は、プログラムでデータベース メールを構成するために使用することはできますが、電子メールの送信や受信した電子メールの処理を行うために使用することはできません。

{
         //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; 
           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, "AdventureWorks2008R2 Administrator", "AdventureWorks2008R2 Automated Mailer", "Mail account for administrative e-mail.", "dba@Adventure-Works.com"); 
           a.Create();  
}

PowerShell によるデータベース メール アカウントの作成

このコード例では、SMO で電子メール アカウントを作成する方法を示します。データベース メールは SqlMail オブジェクトで表現され、Server オブジェクトの Mail プロパティによって参照されます。SMO は、プログラムでデータベース メールを構成するために使用することはできますが、電子メールを送信したり、受信した電子メールを処理したりするために使用することはできません。

PowerShell

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

#Get a server object which corresponds to the default instance
$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server

#Define the Database Mail; reference it using the Server Mail property.
$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.
$a = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Mail.MailAccount -argumentlist $sm, `
"Adventure Works Administrator", "Adventure Works Automated Mailer",`
 "Mail account for administrative e-mail.", "dba@Adventure-Works.com"
$a.Create()