How to configure Mail with MSSQL

  1. See Database Mail for how to enable Database Mail.
  2. After enabling Database Mail, you will need to create and configure the accounts. It's done in 3 steps:
    1. Create a profile with sysmail_add_profile_sp
    2. Create an email account with sysmail_add_account_sp
    3. Associate the email account to the profile with sysmail_add_profileaccount_sp

Applies to:

  • Microsoft SQL Server 2005

Creating SQL email accounts

  1. EXEC msdb.dbo.sysmail_add_profile_sp
  2.      @profile_name = 'Main',
  3.      @description = 'Main profile';
  4. go
  5.  
  6. EXEC msdb.dbo.sysmail_add_account_sp
  7.      @account_name = 'System',
  8.      @email_address = 'system@smtp.system.com',
  9.      @display_name = 'sql',
  10.      @mailserver_name = 'smtp.system.com';
  11. go
  12.  
  13. EXEC msdb.dbo.sysmail_add_profileaccount_sp
  14.      @profile_name = 'Main',
  15.      @account_name = 'System',
  16.      @sequence_number = 1;
  17. go
Command(s) completed successfully
  1. Create the profile
  2. Create the email account, with the email address on line 8 and the actual mail server on line 10. The email account must be enable on the actual mail server.
  3. Associate the the profile with the account number. The sequence number on line 16 determines the order in which accounts are used in the profile.
  4. Note that there is only 1 line replied by the mail server, in spite of the 3 commands executed.

Deleting SQL email accounts

  • Similar commands are used, just replace the word add with delete:
    • sysmail_delete_profileaccount_sp
    • sysmail_delete_profile_sp
    • sysmail_delete_account_sp
  1. EXEC msdb.dbo.sysmail_delete_profileaccount_sp
  2.      @profile_name = 'Main',
  3.      @account_name = 'System';
  4. go
  5.  
  6. EXEC msdb.dbo.sysmail_delete_profile_sp
  7.      @profile_name = 'Main';
  8. go
  9.  
  10. EXEC msdb.dbo.sysmail_delete_account_sp
  11.      @account_name = 'System';
  12. go
Command(s) completed successfully
  1. Notice that the order has changed, you first need to delete the profile account association, then you can delete the account and/or the profile.
  2. Note that there is only 1 line replied by the mail server, in spite of the 3 commands executed.