使用数据库邮件

在 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, "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()