Reads a mail message from the Microsoft® SQL Server™ mail inbox. This procedure is used by sp_processmail to process all mail in the SQL Server inbox.
xp_readmail [[@msg_id =] 'message_number'] [, [@type =] 'type' [OUTPUT]]
[,[@peek =] 'peek']
[,[@suppress_attach =] 'suppress_attach']
[,[@originator =] 'sender' OUTPUT]
[,[@subject =] 'subject' OUTPUT]
[,[@message =] 'message' OUTPUT]
[,[@recipients =] 'recipients [;...n]' OUTPUT]
[,[@cc_list =] 'copy_recipients [;...n]' OUTPUT]
[,[@bcc_list =] 'blind_copy_recipients [;...n]' OUTPUT]
[,[@date_received =] 'date' OUTPUT]
[,[@unread =] 'unread_value' OUTPUT]
[,[@attachments =] 'attachments [;...n]' OUTPUT])
[,[@skip_bytes =] bytes_to_skip OUTPUT]
[,[@msg_length =] length_in_bytes OUTPUT]
[,[@originator_address =] 'sender_address' OUTPUT]]
IP[M | C].Vendorname.subclass
If used on input, this must define the type for a specific message; type is ignored on input if the message_number is NULL. type is varchar(255), with a default of NULL.
0 (success) or 1 (failure)
xp_readmail returns a result set with these columns (older messages appear first).
Column name | Description |
---|---|
Originator | Sender of e-mail message |
Date Received | Date the e-mail message was received |
Recipients | The people to whom the message was sent |
CC List | The people on the CC’d line of the e-mail message |
BCC List | The people on the BCC’d line of the e-mail message |
Subject | Subject line of the e-mail message |
Message | Message body (text) |
Unread | Whether this message is unread |
Attachments | Any attachments for the message |
Message ID | Message ID |
Type | Message type |
Any failure except an invalid parameter is logged to the Microsoft Windows NT® application log.
There are two ways to use xp_readmail:
To return the contents of the inbox as a result set to the client either set message_number to NULL or do not include message_number. In this situation, type can be used to read specific messages. You can specify peek and suppress_attach as input parameters to control the way the message is read.
To read a single message from the inbox, supply a valid message_number returned by xp_findnextmsg as an input parameter to xp_readmail. You can specify peek and suppress_attach as input parameters to control the way the message is read. When using peek and suppress_attach with this method, all other parameters are optional output parameters containing specific information from the message to be read.
You can view an example of using xp_findnextmsg as an input parameter to xp_readmail by executing the following command:
sp_helptext 'sp_processmail'
When used to read a single message, xp_readmail can read message text of longer than 255 bytes in sections. Use length_in_bytes and length_in_bytes to read message text of longer than 255 bytes in sections. Using length_in_bytes as both an input and output parameter allows coding of a loop to process the entire message text. The following code shows an example of such a loop, assuming message_number is set to a valid message identifier returned by xp_findnextmsg.
USE master
WHILE (1 = 1)
BEGIN
EXEC @status = xp_readmail @msg_id = @msg_id,
@message = @message OUTPUT,
@skip_bytes = @skip_bytes OUTPUT,
@msg_length = @msg_length OUTPUT
IF @status <> 0 BREAK
SELECT 'msg_id' = @msg_id, 'msg_part' = @message
IF @skip_bytes = @msg_length BREAK
END
Execute permissions for xp_readmail default to the members of the sysadmin fixed server role but can be granted to other users.
This example returns the status when reading a message. In this example, the value of a message ID from xp_findnextmsg is placed in the local variable @message_id and passed to xp_readmail.
USE master
EXEC @status = xp_readmail @msg_id = @message_id,
@originator = @originator OUTPUT,
@cc_list = @cc_list OUTPUT,
@subject = @msgsubject OUTPUT,
@message = @query OUTPUT,
@peek = 'TRUE',
@suppress_attach = 'TRUE'
sp_processmail | xp_startmail |
xp_deletemail | xp_stopmail |
xp_findnextmsg | System Stored Procedures (SQL Mail Extended Procedures) |
xp_sendmail |