使用数据库邮件

适用于:SQL Server Azure SQL 数据库 azure Synapse Analytics Azure SQL 托管实例

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

示例

若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。 有关详细信息,请参阅 在 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 对象表示,并由 Mail 对象的 Server 属性引用。 SMO 可用于以编程方式配置数据库邮件,但它无法用于发送或处理收到的电子邮件。

'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 对象表示,并由 Mail 对象的 Server 属性引用。 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, "AdventureWorks2022 Administrator", "AdventureWorks2022 Automated Mailer", "Mail account for administrative e-mail.", "dba@Adventure-Works.com");   
           a.Create();    
}  

使用 PowerShell 创建数据库邮件帐户

此代码示例说明如何在 SMO 中创建电子邮件帐户。 数据库邮件由 SqlMail 对象表示,并由 Mail 对象的 Server 属性引用。 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()