xp_sendmail (Transact-SQL)
Sends an e-mail message, which may include a query result set attachment, to the specified recipients. This extended stored procedure uses SQL Mail to send the message.
Note |
|---|
This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. To send mail from SQL Server, use Database Mail. |
xp_sendmail { [ @recipients= ] 'recipients [ ;...n ]' }
[ ,[ @message= ] 'message' ]
[ ,[ @query= ] 'query' ]
[ ,[ @attachments= ] 'attachments [ ;...n ]' ]
[ ,[ @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 SQL Mail session must be started before xp_sendmail is run. Sessions can be started either automatically or with xp_startmail. For more information about how to set up a SQL Mail session automatically, see Configuring Extended MAPI Mail Profiles. One SQL Mail session supports all users for the SQL Server instance, but only one user at a time can send a message. Other users sending mail messages 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 runs the specified query. SQL Mail makes a separate connection to SQL Server; it does not share the same connection as the original client connection issuing xp_sendmail.
Note |
|---|
query can be blocked by a lock held by the client connection issued xp_sendmail. For example, if you are updating a table within a transaction and you create a trigger for update that attempts to select the same updated row information as the query parameter, the SQL Mail connection is blocked by the exclusive lock held on the row by the initial client connection. |
xp_sendmail runs in the security context of SQL Server. A valid user of xp_sendmail can access files for attachment to a mail message in an administrator's security context. If users other than system administrators must access xp_sendmail and you want to guard against unsecured access to attachment files, the system administrator can create a stored procedure that calls xp_sendmail and provides the needed functionality, but which does not expose the attachments parameter. This stored procedure must be defined in the master database. The system administrator then grants execute permission on the stored procedure to the necessary users without granting permission to the underlying xp_sendmail procedure.
xp_sendmail sends a message and a query result set or an attachment to specified recipients, and it uses a bound connection for the query parameter. The query connection made by SQL Mail is not blocked by locks held by the client that issues the xp_sendmail request. This makes xp_sendmail easier to use from within triggers. The query statement, however, cannot refer to the logical inserted and deleted tables that are only available within a trigger.
Note |
|---|
An access violation can result from an attempt to run xp_sendmail when the post office and address book are on a file share that the MSSQLServer service cannot access because of inadequate permissions. |
xp_sendmail does not fully support the xml data type. Queries that use the xml data type may be formatted incorrectly. Use Database Mail to send e-mail that includes xml data.
A. Sending a message to a single recipient
The following example sends a message to user Dan Wilson (e-mail is danw) that the master database is full.
EXEC master.dbo.xp_sendmail
@recipients=N'danw@Adventure-Works.com',
@message=N'The master database is full.' ;
B. Sending a message to multiple recipients
The following example sends the message to users Dan Wilson and Ashvini Sharma (e-mail is ashvinis), with a copy sent to Peter Connelly (e-mail is peterc). The example also specifies a subject line for the message.
EXEC master.dbo.xp_sendmail @recipients=N'danw@Adventure-Works.com;ashvinis@Adventure-Works.com', @message=N'The master database is full.', @copy_recipients=N'peterc@Adventure-Works.com', @subject=N'Master database status' ; GO
C. Sending results
The following example sends the results of sp_configure to Dan Wilson.
EXEC master.dbo.xp_sendmail @recipients=N'danw@Adventure-Works.com', @query = N'EXEC sp_configure' ; GO
D. Sending results as an attached file
The following example sends the results of the query SELECT * FROM INFORMATION_SCHEMA.TABLES as a text file attachment to Dan Wilson. The example includes a subject line for the mail and a message that will appear before the attachment. The @width parameter is used to prevent line breaks in the output lines.
EXEC master.dbo.xp_sendmail @recipients = N'danw@Adventure-Works.com', @query = N'SELECT * FROM INFORMATION_SCHEMA.TABLES', @subject = N'SQL Server Report', @message = N'The contents of INFORMATION_SCHEMA.TABLES:', @attach_results = 'TRUE', @width = 250 ;
E. Sending messages longer than 7,990 byte characters
The following example shows how to send a message longer than 7,990 byte characters. Because message is limited to the length of a VARCHAR (less row overhead, as are all stored procedure parameters), this example writes the long message into a global temporary table that is made up of a single text column. The contents of this temporary table are then sent in mail by using the @query parameter.
CREATE TABLE ##mail_body(c1 NVARCHAR(4000)) ;
DECLARE @cmd VARCHAR(56) ;
INSERT ##mail_body(c1)
VALUES ('Put your long message here.') ;
SET @cmd = 'SELECT c1 FROM ##mail_body' ;
EXEC master.dbo.xp_sendmail
@recipients = 'danw@Adventure-Works.com',
@query = @cmd,
@no_header= 'TRUE' ;
DROP TABLE ##mail_body ;
Note