SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software SQL Server and T-SQL Development Tutorials
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.

download SQL Server 2017
download SQL Server 2016
download SQL Server 2014
download SQL Server 2012

SQL Server Email using sp_send_dbmail with File Attachment

SQL Server sp_send_dbmail enabled developers easily send email from SQL Server. With each SQL Server version, sp_send_dbmail stored procedure is being improved and it is now easier to attach files to database mails in SQL Server sent by using sp_send_dbmail.

Note : If you are new with database mail with sp_send_dbmail please refer to tutorial How to configure SQL Server 2005 Database Mail sp_send_dbmail

Transact-SQL developers can pass physical file path of the attachment file into the @file_attachments parameter of SQL sp_send_dbmail procedure.
Here is sample SQL code which sends email from SQL Server with file attachments.

EXEC msdb.dbo.sp_send_dbmail
 @profile_name = 'SQLServerDatabaseMailProfile',
 @recipients = 'sqlemailsample@kodyaz.com',
 @subject = 'Email with file attachment sent from SQL Server',
 @body = 'A file attachment has been included with in this SQL Server email.',
 @file_attachments = 'C:\SQLServer2012.jpg'

And here is the email delivered by SQL Server 2008 R2 to my mailbox using the above sql code.

SQL Server email sample using sp_send_dbmail with file_attachments

It is also possible to send multiple file attachments within a mail using semi-column (";") as seperator character between file list in the @file_attachments parameter. Below sql programmers can find T-SQL code which execute sp_send_dbmail with ";" seperated list of files in @file_attachments input parameter.

EXEC msdb.dbo.sp_send_dbmail
 @profile_name = 'SQLServerDatabaseMailProfile',
 @recipients = 'sqlemailsample@kodyaz.com',
 @subject = 'Multiple file attachments in SQL Server DBMail',
 @body = 'Multiple files are attached in this email sent via SQL Server sp_send_dbmail',
 @file_attachments = 'C:\SQLServer2012.jpg;C:\SQL Server Denali.pdf'

Below you can see multiple files as email attachment in database mail sent by SQL Server sp_send_dbmail procedure

multiple file attachments in SQL Server dbmail

For more sample codes on SQL Server emailing please read T-SQL tutorial SQL Server Database Mail sp_send_dbmail example

Related SQL Resources

SQL Server Articles

SQL Server 2012

SQL Server Tools

SQL Blog

SQL Server 2008 Blog

Certification Exams Blog

Reporting Services Blog

Analysis Services Blog

MS SQL Server Forums

Copyright © 2004 - 2018 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems