Questions tagged [sp-send-dbmail]

`sp-send-dbmail` is a built-in stored procedure available in SQL Server since 2005, which allows one to send e-mails using the SQL service, for alerting a DBA when a problem is detected or limited reporting.

This procedure allows one to send an e-mail directly from SQL, including an optional query. It is well-suited for simple alerts and debugging, and in a pinch can be used to distribute reports, though it supports only a limited amount of formatting.

To use this sproc, one must first configure a Database Mail account and profile. This is available from within SQL Studio under [Server Name] > Management > Database Mail > right-click > Configure Database Mail.

Here's a code sample for a very simple message:

EXEC msdb..sp_send_dbmail
    @profile_name = 'My Profile',
    @recipients = 'dba@widgetcorp.com',
    @from_address = 'database_alerts@widgetcorp.com',
    @subject = 'Error Detected',
    @body = 'A problem has been found, best get on it.'

sp-send-dbmail has a large number of parameters, well-documented on MSDN:

234 questions
1
vote
1 answer

Only some e-mail gets sent successfully from sp_send_dbmail (Database Mail) on SQL Server 2005

I want to find an answer to what is happening when I use sp_send_dbmail on SQL Server 2005. I have been googeling but without success and I don't know really where to turn now. This is the scenario: I have set up a mail function on our SQL server. I…
user542051
  • 11
  • 2
  • 4
1
vote
2 answers

sql server 2008 dbmail error sp_send_dbmail - cannot insert null value into 'last_mod_user' in table sysmail_mailitems

Our dbmail got hosed and I'm trying to resolve the issue. I've recently recreated sp_send_dbmail, but am not getting an odd error regarding a null value in last_mod_user column in table msdb.dbo.sysmail_mailitems (error 515)
mson
  • 7,762
  • 6
  • 40
  • 70
1
vote
1 answer

Testing the result set of an sp_send_dbmail query?

I'm trying to send an email using sp_send_dbmail in Sql Server 2005. I have both body text and a query being sent as an attachment. Sometimes, however, the query will return an empty dataset. Is there any way for me to test the results of the…
chama
  • 5,973
  • 14
  • 61
  • 77
1
vote
1 answer

How to include leading spaces with msdb.dbo.sp_send_dbmail?

I have the following data stored in SQL table and now I want to email this table using msdb.dbo.sp_send_dbmail. How can I include the leading space? CREATE TABLE #Temp2 ( NewBranchesAdded varchar(1), branch_code varchar(10), Branch…
salvationishere
  • 3,461
  • 29
  • 104
  • 143
1
vote
2 answers

Remove extra spaces in sp_send_dbmail

I have the following table: CREATE TABLE [dbo].[MyTable]( [Day_ID] [nvarchar](50) NULL, [SAS] [nvarchar](50) NULL, [STAMP] [datetime] NULL DEFAULT (getdate()) ) which contains this…
Nicolaesse
  • 2,554
  • 12
  • 46
  • 71
1
vote
1 answer

SQL- send same mail to multiple users from table separately

I want to send same mail to multiple email-ids fetched from a table separately using sp_send_dbmail . If I use below query, the mail is being sent once to all user in "To" section. But my requirement is to send mail to them separately. DECLARE…
Ayush Jain
  • 21
  • 2
1
vote
1 answer

Text After Query with sp-send-dbmail

I have the need to add body text after the query when using sp-send-dbmail. Currently my Stored procedure to send mail looks like this. ALTER PROCEDURE [dbo].[sp_SendSFRProcesingEmail] -- Add the parameters for the stored procedure here …
d90
  • 767
  • 2
  • 10
  • 28
1
vote
1 answer

SQL Email to CSV, Results have Line Splitting issues

This question is similar to several questions I've found where Line Feeds or carriage returns cause copying SQL grid results to Excel to have Line Splitting issues and your results will be cut off and started on the next row. Name Order# Date…
BWoods
  • 110
  • 1
  • 9
1
vote
1 answer

sp_send_dbmail set 2 queries for the @query parameter

all May i ask is it possible to set two queries for the stored procedure sp_send_dbmail? For example: EXEC msdb.dbo.sp_send_dbmail @profile_name = 'example', @recipients = 'example@XXX.com', @query = 'Select * from table1; Select * from…
ProgrammingBaKa
  • 363
  • 2
  • 19
1
vote
1 answer

SQL Email Formatting disappearing CR LF

I have a TSQL script that selects data and generates an email. My script is quite complex but I have cut it down to a simple example to demonstrate my problem. In simple terms when one of my fields go over a particular length (21 characters) the CR…
David P
  • 411
  • 7
  • 21
1
vote
2 answers

T-SQL; How to add sentences before and after a table in an html format using sp_send_dbmail

The MS Library site shows how to create an email and embed an html table result. https://msdn.microsoft.com/en-us/library/ms190307.aspx This is fine, but how should you add the beginning and end wording to the email body table please? (it must be…
Baz
  • 13
  • 4
1
vote
1 answer

No permission to execute sp_send_dbmail

I have no permission to execute sp_send_dbmail in server and no admin permission to configure that. So, I need an alternative solution. Maybe a script similar to sp_send_dbmail that could be scheduled in the server, sending emails with the query…
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
1
vote
2 answers

T-SQL inconsistent sp_send_dbmail results

I have a job set up on a handful of servers (all in the same domain). The job sends an email via sp_send_dbmail, and the subject of the email should look like the following format: =servername (ip address)= Weekly DB Backup to Azure Report So as a…
Jeffrey Van Laethem
  • 2,601
  • 1
  • 20
  • 30
1
vote
1 answer

How to change font color and text format of the email body of an sp_send_dbmail stored procedure?

I'm sending a generic email when records get uploaded to a sql table. My issue is that the font of the email being sent is green. How do I change that? And Where? Here is my code: DECLARE @MyRecipients varchar(max) SET @MyRecipients = …
Andy Hunn
  • 21
  • 2
  • 7
1
vote
0 answers

Using Service Broker to run stored procedure that calls sp_send_dbmail, blocking against itself

I have a stored procedure that I currently manually run whenever a user needs me to. I am developing an interface for them to be able to kick it off themselves. I've got it up and running using SQL Service Broker to queue the users' call to the…