
For an instructor lead, in-depth look at learning SQL click below.
SQL Server Database Mail, a component of Microsoft SQL Server, is a feature that provides an easy, efficient way to send asynchronous mail messages from SQL Server. These messages can be received by any SMTP-enabled device, such as email accounts, cell phones, or pagers. The Database Mail feature is highly configurable, allowing you to specify the mail server, port, and authentication method.
Setting up SQL Server Database Mail
To use SQL Server Database Mail, you’ll first need to configure it. Below is an example of the T-SQL commands to setup Database Mail with sql server authentication:
1 2 3 4 5 6 7 8 9 |
EXECUTE m<a href="mailto:sdb.dbo.sysmail_add_account_sp @account_name" >sdb.dbo.sysmail_add_account_sp @account_name</a> = 'Test_Mail_Account', @description = 'Mail account for administrative e-mail.', @email_address = <a href="mailto:'admin@yourdomain.com'" >'admin@yourdomain.com'</a>, @replyto_address = <a href="mailto:'replyto@yourdomain.com'" >'replyto@yourdomain.com'</a>, @mailserver_name = 'smtp.yourdomain.com'; |
Sending a Test Email
Once your account is set up, you can use the following code to send an email from your new mail account:
1 2 3 4 5 6 7 8 |
EXEC m<a href="mailto:sdb.dbo.sp_send_dbmail @profile_name" >sdb.dbo.sp_send_dbmail @profile_name</a> = 'Test_Mail_Profile', @recipients = <a href="mailto:'test@yourdomain.com'" >'test@yourdomain.com'</a>, @subject ='Test Email', @body ='This is a test email' |
Monitoring your Database Mail
If you want to check the status of an email, you can use the following command:
1 2 3 |
EXEC msdb.dbo.sysmail_help_queue_sp @queue_type = 'mail'; |
Conclusion
Using SQL Server Database Mail, you can conveniently send emails directly from the SQL Server environment. With its wide range of features and versatile configuration options, SQL Server Database Mail is an indispensable tool for database administrators and developers alike.