Send email with attachments from stored procedure in MS SQL 2005


I want to create a sql job that will run every night at 3 AM and send emails.

I am able to send emails from the stored procedure using "msdb.dbo.sp_send_dbmail".

                SET @SQL =     ' EXEC msdb.dbo.sp_send_dbmail
                @profile_name = ''' + @Profile_Name + ''',
                @recipients = ''' + @EmailRecipients + ''',
                @body_format = ''html'',
                @subject = ''' + @EmailSubjectToSend + ''',
                @body = ''' + @EmailBodyToSend + ''';'

        EXEC (@SQL)

Now In many cases I want to attach some files (one or more than one) to the email. The attachments are stored in the database itself (in one table. The table structure is:

CREATE TABLE [dbo].[tblCourseAutoEmailAttachments](
    [AttachmentID] [int] IDENTITY(1,1) NOT NULL,
    [CourseAutomatedEmailID] [int] NULL,
    [Attachment] [image] NULL,
    [AttachmentName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AttachmentSize] [int] NULL)

Please let me know how can I send emails with attachments from the stored procedure.

Thanks in advance.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You have to use bcp to dump you Attachment to a temp folder on the Hard Drive and then use the database mail to pick that up and send.

This link will help you with the above task

This can be used to send the mail

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Administrator',
    @recipients = '',
    @file_attachments =  'D:\File1.txt;D:\File2.txt'  
    @body = 'Test File Attachments',
    @subject = 'Files Attached';
aplusexpertAuthor Commented:

I will try this solution and let you know.

- Ankit
aplusexpertAuthor Commented:

This is not the solution I was looking for. The suggested solution above creates .txt file only, while I needed to get any type of file which was saved in that table, like .doc, .doc, .xls, .pdf, .jpg, .txt.

I have selected an alternate way to achieve this functionality by creating windows service.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.