编辑
我之前关于这个主题的帖子涉及程序中的特定交易,这些交易不再使用,现在错误似乎只发生在非常特定的情况下,当我对我们公司的电子邮件地址使用无效的用户名时badname@ourcompany.co.uk
代理每十分钟运行一次存储过程以发送电子邮件。如果它找到一条新记录,那么它会发送一封邮件。
注意到一些奇怪的行为 - 如果TO
字段正确但BCC
电子邮件地址无效但使用我们有效的公司域名(例如rubbishPart@ourcompany.co.uk
),则存储过程会向TO
. 域名需要是我们公司的域名才能获得这种行为......如果我将域更改为gmail.com
然后只有一封邮件出去。
- 为什么会这样?
- 我该如何改变这种行为?
- 这个程序中的 是
Catch
多余的吗?
这是程序:
BEGIN TRY
DECLARE @Exit TINYINT = 0
WHILE @Exit = 0
BEGIN
DECLARE @MailIdFound INT =
(
SELECT CASE
WHEN MIN(EmailId) IS NULL THEN 0
ELSE MIN(EmailId)
END
FROM WH.dbo.EmailQueue
WHERE DateEmailKey IS NULL
)
IF @MailIdFound = 0
BEGIN SET @Exit = 1 END --exit here as
ELSE
BEGIN --send the mail here
DECLARE @DateEmailKey INT
DECLARE @EmailBCC NVARCHAR(1000)
DECLARE @EmailTO NVARCHAR(1000)
DECLARE @EmailCC NVARCHAR(1000)
DECLARE @EmailBody NVARCHAR(MAX)
DECLARE @EmailImportance VARCHAR(6)
DECLARE @EmailSubject NVARCHAR(1000);
WITH myMostUrgentMail_cte
AS
(
SELECT TOP 1
DateEmailKey,
EmailBCC,
EmailTO,
EmailCC,
EmailBody,
EmailImportance,
EmailSubject
FROM WH.dbo.EmailQueue
WHERE EmailId = @MailIdFound
)
SELECT @DateEmailKey = DateEmailKey,
@EmailTO = EmailTO,
@EmailCC = EmailCC,
@EmailBCC = EmailBCC,
@EmailBody = EmailBody,
@EmailSubject = EmailSubject
FROM myMostUrgentMail_cte;
SET @EmailBody = @EmailBody + '<html> <span style=''font-size:11.0pt;font-family:"Calibri","sans-serif"; color:black''><p><b>blah blah</b></p></span></html>'
EXEC msdb..sp_send_dbmail
@recipients = @EmailTO,
@copy_recipients = @EmailCC,
@blind_copy_recipients = @EmailBCC,
@subject = @EmailSubject,
@Importance = @EmailImportance,
@body_format = 'html',
@body = @EmailBody
UPDATE x
SET x.DateEmailKey = (CONVERT(CHAR(8),GETDATE(),(112))),
x.DateEmailTime = (CONVERT([time](7),left(CONVERT([char](12),GETDATE(),(114)),(8)),(0)))
FROM WH.dbo.EmailQueue x
WHERE x.EmailId = @MailIdFound
END
END
END TRY
BEGIN CATCH
-- handle error here
EXEC msdb..sp_send_dbmail
@recipients = 'ME@ME.com;'
, @subject = 'ERROR OCCURED DURING EMAIL CREATION'
, @body_format = 'html'
, @body = 'ERROR OCCURED'
UPDATE x
SET x.ErrorOccured = 1
FROM WH.dbo.EmailQueue x
WHERE x.EmailId = @MailIdFound
END CATCH;