In this article I will explain with an example, how to send email with attachment from Folder (Directory) in SQL Server.
This article is applicable to following SQL Server versions i.e. 2008, 2008R2, 2012, 2014, 2016, 2017, 2019 and 2022.
 
 

Configuring SQL Server for sending emails

In order to start sending emails, the very first thing to be done is to configure SQL Server for sending emails from Database.
Thus, in order to achieve the same, please refer my article Configure SQL Server for sending emails.
 
 

Sending Email with attachment from Folder (Directory) in SQL Server

Once the SQL Server Database is configured for sending emails, you are now ready for sending emails with attachment from Folder (Directory).
In the following SQL query, the Stored Procedure sp_send_dbmail is used to send an email from SQL Server.
EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Mudassar_Email_Profile'
   ,@recipients='recepient@gmail.com'
   ,@subject='Email from SQL Server'
   ,@body='Customer Details.'
   ,@importance ='HIGH'
   ,@file_attachments = 'D:\Files\Customers.csv'
GO
 
Note: In case you are getting any error related to attachment, please refer the article SQL Server sp_send_dbmail Error: attachment file is invalid.
 
Once executed, the following messages will appear which confirms, your email has been queued.
Send email with attachment from Folder (Directory) in SQL Server
 
 

Checking Emails Status and Errors using Log Tables

Once the Stored Procedure sp_send_dbmail is executed, the mails are queued and then sent.
In case you do not receive email, then you can find the status of email i.e. sent, unsent or failed using my article Find email sent status using sysmail_sentitems in SQL Server.
 
 

GMAIL SMTP Errors

The possible errors (exceptions) occurring while sending email using GMAIL SMTP are covered in the following article.
 
 

Screenshots

Mail

Send email with attachment from Folder (Directory) in SQL Server
 

Attachment

Send email with attachment from Folder (Directory) in SQL Server
 
 

Downloads