Sending Email from SQL Server

Abhilash S
intertoons
Published in
1 min readApr 17, 2023

Here’s an example SQL Server code for sending an email using the built-in sp_send_dbmail stored procedure

EXEC msdb.dbo.sp_send_dbmail  
@profile_name = 'YourEmailProfile', -- Replace with your email profile name
@recipients = 'youremail@example.com', -- Replace with your email address
@subject = 'Email Subject',
@body = 'Email Body',
@file_attachments = 'C:\path\to\attachment.txt' -- Replace with the path to your attachment file, optional parameter

This code uses the sp_send_dbmail stored procedure to send an email. You need to replace the profile_name parameter with your email profile name, the recipients parameter with your email address, and customize the subject and body message according to your needs.

Optionally, you can also attach a file to the email by specifying the path to the file using the file_attachments parameter.

Note that you need to configure Database Mail and set up an email profile before you can use sp_send_dbmail. You also need to make sure that your SMTP server and firewall allow email communication.

--

--

Abhilash S
intertoons

Solutions Architect @ Intertoons , Over 16 years of Collective experiences In Windows , Linux Developments , Redhat, Google Certified Professional