데이터베이스 메일 사용

SMO에서 데이터베이스 메일 하위 시스템은 Mail 속성에서 참조되는 SqlMail 개체로 표시됩니다. SMO SqlMail 개체를 사용하여 데이터베이스 메일 하위 시스템을 구성하고 프로필 및 메일 계정을 관리할 수 있습니다. SMO SqlMail 개체는 Server 개체에 속하는데, 이는 메일 계정의 범위가 서버 수준임을 의미합니다.

제공된 코드 예제를 사용하려면 응용 프로그램을 만들 프로그래밍 환경, 프로그래밍 템플릿 및 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual Basic SMO 프로젝트 만들기 또는 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하십시오.

SQL Server 데이터베이스 메일을 사용하는 프로그램에 대해 메일 네임스페이스를 한정하는 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, "AdventureWorks Administrator", "AdventureWorks 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, "AdventureWorks2012 Administrator", "AdventureWorks2012 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()