使用数据库邮件

在 SMO 中,数据库邮件子系统由 SqlMail 属性引用 Mail 的对象表示。 通过使用 SMO SqlMail 对象,可以配置数据库邮件子系统并管理配置文件和邮件帐户。 SMO SqlMail 对象属于该 Server 对象,这意味着邮件帐户的范围在服务器级别。

例子

若要使用提供的任何代码示例,必须选择要在其中创建应用程序的编程环境、编程模板和编程语言。 有关详细信息,请参阅 在 Visual Studio .NET 中创建 Visual Basic SMO 项目 ,或在 Visual Studio .NET 中创建 Visual C# SMO 项目

对于使用 SQL Server 数据库邮件的程序,必须包含 Imports 语句来限定 Mail 命名空间。 将语句插入到其他 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()