发送存储在表中的电子邮件的Sql Server存储过程

时间:2015-06-12 13:09:29

标签: database email stored-procedures sql-server-2008-r2

我目前有一个发送电子邮件的存储过程,但是收到错误。此外,是否有一些错误检查我可以确保电子邮件发送到位?我将数据插入名为outbound_email_queue的表中,该表定义为:

CREATE TABLE dbo.outbound_email_queue(

email_id int IDENTITY(1,1) NOT NULL,
out_type varchar(3) NOT NULL,
send_time datetime NULL,
from_addr varchar(100) NOT NULL,
to_addr varchar(254) NOT NULL,
reply_addr varchar(60) NULL,
subject varchar(254) NOT NULL,
letter_body varchar(8000) NOT NULL

我正在使用的存储过程如下。 它在Commit语句中出错。

它似乎做了删除,即使我省略了提交。即使我让它执行该程序,它也会清空表,但它似乎没有发送电子邮件。

CREATE PROCEDURE [dbo].[uspEmail]
as
BEGIN

Declare @email_id as bigint
Declare @email_type as nvarchar(3) 
Declare @from_add as nvarchar(100)
Declare @to_add as nvarchar(254) 
Declare @reply_add as nvarchar(60)
Declare @email_prof as nvarchar(80)   
Declare @MySubject as nvarchar(254) 
Declare @Message as nvarchar(MAX) 

Declare EmailCursor CURSOR FAST_FORWARD FOR 
    Select email_id, out_type, from_addr, to_addr, reply_addr, subject,   
     letter_body from outbound_email_queue 

set @email_prof = (select email_profile from system_file)

OPEN EmailCursor 
FETCH NEXT FROM EmailCursor INTO @email_id, @email_type, @from_add, @to_add,    
@reply_add, @MySubject, @Message

WHILE @@FETCH_STATUS = 0 BEGIN    -- send the email     


EXEC msdb.dbo.sp_send_dbmail       
@profile_name = @email_prof,     
@recipients = @to_add, 
@reply_to = @reply_add, 
@body_format = 'HTML', 
@from_address = @from_add,    
@subject = @MySubject,       
@body = @Message    

DELETE FROM OUTBOUND_EMAIL_QUEUE WHERE EMAIL_ID = @email_id

FETCH NEXT FROM EmailCursor INTO @email_id, @email_type, @from_add, @to_add,     
@reply_add, @MySubject, @Message
END CLOSE EmailCursor 
DEALLOCATE EmailCursor

COMMIT

END

GO

0 个答案:

没有答案