Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Dear All,

I am using SQL Server 2005 & it's unable to send an email when I am inserting a record in the inventory table. It's showing Mail queued after insert a record in the inventory table.

Please let me know the solution as early as possible.

Note: It's showing 'unsent' in the sent_status column when I am execute below query.
C#
" SELECT 'sysmail_allitems' as TableName, * FROM msdb.dbo.sysmail_allitems "

I write the queries as follows:
SQL
--Enable SQL SMail
use PracticeDB
go
sp_configure 'show advanced options',1
go
reconfigure with override
go
sp_configure 'Database Mail XPs',1
go
reconfigure
go

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'DBAC',
@email_address = 'Emailid@hotmail.com',
@display_name = 'Mohammed Abdul Mohsin',
@mailserver_name = 'mail.hotmail.com'

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'DBPN'

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'DBPN',
@account_name = 'DBAC',
@sequence_number = 1 ;

--Table

CREATE TABLE dbo.inventory (
item varchar(50),
price money
)
GO

--Trigger for Inventory Table

CREATE TRIGGER Trg_Inventory ON dbo.inventory AFTER INSERT AS
DECLARE @price money
DECLARE @item varchar(50)
SET @price  = (SELECT price FROM inserted)
SET @item = (SELECT item FROM inserted)
IF @price >= 1000
BEGIN
DECLARE @msg varchar(500)
SET @msg = 'Expensive item "' + @item + '" entered into inventory at $' + CAST(@price as varchar(10)) + '.'
EXEC msdb.dbo.sp_send_dbmail @recipients=N'EmailId@hotmail.com', @body= @msg,  @subject = 'SQL Server Trigger Mail', @profile_name = 'DBMailProfile'
END
GO
Posted
Updated 1-Jan-12 0:29am
v4

1 solution

Check that the service broker is actually running. For example try staring the broker using sysmail_start_sp[^]
 
Share this answer
 
Comments
Espen Harlinn 1-Jan-12 8:51am    
Good call :)
huzaifa.osman 2-Jan-12 0:03am    
It's getting an error 'Could not find Stored Procedure'. How I can fix it. Please reply to me as soon as possible?

USE PracticeDB ;
GO

EXECUTE dbo.sysmail_start_sp ;
GO

Error Msg: Could not find stored procedure 'dbo.sysmail_start_sp'.
Wendelius 2-Jan-12 2:56am    
The procedure is in msdb, so try using:

exec msdb..sysmail_start_sp;
huzaifa.osman 2-Jan-12 5:16am    
Dear Mr.Mika, It's running fine, but I am using PracticeDB Database.

Again, I am getting the same problems. It's failed to send an email from PracticeDB. Do have any solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900