How to: Create a Database Mail Account by Using Visual Basic .NET

This section describes how to create a Database Mail e-mail account in Microsoft Visual Basic .NET.

The 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 actually send or handle received e-mail.

Creating a Database Mail user account

  1. Start Visual Studio 2005.

  2. On the File menu, select New Project. The New Project dialog box appears.

  3. In the Project Types pane, select Visual Basic. In the Templates pane, select Console Application.

  4. (Optional) In the Name box, type the name of the new application.

  5. Click OK to load the Visual Basic console application template.

  6. On the Project menu, select Add Reference item. The Add Reference dialog box appears. Select Browse and locate the SMO assemblies in the C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies folder. Select the following files:

    Microsoft.SqlServer.ConnectionInfo.dll

    Microsoft.SqlServer.Smo.dll

    Microsoft.SqlServer.SqlEnum.dll

    Microsoft.SqlServer.SmoEnum.dll

  7. On the View menu, click Code.-Or-Select the Mobile1.vb window to display the code window.

  8. In the code, before any declarations, type the following Imports statements to qualify the types in the SMO namespace.

    Imports Microsoft.SqlServer.Management.Smo
    Imports Microsoft.SqlServer.Management.Common
    Imports Microsoft.SqlServer.Management.Smo.Mail
    
  9. Insert the code that follows this procedure into the main program.

  10. Run and build the application.

示例

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

请参阅

概念

Using Database Mail

帮助和信息

获取 SQL Server 2005 帮助