Επεξεργασία

Κοινή χρήση μέσω


Using Database Mail

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics SQL database in Microsoft Fabric

In SMO, the Database Mail subsystem is represented by the SqlMail object that is referenced by the Mail property. By using the SMO SqlMail object, you can configure the Database Mail subsystem and manage profiles and mail accounts. The SMO SqlMail object belongs to the Server object, meaning that scope of the mail accounts is at the server level.

Examples

To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see Create a Visual C# SMO Project in Visual Studio .NET.

For programs that use SQL Server Database Mail, you must include the Imports statement to qualify the Mail namespace. Insert the statement after the other Imports statements, before any declarations in the application, such as:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Mail

Creating a Database Mail Account by Using Visual Basic

This code example shows how to create an e-mail account in SMO. Database Mail is represented by the SqlMail object and referenced by the Mail property of the Server object. SMO can be used to programmatically configure Database Mail, but it cannot be used to send or handle received e-mail.

'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()

Creating a Database Mail Account by Using Visual C#

This code example shows how to create an e-mail account in SMO. Database Mail is represented by the SqlMail object and referenced by the Mail property of the Server object. SMO can be used to programmatically configure Database Mail, but it cannot be used to send or handle received e-mail.

{  
         //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();    
}  

Creating a Database Mail Account by Using PowerShell

This code example shows how to create an e-mail account in SMO. Database Mail is represented by the SqlMail object and referenced by the Mail property of the Server object. SMO can be used to programmatically configure Database Mail, but it cannot be used to send or handle received e-mail.

#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()