gpt4 book ai didi

sql-server - 检查 sp_send_dbmail 是否成功

转载 作者:行者123 更新时间:2023-12-01 20:18:44 25 4
gpt4 key购买 nike

我正在寻找一种方法来检查使用 sp_send_dbmail 排队的特定电子邮件是否最终从我们的 Exchange 服务器成功发送。我查看了系统表 msdb.dbo.sysmail_mailitemsmsdb.dbo.sysmail_logmsdb.dbo.sysmail_log 似乎是两者中更有帮助的一个;具体来说,是它的 description 列。从我迄今为止进行的测试来看,每当发生错误时,description 列中都会出现以下格式的消息:

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2012-11-01T11:28:04). Exception Message: Cannot send mails to mail server. (Mailbox unavailable. The server response was: 5.7.1 Unable to relay for thisemail@email.com). )

这与共享相同 process_id 的其他行一起包含。所附记录的描述为

DatabaseMail process is started

DatabaseMail process is shutting down

如果电子邮件已成功发送,则表中会记录相同的 2 行,但它们之间没有包含行。

因此,如果我成功发送,表中将显示以下内容

enter image description here

如果我发送失败,日志会记录这一点

enter image description here

是否有其他实例说明如何在发送失败或发送成功时记录条目?例如,是否有可能有 4 行发送条目(其中 2 行说明其启动时间和关闭时间,另外 2 行说明电子邮件已成功发送)。我没有发现任何日志记录与上面列出的模式不同,但想在基于此假设编写逻辑之前确定一下。

最佳答案

sysmail_faileditems 只会为您提供失败电子邮件的列表。如果您需要查看成功电子邮件的列表,则需要使用sysmail_mailitems

使用以下查询获取同一日期发送的所有电子邮件的详细信息:

SELECT * FROM msdb..sysmail_mailitems WHERE sent_date > DATEADD(DAY, -1,GETDATE())

以下是获取过去 24 小时内所有失败电子邮件的完整查询:

SELECT items.subject ,
items.recipients ,
items.copy_recipients ,
items.blind_copy_recipients ,
items.last_mod_date ,
l.description
FROM msdb.dbo.sysmail_faileditems AS items
LEFT OUTER JOIN msdb.dbo.sysmail_event_log AS l
ON items.mailitem_id = l.mailitem_id
WHERE items.last_mod_date > DATEADD(DAY, -1,GETDATE())

关于sql-server - 检查 sp_send_dbmail 是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com