Setting up DB mail

Here's a script to setup db mail, copied from https://mathaywardhill.com/2017/03/01/setting-up-database-mail-to-use-my-gmail-account/

I haven't used it yet, but I do this about once a month and I didn't want to lose the script.

I don't see a flag in the script to 'set as default', I'll have to look for that when I use it first.

use [master];
GO
/* Create database mail account
*/
exec msdb.dbo.sysmail_add_account_sp
  @account_name = 'My Gmail account'
, @email_address = 'youraccount@gmail.com'
, @mailserver_name = 'smtp.gmail.com'
, @port = 587
, @enable_ssl = 1;
GO
/* Create database mail profile
*/
exec msdb.dbo.sysmail_add_profile_sp
  @profile_name = 'Database Mail profile';
GO
/* Associate Database Mail profile with the account My Gmail account
*/
exec msdb.dbo.sysmail_add_profileaccount_sp
  @profile_name = 'Database Mail profile'
, @account_name = 'My Gmail account'
, @sequence_number = 1;
GO
/* Enable Database Mail
*/
exec sp_configure 'show advanced options',1
reconfigure
GO
exec sp_configure 'Database Mail XPs',1
reconfigure
GO

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences