question

liton avatar image
liton asked

db mail from cmd

Is it possible to send email attachment using dbmail from cmd? I'm using a batch file to send email attachment using dbmail but getting an error. Here is my code: sqlcmd -q "EXEC msdb.dbo.sp_send_dbmail @profile_name = 'ProfileName', @ recipients='example@test.com', @subject='Test Attachment ', @body='Using dbmail to send attachment', @file_attachments='C:\document\example.txt'" the error message that i'm getting is incorrect syntax near 'exec msdb.dbo.sp_send dbmail @profile_name='auditProfile'
dbmail
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image
JohnM answered
Yes, you should be able to send an attachment, as per the guidance: http://msdn.microsoft.com/en-us/library/ms190307(v=sql.90).aspx (this is for SQL 2005). The only thing that I notice is the lack of '\\' in your attachment string. Probably should read: "@file_attachments = 'C:\\ DocumentExample.txt'" Everything else, at least to me, looks correct. Although it's quite possible you have that in there and the forum monster's took it as an escape character. ;-)
5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I do have the forward slash but I think this forum removed it.
0 Likes 0 ·
Thought that is what might have happened. ;-)
0 Likes 0 ·
Does it matter if you specify a specific server connection with '-S' before the query?
0 Likes 0 ·
I'm not specifying any server connection. I'm about execute other stored proc using similar method but not this one
0 Likes 0 ·
I've edited the question to make the code bits appear as code blocks. This magically puts the \\s back. ;-)
0 Likes 0 ·
ThomasRushton avatar image
ThomasRushton answered
One thing that comes to my mind - you've shown the Query parameter above spread across multiple lines. Is that as it is in your script, or have you just formatted it for clarity? Something that might be worth doing, if you're having that sort of problem, is putting the whole of your SQL Query into a separate file, and sending that through to SQLCmd using the `-i` parameter.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I just formatted to make it clear to understand.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.