Search
Home
Articles
Backup
Books
Certification
FAQ
Products
Replication
Scripts
Seminars
Training
TSQL

MSDN Fourms
Fort Worth SSUG
Oklahoma City SSDG

Resume

Champion Valley Pens

Configuring and Managing SQL Mail 1 2 3 4 5 6 7

Using XP_SendMail

The xp_sendmail procedure is the heart of the system if you’re creating an outbound messaging system, probably the most common application for SQL Mail. Sendmail lets you indicate where you want to send the message, what the contents are, the attachments, etc. All of the attributes to email that you’re probably used to can be indicated in this call.

xp_sendmail {[@recipients =] 'recipients [;…n]'}
[,[@message =] 'message']
[,[@query =] 'query']
[,[@attachments =] attachments]
[,[@copy_recipients =] 'copy_recipients [;…n]'
[,[@blind_copy_recipients =] 'blind_copy_recipients [;…n]'
[,[@subject =] 'subject']
[,[@type =] type]
[,[@attach_results =] 'attach_value']
[,[@no_output =] 'output_value']
[,[@no_header =] 'header_value']
[,[@width =] width]
[,[@separator =] separator]
[,[@echo_error =] 'echo_value']
[,[@set_user =] user]
[,[@dbuse =] database]

The parameters are outlined in the table below.

Parameter Description
@recipients This is a semicolon separated list of recipients that the message will be sent to.
@message The message that will be sent. Note that it can only be 8,000 bytes long.
@query Any valid query to be executed and attached to the mail message. It can not reference special #Inserted and #Deleted tables.
@attachments A file to attach to the message. Note that this file path is on the server, not the client system.
@subject The subject of the message.
@attach_results By default, the results of the query are simply placed in the body of the message. If instead you want them to be added as an attachment, set this to true. This can be handy if your query is particularly large – it can help avoid wrapping. Remember, you don’t control the recipients mail system and how they view the mail message, but attachments generally come through as sent.
@no_header Suppresses the column headers for a query.
@set_user The security context the query should be run under. The default is guest. There is an apparent bug with using this option. Any user name that is specified returns an error message stating that user does not exist. Also even if the guest account is not enabled, a query will still execute. This query is executed under the sa account.
@dbuser The database context to run the query in. You should always specify the database.

Configuring and Managing SQL Mail 1 2 3 4 5 6 7

Michael R. Hotek

All content on this site, except where noted, represents an original work of Michael R. Hotek and is protected by applicable copyright laws. The SQL Server FAQ is the sole work of Neil Pike. No page, portion of a page, or download may be used for commercial purposes in whole or in part without the express, written permission of the applicable author.