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
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, "AdventureWorks2012 Administrator", "AdventureWorks2012 Automated Mailer", "Mail account for administrative e-mail.", "dba@Adventure-Works.com");
a.Create();
}
PowerShell を使用したデータベース メール アカウントの作成
このコード例では、SMO で電子メール アカウントを作成する方法を示します。 データベース メールは、SqlMail オブジェクトによって表され、Server オブジェクトのMail プロパティによって参照されます。 SMO を使用してデータベース メールをプログラムで構成できますが、受信した電子メールの送受信には使用できません。
#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()