Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Using Database Mail

 

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

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 Basic SMO Project in Visual Studio .NET or 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

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

VB.NET

Invalid code reference, code snippet not found, please check the code snippet name 'SMO How to' and try again.

This code example shows how to create an e-mail account in SMO. Database Mail is represented by the T:Microsoft.SqlServer.Management.Smo.Mail.SqlMail object and referenced by the P:Microsoft.SqlServer.Management.Smo.Server.Mail property of the T:Microsoft.SqlServer.Management.Smo.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, "AdventureWorks2012 Administrator", "AdventureWorks2012 Automated Mailer", "Mail account for administrative e-mail.", "dba@Adventure-Works.com"); 
           a.Create();  
}

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

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()
Show:
© 2015 Microsoft