Sends a message, and/or a query results set, and/or an attachment to the specified recipients.
xp_sendmail @recipient = recipient [; recipient2; [...; recipientn]]
[, @message = message]
[, @query = query]
[, @attachments = attachments]
[, @copy_recipients = recipient [; recipient2; [...; recipientn]]]
[, @blind_copy_recipients = recipient [; recipient2; [...; recipientn]]]
[, @subject = subject]
[, @type = type]
[, @attach_results = {'true' | 'false'}]
[, @no_output = {'true' | 'false'}]
[, @no_header = {'true' | 'false'}]
[, @width = width]
[, @separator = separator]
[, @echo_error = {'true' | 'false'}]
[, @set_user = user]
[, @dbuse = dbname]
where
IP[M | C].Vendorname.subclass
Message types beginning with IPM will appear in the inbox of the mail client, and will be found or read by xp_findnextmsg if the @type is NULL. Message types beginning with IPC do not appear in the inbox of the mail client and must be found or read by setting the @type parameter. The default is NULL.
For details about using custom message types, see the Microsoft Windows NT Resource Kit or the Microsoft Mail Technical Reference, available separately.
Note If @echo_error is true, xp_sendmail returns a status of 0 (success) if the mail is successfully sent, even if DB-Library errors or messages are encountered or the query returns no results.
The SQLMail session must be started prior to executing xp_sendmail. Sessions can be started either automatically (using the Auto Start Mail Client option in the Set Server Options dialog box of setup) or with xp_startmail. One SQLMail session supports all users on the SQL Server, but only one user at a time can send a message. Other users sending mail messages will automatically wait their turns until the first user's message is sent.
If @query is specified, xp_sendmail logs in to SQL Server as a client and executes the specified query. SQLMail makes a separate connection to SQL Server; it does not share the same connection as the original client connection that issued xp_sendmail. Note that @query can be blocked by a lock held by the client connection that issued xp_sendmail. For example, if you are updating a table within a transaction and you create a trigger for that update that attempts to select the updated row information as the @query parameter, the SQLMail connection will be blocked by the exclusive lock held on that row by the initial client connection.
For details, see the Microsoft SQL Server Administrator's Companion.
This example sends a message to user1 that the master database is full.
xp_sendmail 'user1', 'The master database is full.'
This example sends the message to user1 and user2, with copies sent to user3 and user4. It also specifies a subject line for the message.
xp_sendmail @recipients = 'user1;user2', @message = 'The master database is full.', @copy_recipients = 'user3;user4', @subject = 'Master Database Status'
This example sends the results of the sp_configure system stored procedure to user1.
xp_sendmail 'user1', @query = 'sp_configure'
This example sends the results of the query SELECT * from sysobjects as a text file attachment to user1. It includes a subject line for the mail and a message that will appear before the attachment. The @width option is used to prevent line breaks in the output lines.
xp_sendmail @recipients = 'user1', @query = 'select * from sysobjects', @subject = 'SQL Server Report', @message = 'The contents of sysobjects:', @attach_results = 'TRUE', @width = 250
This example shows how to send a message longer than 255 characters. Because the @message parameter is limited to the length of a varchar (as are all stored procedure parameters), this example writes the long message into a global temporary table consisting of a single text column. The contents of this temporary table are then sent in mail using the @query parameter.
CREATE TABLE ##texttab (c1 text) INSERT ##texttab values ('Put your long message here.') DECLARE @cmd varchar(56) SELECT @cmd = 'SELECT c1 FROM ##texttab EXEC master.dbo.xp_sendmail 'user1', @query = @cmd, @no_header= 'TRUE' DROP TABLE ##texttab
Execute permission defaults to the system administrator, who can grant permission to other users.
sp_processmail | xp_readmail |
xp_deletemail | xp_startmail |
xp_findnextmsg | xp_stopmail |