2

我相信 dbMail 已正确设置,我可以使用它向自己发送电子邮件。但我有一个预定的工作,看起来像;

DECLARE @Forename VARCHAR(50) 
DECLARE @EmailAddress VARCHAR(50)
DECLARE @subject VARCHAR(500)
DECLARE @emailsubject VARCHAR(500)
Declare @bodypre as  varchar(5000)
DECLARE @body VARCHAR(5000)
declare @recipients VARCHAR(5000)
Declare @emailStop  as int
declare @replaceApproverName as varchar(20)
declare @link as varchar(100)
declare @replaceNextLine as varchar(20)
declare @profile_name as varchar(50)
declare @bodypost as varchar(5000)
declare @footerMessage as varchar(1000)
set @replaceApproverName ='$Approver'
set @replaceNextLine ='$NextLine'
set @link ='$Link'
DECLARE @NewLineChar AS CHAR(1) = CHAR(13) 

select @emailsubject=EmailMessageSubject,
@bodypre = EmailMessageBody,
@emailStop = EmailStop_Flag
from STAS.dbo.MC_STAS_EmailMessage where TimeSheetStatusID = 2 

SET @bodypre =Replace(@bodypre ,@link, 'http://liveweb.website.com/stas');
select @footerMessage =EmailMessageBody  from STAS.dbo.MC_STAS_EmailMessage where MessageType='Footer'
SET @bodypre += @footerMessage;
SET @bodypre =Replace(@bodypre ,@replaceNextLine, @NewLineChar) ; 
DECLARE emailCursor CURSOR  FOR 
SELECT  distinct EmailAddress,Forename  from [STAS].[dbo].[NotifictationEmailID_View] vw
OPEN emailCursor FETCH NEXT FROM 
emailCursor INTO @EmailAddress,@Forename WHILE @@FETCH_STATUS = 0 
BEGIN       
if(@emailStop = 1)
set @EmailAddress = 'xxx.xxxx@xxxxx.co.uk'
set @bodypost  = Replace(@bodypre,@replaceApproverName,@Forename) 
EXEC msdb.dbo.sp_send_dbmail 
@profile_name='STAS Alert',                         
                    @recipients =@EmailAddress,   
                    @subject=@emailsubject,                                 
                    @body =@bodypost;

FETCH NEXT FROM emailCursor INTO @EmailAddress,@Forename END 
CLOSE emailCursor 
DEALLOCATE emailCursor 

当我查看数据库邮件错误日志时,我看到了这个;

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-09-04T14:03:48). Exception Message: Cannot send mails to mail server. (The operation has timed out.).
Sending Mail using Account 1 . Exception Message: Cannot send mails to mail server. (Failure sending mail.).

然而,配置文件“STAS Alert”已设置,我可以使用它向自己发送电子邮件。那么为什么会发生这种情况,我应该怎么做才能解决它?

4

1 回答 1

2

您检查过 SQL Server 代理属性/警报系统吗?应该启用邮件配置文件,并且应该正确选择您的邮件配置文件。

于 2013-09-09T08:53:45.610 回答