Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

SQL Server 2008, connecting via SQL Server Authentication.

I have a sproc in DatabaseA which calls sp_send_dbmail in msdb to send an email with a file attachment. The file is on the db server, not on a remote fileshare.

The SQL account being used is not sysadmin, but does belong to the DatabaseMailUserRole in msdb.

Sending an email without an attachment is fine, but when an attachment is present it gives the error:

The client connection security context could not be impersonated. 
Attaching files require an integrated client login

There are a few articles/posts about this out there, but some seem to say conflicting things. I've been looking into impersonation, and one thing that does work is in the sproc in DatabaseA, to do the following:

EXECUTE AS LOGIN = 'sa' -- or any account with sysadmin privileges
EXECUTE msdb..sp_send_dbmail ....
REVERT

I wasn't expecting this to work as I thought to send attachments, you needed to use Windows Authentication. However it does work, but it means the lower privileged SQL account needs to be granted permission to IMPERSONATE the sa (or other sysadmin account).

Doing my due diligence as a dev before unleashing a DBA's nightmare into the wild...

My question is: What is a good/safe way of allowing a user authenticated via SQL Server (non sysadmin) to send email attachments from the local db server disk without opening up a security hole?

Update: Re: Credentials I've created a new Windows Login, created credentials for that account via SSMS, mapped those credentials to my limited privileges SQL account. I get the error:

Msg 22051, Level 16, State 1, Line 0
The client connection security context could not be impersonated. 
Attaching files require an integrated client login

I must be missing something!

share|improve this question
add comment

migrated from stackoverflow.com Feb 1 '12 at 14:07

This question came from our site for professional and enthusiast programmers.

2 Answers

You need to use Credentials:

A credential is a record that contains the authentication information (credentials) required to connect to a resource outside SQL Server. This information is used internally by SQL Server. Most credentials contain a Windows user name and password.

The information stored in a credential enables a user who has connected to SQL Server by way of SQL Server Authentication to access resources outside the server instance. When the external resource is Windows, the user is authenticated as the Windows user specified in the credential.

This way you can associate your SQL login that sends the mail with an NT credential that has file access to the attachment.

share|improve this answer
    
Thanks. I've tried per updated notes in my question, but still get an error –  AdaTheDev Feb 1 '12 at 20:39
add comment

I wasn't able to get this to work with Credentials, but I found another way to get it to work, without having to grant sysadmin impersonation rights to the account that the is used normally.

  1. Set the database as TRUSTWORTHY:

    ALTER DATABASE <your db> SET TRUSTWORTHY ON;
    
  2. Set the owner of the database a sysadmin user - in this case, I used sa

    EXEC [your db].dbo.sp_changedbowner @loginame = N'sa', @map = false
    
  3. Write a sproc to do the sending of mail, declared as so:

    CREATE PROCEDURE dbo.SendMailWithAttachments
    WITH EXECUTE AS OWNER
    AS
    BEGIN
            EXEC msdb.dbo.sp_send_dbmail ....,
                    @file_attachments = '\\some\remote\file';
    END
    

You could also just create a schema, like "mailXfer", that had a sysadmin owner, and then make the sproc in that (i.e. CREATE PROCEDURE mailXfer.SendMailWithAttachements)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.