sp_send_dbmail throws error 22051 (file is invalid) when attaching files from a Share (unc notation) - by Kunk

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 552940 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 4/21/2010 5:18:17 AM
Access Restriction Public


The following description is valid for SL Server 2005 and 2008.
I execute following statement :

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'DBMail'
  , @recipients = 'test.test@test.de'  -- valid recipient
  , @body = 'testmail with attachement from a filershare '
  , @subject = 'Testmail'
  , @file_attachments = N'\\server\Share\path\file'

Without @file_attachements the Mail was sent. So database mail is active an the profile is configured correctly.
The mail is also sent if the file resides on a local disk.
But sp_send_dbmail throws following message if the file resides on a shared filesystem :

Msg 22051, Level 16, State 1, Line 0
Attachment file \\server\Share\path\file is invalid.

The user executing the proc is sysadmin an has proper rights on the share.

So I assume that this is a bug.
Or do I miss something to configure?
Sign in to post a comment.
Posted by Robert Heinig II on 3/11/2013 at 8:57 AM
"Closed by Design" is not adequate. BOL at
still contradicts observed behaviour. Therefore, assuming the documentation states the design goal, 2008 R2 does not perform by design.

For new visitors: BOL suggests the T-SQL execution context login is impersonated for attachment file access. It is not. Therefore, permissions need to be granted to the service or service account. I'd suggest granting to the SQLServerMSSQLUser$... group for local paths, or, if network access is required, run under a domain account and grant access to that account. Additionally, do not rely on mapped network letters, even if the mapping was done under the service account itself (symlinks should work on the other hand).
Posted by R Herring on 11/3/2011 at 2:04 PM
I have a similar problem but the attachments are local files. I can send either file individually but if I specify both files in paramter then I get the error message.
This one works --@file_attachments='D:\MyFolder\Test_Data_20111103.zip'
This one works --@file_attachments='D:\MyFolder\Test_Demo_20111103.zip'
This one Fails -- @file_attachments='D:\MassMutualExtractedData\History\rHerringTest_Data_20111103.zip; D:\MassMutualExtractedData\History\rHerringTest_Data_20111103.zip'
Posted by Dathuraj on 7/12/2011 at 3:57 AM
Hi Team,
Even I am facing this issue when attaching multiple files (<200 KB each) but this works for a single file without any error.
My code:
    @file_attachments='\\lonsql99\EV Archived Reports\Copy of VaultStore3_ArchivedItems.csv;
    \\lonsql99\EV Archived Reports\Copy of VaultStore4_ArchivedItems.csv';

Have full access on the share folder for the domain account and all sql services are running under this account only.

Msg 22051, Level 16, State 1, Line 0
Attachment file
    \\lonsql99\EV Archived Reports\Copy of VaultStore4_ArchivedItems.csv is invalid.

Kindly suggest.

Posted by Kunk on 9/8/2010 at 12:49 AM
That is the trick. Sending files from a share works if the SQL Server engine account has read access to the file!
Thanks for that information!

Wolfgang Kunk
Posted by Microsoft on 9/7/2010 at 11:48 AM
Thank you for reporting this issue. We will come back to it later to review if documentation needs more clarity in this area.

Best regards,
Evgeny Krivosheev
Posted by tibid on 8/26/2010 at 5:01 PM
DbMail is executed in the context of Sql Engine, not the user. In order for this to work, you need Sql Server Engine Account (you can see it in services) to have READ access to the share. Otherwise you will get this error.
You cannot execute the mail process in the context of the user.
Posted by MDocteroff on 7/26/2010 at 10:46 AM
Is there any update on this issue??

Please let me know, I am experiencing the same problem.

Posted by Microsoft on 4/23/2010 at 10:06 AM
Thank you for reporting this issue - we are investigating and we will get back to you shortly.


Amy Lewis