In SQL Server, you can use the sysmail_update_account_sp
stored procedure in the msdb
database to update your existing Database Mail accounts with T-SQL.
Update the Account Details
You can update an account’s details based on its name.
That is, if you don’t need to change the account name, you don’t need to provide the account ID – as long as you provide the name.
Example:
EXECUTE msdb.dbo.sysmail_update_account_sp
@account_name = 'DB Admin',
@description = 'Mail account for admin emails.',
@email_address = '[email protected]',
@replyto_address = '[email protected]',
@display_name = 'DB Automated Mailer',
@mailserver_name = 'smtp.example.com',
@port = 25;
Update the Account Name
When both the account name and the account ID are specified, the stored procedure changes the account name in addition to updating the information for the account.
EXECUTE msdb.dbo.sysmail_update_account_sp
@account_id = 1,
@account_name = 'Updated DB Admin',
@description = 'Mail account for admin emails.',
@email_address = '[email protected]',
@replyto_address = '[email protected]',
@display_name = 'DB Automated Mailer',
@mailserver_name = 'smtp.example.com',
@port = 25;
In this example I updated the name and various other details of the account.
The Syntax
The official syntax goes like this:
sysmail_update_account_sp [ [ @account_id = ] account_id ] [ , ] [ [ @account_name = ] 'account_name' ] ,
[ @email_address = ] 'email_address' ,
[ @display_name = ] 'display_name' ,
[ @replyto_address = ] 'replyto_address' ,
[ @description = ] 'description' ,
[ @mailserver_name = ] 'server_name' ,
[ @mailserver_type = ] 'server_type' ,
[ @port = ] port_number ,
[ @timeout = ] 'timeout' ,
[ @username = ] 'username' ,
[ @password = ] 'password' ,
[ @use_default_credentials = ] use_default_credentials ,
[ @enable_ssl = ] enable_ssl
See the Microsoft documentation for a detailed explanation of each argument.
Note that the sysmail_update_account_sp
stored procedure is in the msdb
database, and it’s owned by the dbo
schema. Therefore, you’ll need to use three part naming if you execute it from outside the msdb
database.
You can also use the sysmail_add_account_sp
stored procedure to create a new Database Mail account.