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 introduced in SQL Server 2005. Queries that use the xml data type may be formatted incorrectly. Use Database Mail to send e-mail that includes xml data.