HOWTO: configure database mail with MSSQL

Some developers may choose to utilize SQL's Mail option as in some scenarios there may be different front end type web applications tied to a single type of database (in this case MS SQL 2005).

This prevents the developer from having to write the same email method for each type of front end application.

This particular method may also be referenced as sending email from the "back end".

NOTE: Supported on MS SQL 2005 and up ONLY.

Before you continue you must obtain the following information from the customer:

-Database Name.
-DB user.
-Mail Account login (User, password, domain).

Example info-

DB Name:
DB User: caneja

Mail server:
Mail User:
Mail password: sql

Replace any variables referencing the or caneja account with the information provided by the customer then paste the script in a new query window in SQL Management Studio while logged in as an admin.

This operation must be performed on the server where the db is hosted.

You may execute the script at once and it should output a "Command(s) completed successfully".


-- Create a Database Mail account
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = '',
@description = '',
@email_address = '',
@replyto_address = '',
@display_name = '',
@mailserver_name = '',
@username ='',
@password ='sql' ;

-- Create a Database Mail profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = '',
@description = '' ;

-- Add the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = '',
@account_name = '',
@sequence_number =1 ;

exec msdb.dbo.sysmail_help_account_sp

--adding user to msdb db.
use msdb
sp_adduser 'caneja'
sp_addrolemember 'DatabaseMailUserRole','caneja'

-- Grant access to the profile to the DBMailUsers role
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = '',
@principal_name = 'caneja',
@is_default = 1 ;

Add Feedback