In my daily job as a SQL Server consultant I go to a lot of places, many days on the road, and many nights at different hotels. All these things are a part of the job, so of course I am not complaining.

What some times can be a challenge is to have access to the same SMTP server again and again, and especially when I am testing something with Database Mail, SQL Server alerts, Operators or something else that uses a SMTP server from SQL Server to send out an email.

Because of these challenges I normally use a Gmail account to send mails from, Gmail requires authentication to send mails via their SMTP servers, so an account type like this can be used from anywhere.

So many people have blogged about this before me, including myself actually – I blogged some thing like this a few years back – in DANISH. I always struggle to find my script to configure these things, so that is why I am blogging about this again – to refresh my brain, and to share my code with You.

Before we can start playing with Database mail, we have to enable the feature, that is done with the following code:

  1. EXEC sp_Configure 'Database Mail XPs', 1  
  2. GO  
  3.   
  4. RECONFIGURE  
  5. GO  

With the feature enabled, all there is left to do before sending mails from SQL Server, is to changes the following script so that is takes you account info, and not the bogus info that I have entered:

  1. -- Create a Database Mail account  
  2. EXEC msdb.dbo.sysmail_add_account_sp    
  3.       @account_name = 'MyMailAccount'  
  4.     , @email_address = 'me@gmail.com'  
  5.     , @display_name = 'My Servers Database Mail Account'  
  6.     , @description = 'MyMailAccount'   
  7.     , @mailserver_name = 'smtp.gmail.com'   
  8.     , @port = 587  
  9.     , @username = 'me@gmail.com'  
  10.     , @password = 'mYpASSWORD'  
  11.     , @enable_ssl = 1  
  12.   
  13. -- Create a Database Mail profile  
  14. EXECUTE msdb.dbo.sysmail_add_profile_sp  
  15.     @profile_name = 'MyPublicProfile',  
  16.     @description = 'My Public Profile';  
  17.   
  18. -- Add the account to the profile  
  19. EXECUTE msdb.dbo.sysmail_add_profileaccount_sp  
  20.     @profile_name = 'MyPublicProfile',  
  21.     @account_name = 'MyMailAccount',  
  22.     @sequence_number = 1;  
  23.   
  24. -- Configuring global profile, and setting default mail profile  
  25. EXECUTE msdb.dbo.sysmail_add_principalprofile_sp  
  26.     @profile_name = 'MyPublicProfile',  
  27.     @principal_name = 'public',  
  28.     @is_default = 1;  
  29. GO  

After execution this script, we are ready to send out an test mail, to check that everything is working, here is the code to do that:

  1. EXECUTE msdb.dbo.sp_send_dbmail  
  2.     @subject = 'Test Database Mail Message',  
  3.     @recipients = 'me@myemail.com',  
  4.     @query = 'set nocount on;SELECT Getdate() as ServerTime';  
 

Wait one second and you should receive an email from you SQL Server. In the next blog post we’ll look at how to monitor and debug Database mail – stay tuned for that next week.

Before I finish this blog post totally, I’ll just show you a few functions and views that you can use to look at the profiles and accounts created. And another god thing to have handy is how to delete the account/profile just created.

  1. exec msdb.dbo.sysmail_delete_account_sp @account_name = 'MyMailAccount'  
  2. exec msdb.dbo.sysmail_delete_profile_sp @profile_name = 'MyPublicProfile' 
  3. select * from msdb.dbo.sysmail_account  
  4. select * from msdb.dbo.sysmail_profile  
 
Geniiius ApS