sp_send_dbmail with Binary Attachment Issue - sql

I am attempting to use sp_send_dbmail to send out an attachment.
The attachment is stored in a varbinary(MAX) column in a table. Here is my query:
EXEC msdb.dbo.sp_send_dbmail
#recipients='mick.walker#somewhere.com',
#subject = 'Test Attachment',
#body = 'Test',
#body_format = 'HTML',
#profile_name = 'intranetadmin',
#query = 'Select DocumentData from [myDB].[dbo].[Documents] Where DocumentID = 8',
#query_result_header = 0,
#attach_query_result_as_file = 1,
#query_attachment_filename = 'Test.pdf',
#exclude_query_output = 1,
#query_no_truncate = 0;
The email sends sucessfully with a pdf attachment. However when it comes to opening the attachment, I get an error. I think the size of the file is being truncated, even though I explicitaally state no to in my query.
I have checked the MAX allowed message size in the Database Mail settings and it is currently 104857600 bytes (100mb), the files I am attempting to send, are nowhere near this size - so I am a little puzzled.

I know this is an old thread but I have just come across the same issue. The problem is SQL reported an error and has stored the error message in the attachment.
Change the name of the attachment to have a .txt extension and send the email. Open the .txt file and view the error. It is probably something to so with a security configuration.

Try setting #query_no_truncate parameter to 1. When large variable length data types are used in the query and this option is 0 or not specified data gets cut to 256 characters. Reference: http://msdn.microsoft.com/en-us/library/ms190307.aspx

Related

How to send message through sql with an image

In my SQL, I already created profile & account.
I also know how to send the message though code :
USE msdb
GO
EXEC sp_send_dbmail #profile_name='yourprofilename',
#recipients='test#Example.com',
#subject='Test message',
#body='This is the body of the test message.
Congrates Database Mail Received By you Successfully.'
** Above is an example
However, I was wondering how can I insert an image into the message.

sp_send_dbmail blocks and causes PREEMPTIVE_OS_GETPROCADDRESS

I have a strange issue with msdb.dbo.sp_send_dbmailwithin a stored procedure (code simplified):
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- ...
SET #msg = N'...';
SET #filename = N'...';
EXEC msdb.dbo.sp_send_dbmail
#recipients = 'mailaddr#example.com'
, #blind_copy_recipients = 'another_mailaddr#example.com'
, #from_address = 'senders_mail#example.com'
, #subject = N'...'
, #body = #msg
, #query = N'SET NOCOUNT ON; SELECT <something> FROM <a_view>;'
, #execute_query_database = N'<same database the proc resides in>'
, #query_result_width = 8000
, #attach_query_result_as_file = 1
, #query_attachment_filename = #filename
, #query_result_header = 1
, #query_result_separator = ';'
, #query_result_no_padding = 1
, #exclude_query_output = 1;
I have a database user which is member of db_datareaderand EXECUTEpermissions for the stored procedure. Furthermore the assigned server login is mapped to msdband a member of the DatabaseMailUserRole. There is only 1 single mail profile which is public and flagged as default.
Everything works fine from SSMS: connect with the user's credentials and execute the stored procedure. Great!
The first oddity: if I'm logged in as sysadmin and try to EXECUTE AS it doesn't work. Okay, I found something that points out that there are issues with this.
But the main issue is this: I call the procedure from a 3rd party Java application which connects using the jTDS driver (don't know if this is important). The applicaton executes the procedure ... and nothing else happened (no log entries, the task freezes).
In the activity monitor I see the following:
Process <...>
Database master (??? I've never connected to this
db!)
Task State Running
Wait Type PREEMPTIVE_OS_GETPROCADDRESS
Head Blocker 1
To make things worse I cannot kill this process. If I try this the Command column in the activity monitor shows only KILLED/ROLLBACK.
KILL <PROCESS-ID>shows
spid <...>: Transaction rollback in progress. Estimated rollback completion: 0% Estimated time left: 0 seconds.
I have to restart the whole instance to get rid of the process.
What is happening here?
Finally I found the answer here: blocking from xp_sysmail_format_query waittype of preemptive_os_getprocaddress
It seems that the Java application opens a transaction explicitly (there are 2 calls to stored procedures consecutively). After setting the Auto-Commit option of the database adapter to on everything work's fine.

Compilation errors to send email with TRIGGER sql

Hello i'm working on PL/SQL Developper and i've got an annoying error ... I'm trying to make a trigger to send an email when something is added in the "JDEF" database.
This is my code :
CREATE OR REPLACE TRIGGER Send_Email
AFTER INSERT
ON JDEF
BEGIN
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'me',
#recipients = 'example#ent.com',
#body = 'The stored procedure finished successfully.',
#subject = 'Automated Success Message'
END
The error compilation are for those two lines : EXEC msdb.dbo.sp_send_dbmail AND #profile_name = 'me'. They speak about "MSDB" and ",' symbols who are replaced by other one to compile.
Have you any ideas to help me ?
thx
Your code looks like mssql in oracle standard way to send email is for example:
UTL_MAIL.send(sender => 'me#ent.com',
recipients => 'example#ent.com',
subject => 'Automated Success Message',
message => 'The stored procedure finished successfully.',
mime_type => 'text; charset=us-ascii');
But it needs being enabled before you can use UTL_MAIL. Another package to send mail is UTL_TCP
This may be helpful: http://www.orafaq.com/wiki/Send_mail_from_PL/SQL

SQL Server OLEDB error with no details

I have a SQL Server Agent job running, which uses a stored procedure to do several operations, then exports some data to an xls spreadsheet and emails that spreadsheet. Most of the time it works, but several times a month the job fails with the error:
OLE DB provider 'Microsoft.Jet.OLEDB.4.0' reported an error. The provider did not give any information about the error. [SQLSTATE 42000] (Error 7399). The step failed.
Thanks, Microsoft, for the detailed error message. Anyway, the short term fix is usually to simply re-run the job. Usually this works, but in rarer cases it does not, and I must restart the SQL Server instance.
Here is how my code interacts with OLEDB:
Insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 5.0;Database=\\Excel\POStatus\POStatus.xls;',
'SELECT * FROM [POStatus$]')
Select --Tons of columns with tons of math and functions
FROM --5 tables joined together (left joins)
WHERE -- Tons of where conditions
Order by --Case statement for custom sorting
Set #vCommand = 'copy \\Excel\POStatus\POStatus.xls \\Excel\POStatus\POStatus_' + #vDate + '.xls'
EXEC master..xp_cmdshell #vCommand , NO_OUTPUT
... omitted for brevity...
Set #nvSubject = ' POStatus ' + #vDate
Set #nvMessage = ' This is an automated message, please respond to the IS department, thank you '
Set #nvMessage = #nvMessage + char(13) + char(10)
Set #nvAttachments = '\\Excel\POStatus\POStatus_' + #vDate + '.xls'
Exec master..xp_sendmail
#recipients = #nvRecipients , #copy_recipients = #nvCopy_recipients ,
#subject = #nvSubject , #message = #nvMessage ,
#query = #nvQuery , #width = #iWidth , #attachments = #nvAttachments
So, what is the cause of this, and how can I prevent it?
When you call OPENROWSET, it load the DLL for OLEDB provider for Excel. These operations happens inside the SQL Server Stack memory. When a lot of call to older DLL (ActiveX/COM) are made, it's not impossible that your stack might get full. You can resolve this in 2 ways:
1) You make these operations outside a TSQL code. You can use a SSIS package for example but you have to change your code not to use OPENROWSET. You can use the DTS Wizard to do most of the job. This is my personnal recommandation !
2) You can also set SQL Server to have a bigger memory Stack by using the -g command parameter. I think 256 MB is the default and you can set it to 512. To do that you need to open the SQL Server Configuration Manager. Depending on your version, you should have a place to change the "Startup Parameters"

sql db mail problem while sending in bulk

I am using db mail(sql server 2005) to send bulk email(>2000).
The code tat i use is,
exec msdb..sp_send_dbmail
#profile_name = 'My Profile',
#recipients = 'raghav.cinch#gmail.com',
#subject = 'test',
#body = 'test',
#body_format = 'HTML'
If i send few emails(less than 100), all emails are sent successfully. But only bulk emails give me error.
The error I get is,
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 8 (2011-09-27T21:29:17). Exception Message: Cannot send mails to mail server. (Unable to send to all recipients.).
)
The error comes after 100 or 105 mails. The email addresses are correct and if i sent in cycles of 100, all mails are sent successfully.
I believe it should be some configuration settings tat need to be tweaked. Could someone pls help me in fixing it..
Thanks in advance.
Changed the iis6 settings, max queue length for the smtp server and this worked like charm.