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
2
votes
0 answers

Embedded images sent via sp_send_dbmail are not working in gmail or Outlook 2013 for OSX

I'm having a problem where emails sent through Sql Server are not properly being embedded in some mail clients - notably gmail and Outlook 2013 on OSX. (Outlook 2013 on PC is fine) Here's a sample call that will demonstrate the problem in my…
Joe Zack
  • 3,268
  • 2
  • 31
  • 37
2
votes
1 answer

Sending email using sp_send_dbmail() does not work sometimes

I am sending notification email using SQL Server but sometimes emails aren't sent to users. Here is my SQL table that I store emails which it will be sent to users CREATE TABLE [dbo].[EmailNotification]( [Id] [INT] IDENTITY(1,1) NOT…
Nurlan
  • 105
  • 1
  • 10
2
votes
1 answer

Working with sub-folders in file path with sp_send_dbmail and T-SQL

Is it possible to populate a sub-folder name in the @file_attachements path with some use of a wildcard? I run a query to get the document name I'm looking for. I then assign that to a variable and concatenate the @file_location with @document_name…
phildown
  • 23
  • 2
2
votes
1 answer

sp_send_dbmail is sending a blank email when the queried table is empty

I am using the stored procedure msdb.dbo.sp_send_dbmail to send an HTML email with a queried table build into the text. I used the following code example from Microsoft's MSDN to create my email sending stored procedure: DECLARE @tableHTML …
Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
2
votes
3 answers

SQL sp_send_dbmail send email with attachment, pdf file got corrupted

I have a stored procedure to send email, which will be invoke when a record is inserted to a table. I wanted to attach a document added to the table and send an email.I can get the attached file but its corrupted when I open it(It says file has been…
Dagim Belayneh
  • 141
  • 1
  • 2
  • 6
2
votes
3 answers

Formatting query data in HTML email from SQL Server 2008

I am trying to get SQL Server 2008 to send HTML-formatted email, however one of the fields I am pulling in my query is a "money" data type and therefore displays with 3 digits after the decimal place and I can't seem to get the dollar sign to show…
LegalEagle
  • 97
  • 3
  • 15
2
votes
1 answer

How to Check MSDB for User Permissions/Roles

I have a connectionstring Before using sp_send_dbmail and through a sql query, how to…
usefulBee
  • 9,250
  • 10
  • 51
  • 89
2
votes
2 answers

sp_send_dbmail Unable to Relay

I am trying to figure out something. I have a Stored Procedure that is executing the sp_send_dbmail system stored procedure to send an email through our open relay (locked down to the IP address of the SQL Server) on our exchange server. We are BCC…
jlimited
  • 685
  • 2
  • 11
  • 20
2
votes
0 answers

db_send_mail in html table format using CTE

I have a query which I need to send using mail in html table format. I don't know how to structure exactly in the html table. Below is the query I am using WITH app AS ( SELECT TOP 25…
Simple_Noob
  • 51
  • 1
  • 3
2
votes
2 answers

send dbmail on @@error from sql server 2005

I am trying to send database mail when error occurs inside the transaction.My setup for dbo.sp_send_dbmail is correct , when I execute the proc I do get an email within 1 min. However when I try to use dbo.sp_send_dbmail inside another proc within…
Zeus
  • 3,091
  • 6
  • 47
  • 60
2
votes
1 answer

Send email from SQL server for each row of a dataset

I have built a stored procedure for sending an email reminder to a set of employees each week. The SQL server agent runs a scheduled procedure each week that builds this weeks dataset of employees and then I need to have each of the employees…
defect833
  • 275
  • 7
  • 22
2
votes
1 answer

Use send_dbmail to send an email for each row in sql table

I am having difficulties figuring out how to make send_dbmail loop through my table and send a new email for each record. My table is dropped and re-created each time this package runs, sometimes it will have zero records, other times it may have a…
Jeremy
  • 131
  • 1
  • 10
2
votes
2 answers

Can't CATCH Errors from sp_send_dbmail

We use sp_send_dbmail to notify us of certain problems and sometimes we attach files to the emails. Occasionally we will get an Error 32 (file in use) error. I've put some code together to send the email without the attachment in this situation but…
user1106421
  • 39
  • 1
  • 3
2
votes
1 answer

sp_send_dbmail - formatting row in table as red for an alert

I'm using something similar to example C on this MSDN page: http://msdn.microsoft.com/en-us/library/ms190307.aspx DECLARE @tableHTML NVARCHAR(MAX) ; SET @tableHTML = N'

Work Order Report

' + N'' + …
NealWalters
  • 17,197
  • 42
  • 141
  • 251
1
vote
0 answers

Streaming htm file with an image into sp_send_dbmail

I have a stored procedure the runs sp_makewebtask to create a .HTM file. Thie file consists of an image and the output of a query along with some addtional verbage. What I would like to do is to send out that .HTM file using sp_send_dbmail. I know…
user142253
  • 494
  • 1
  • 6
  • 16
1 2
3
15 16