Export (0) Print
Expand All

Using Database Mail

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.

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 How to: Create a Visual Basic SMO Project in Visual Studio .NET or How to: 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 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.

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, "AdventureWorks Administrator", "AdventureWorks 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 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, "AdventureWorks2008R2 Administrator", "AdventureWorks2008R2 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 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.

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft