预定作业不发送电子邮件

时间:2013-09-04 14:54:22

标签: sql-server sql-server-2008 email

我相信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”配置文件,我可以发送电子邮件给自己。 那么为什么会发生这种情况,我应该怎么做才能解决它?

1 个答案:

答案 0 :(得分:2)

您是否查看了SQL Server代理属性/警报系统? 应启用邮件配置文件,并正确选择您的邮件配置文件。