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.
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
For more sample codes on SQL Server emailing please read T-SQL tutorial SQL Server Database Mail sp_send_dbmail example