Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

capturing errors thrown by xp_sendmail

Status
Not open for further replies.

spencer2

Programmer
Apr 14, 2004
3
US
I have a procedure that calls xp_sendmail. I would like to capture errors that occur when xp_sendmail loses its connection to exchange, but even though the message is outputted to the screen in query analyzer and captured by the sql agent job history when running the procedure as a job, I cannot seem to capture the error message in a variable in my procedure. Here is an example:

I haven't set up sql mail so that it will throw an error. I run the following code:

declare @err int
exec @err = xp_sendmail 'dousch03', 'test'

--select @err = @@error

Print @err

I get the following result:

Server: Msg 18030, Level 16, State 1, Line 0
xp_sendmail: Either there is no default mail client or the current mail client cannot fulfill the messaging request. Please run Microsoft Outlook and set it as the default mail client.
1

I would like @err to be 18030 instead of 1. Is it possible to capture the 18030 message number somehow? Help!
 
I found a possible way to get the message -- you can use DBCC OUTPUTBUFFER (passing the spid) to get the output xp_sendmail sends to the console and parse through that to get the error number/message. If anyone can think of an easier way, let me know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top