在SQL Server 2008中使用sp_send_dbmail只传送少量邮件

时间:2014-08-09 14:57:11

标签: sql sql-server sql-server-2008 tsql

我从表中的列中提取收件人列表,并且我正在运行存储过程sp_send_dbmail以将电子邮件发送给这些收件人。

总共有大约200名收件人。

具有讽刺意味的是,即使我收到的邮件是邮件排队,也只有少数邮件已发送。数据库邮件已正确配置,我正在使用Public数据库配置文件。

当我检查msdb.dbo.sysmail_mailitems表时,sent_status列中的预期值对于为其发送邮件的收件人为1,其余为0或2。

我完全确定收件人列表是100%正确的。我们有解决此问题的解决方法吗?

以下是我正在运行的代码:

CREATE procedure [dbo].[sp_dataRefreshNotification]  
AS  
BEGIN  
DECLARE @ToMail VARCHAR(20)  
DECLARE @Body1 VARCHAR(MAX) =   
'Dear User,  
Data has been refreshed.  
Regards,  
IT Support Team  

Note: This is an auto generated e-mail, please do not reply this mail. '      

SELECT DISTINCT RecipientAddress FROM dbo.RecipientAddressList  
OPEN notification_cursor  
FETCH NEXT FROM notification_cursor  
INTO @ToMail  

WHILE @@FETCH_STATUS = 0  
BEGIN  
EXEC msdb.dbo.sp_send_dbmail  
@profile_name  ='aaaa',    
@Recipients = @Tomail,  
@Subject = 'Required Data',      
@Body = @Body1  
FETCH NEXT FROM notification_cursor  
INTO @ToMail  
END  
CLOSE notification_cursor  
DEALLOCATE notification_cursor  
END  

2 个答案:

答案 0 :(得分:1)

使用Wait for Delay功能将打破光标的每个实例。

CREATE procedure [dbo].[sp_dataRefreshNotification]  
AS  
BEGIN  
DECLARE @ToMail VARCHAR(20)  
DECLARE @Body1 VARCHAR(MAX) =   
'Dear User,  
Data has been refreshed.  
Regards,  
IT Support Team  

Note: This is an auto generated e-mail, please do not reply this mail. '      

SELECT DISTINCT RecipientAddress FROM dbo.RecipientAddressList  
OPEN notification_cursor  
FETCH NEXT FROM notification_cursor  
INTO @ToMail  

WHILE @@FETCH_STATUS = 0  
BEGIN
Waitfor Delay '000:00:10' 
EXEC msdb.dbo.sp_send_dbmail  
@profile_name  ='aaaa',    
@Recipients = @Tomail,  
@Subject = 'Required Data',      
@Body = @Body1  
FETCH NEXT FROM notification_cursor  
INTO @ToMail  
END  
CLOSE notification_cursor  
DEALLOCATE notification_cursor  
END  

答案 1 :(得分:0)

你不需要光标,使用简单的选择查询可以做同样的事情,见下文

CREATE PROCEDURE [dbo].[sp_dataRefreshNotification]  
AS  
BEGIN  
  SET NOCOUNT ON;
    DECLARE @ToMail VARCHAR(MAX);  
    DECLARE @Body1 VARCHAR(MAX);

SET @Body1  =   'Dear User, ' + CHAR(10) + 
                'Data has been refreshed.   ' + CHAR(10) + 
                'Regards,   ' + CHAR(10) + 
                'IT Support Team   ' + CHAR(10) + CHAR(10) + 

                'Note: This is an auto generated e-mail, please do not reply this mail.  ' + CHAR(10)    


    SELECT @ToMail = COALESCE(@ToMail+';' ,'') + RecipientAddress 
    FROM (SELECT DISTINCT RecipientAddress 
          FROM dbo.RecipientAddressList) t

    EXEC msdb.dbo.sp_send_dbmail  @profile_name  ='aaaa'
                                 ,@Recipients = @Tomail
                                 ,@Subject = 'Required Data'
                                 ,@Body = @Body1  

END