只有部分电子邮件从 SQL Server 2005 上的 sp_send_dbmail(数据库邮件)成功发送
我想找到当我在 SQL Server 2005 上使用 sp_send_dbmail
时发生的情况的答案。我一直在谷歌搜索,但没有成功,而且我现在不知道该转向哪里。
场景是这样的:
我在我们的SQL服务器上设置了邮件功能。我的电子邮件地址位于一个表中,内容位于另一表中。对于我在表中找到的每个地址,我使用 sp_send_dbmail
存储过程将新消息编译到该地址。
当我稍后执行我自己的 SP 来执行上述操作(获取电子邮件并编译一条新消息给收件人)时,DatabaseMail90.exe 在服务器上启动,并且正在发送电子邮件。但这就是棘手的地方,因为一些邮件是直接分发的,可能是因为它们位于同一域和网络上。复杂的是,大多数(不是全部)外部邮件在 msdb.dbo.sysmail_mailitems 表中都会出现错误。但混乱还不止于此。尽管 msdb.dbo.sysmail_mailitems 表中报告了错误,但某些电子邮件仍在传送。
这是我用来查看发送消息状态的查询:
SELECT
mailitem_id,
recipients,
subject,
send_request_date,
sent_status, --0 new, not sent, 1 sent, 2 failure or 3 retry.
sent_date
FROM
msdb.dbo.sysmail_mailitems
WHERE
sent_status != 1
我已经配置了数据库邮件,系统参数如下:
- 帐户重试尝试:3
- 帐户重试延迟(秒):30
- 最大文件大小(字节):50000000(永远不会这么大,但只是为了安全起见)
- 禁止的附加文件扩展名:exe、dll、vbs、js
- 数据库邮件可执行文件最短生命周期(秒):300
- 日志记录级别:扩展
我的数据库邮件配置文件,是公共的并设置为默认配置文件。我还设置了一个现有的配置文件。
这是执行所有操作并开始发送邮件的 SP:
DECLARE @fldEmail AS VARCHAR(max)
DECLARE @fldSubject AS VARCHAR(max)
DECLARE @fldMessage AS VARCHAR(max)
DECLARE port_cursor CURSOR FOR
SELECT d.[E-Mail],s.Subject,s.HTMLMessage
FROM tbl_NAV_Sendout s,tbl_NAV_DistributionList d
OPEN port_cursor
FETCH NEXT FROM port_cursor
INTO @fldEmail, @fldSubject, @fldMessage
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC msdb.dbo.sp_send_dbmail @recipients = @fldEmail, @subject = @fldSubject, @body = @fldMessage, @body_format = 'HTML', @file_attachments = 'C:\NAVDW\ECNAV.csv', @profile_name = 'DBMail'
FETCH NEXT FROM port_cursor
INTO @fldEmail, @fldSubject, @fldMessage
END
CLOSE port_cursor
DEALLOCATE port_cursor
所以问题仍然存在。有没有办法让我调整设置以更快地发送电子邮件?另外,真正的问题是,为什么有时发送电子邮件时会出现错误?发送到的电子邮件地址没有任何问题。我应该在哪里查找代码或数据库邮件配置中可能存在的错误?你经历过这样的事情吗?
msdb.dbo.sysmail_mailitems 表中的错误描述是什么?我的意思是,向外部域报告了错误,但邮件已送达。或者如果邮件未送达,我应该在哪里查找导致问题的原因。
I want to find an answer to what is happening when I use sp_send_dbmail
on SQL Server 2005. I have been googeling but without success and I don't know really where to turn now.
This is the scenario:
I have set up a mail function on our SQL server. I have the e-mail addresses in one table and the content in another table. For each address I find in my table, I use the sp_send_dbmail
Stored Procedure to compile a new message to the address.
When I later execute my own SP that does the above (takes the email and compiles a new message to the recipient), DatabaseMail90.exe starts on the server and the e-mails are being sent out. But this is where it gets tricky, because some of the mail are being distributed directly, probably because it is on the same domain and network. The complicated thing is that most - not all - external mail gets an error in the msdb.dbo.sysmail_mailitems table. But the confusion does not stop here. Some of the e-mails are being delivered even though it has a reported error in the msdb.dbo.sysmail_mailitems table.
This is the query I use to view sent messages status:
SELECT
mailitem_id,
recipients,
subject,
send_request_date,
sent_status, --0 new, not sent, 1 sent, 2 failure or 3 retry.
sent_date
FROM
msdb.dbo.sysmail_mailitems
WHERE
sent_status != 1
I have configured the Database Mail, System Parameters like this:
- Account Retry Attempts: 3
- Account Retry Delay (seconds): 30
- Maximum File Size (Bytes): 50000000 (NEVER going to be this big, but just to be on the safe side)
- Prohibited Attached File Extensions: exe, dll, vbs, js
- Database Mail Executable Minimum Lifetime (seconds): 300
- Logging Level: Extended
My Database Mail Profile, is Public and set as Default Profile. I also have set up an existing Profile.
This is the SP that executes everything and start sending the mail:
DECLARE @fldEmail AS VARCHAR(max)
DECLARE @fldSubject AS VARCHAR(max)
DECLARE @fldMessage AS VARCHAR(max)
DECLARE port_cursor CURSOR FOR
SELECT d.[E-Mail],s.Subject,s.HTMLMessage
FROM tbl_NAV_Sendout s,tbl_NAV_DistributionList d
OPEN port_cursor
FETCH NEXT FROM port_cursor
INTO @fldEmail, @fldSubject, @fldMessage
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC msdb.dbo.sp_send_dbmail @recipients = @fldEmail, @subject = @fldSubject, @body = @fldMessage, @body_format = 'HTML', @file_attachments = 'C:\NAVDW\ECNAV.csv', @profile_name = 'DBMail'
FETCH NEXT FROM port_cursor
INTO @fldEmail, @fldSubject, @fldMessage
END
CLOSE port_cursor
DEALLOCATE port_cursor
So the question remains. Is there a way for me to tweak the setup to deliver the e-mails faster? And also, the real question, why is there an error when sending the e-mail sometimes? There is nothing wrong with the e-mail address it is sending to. Where should I look to find possible errors in my code or my Database Mail configuration? Have you experienced something like this?
What is the description of an error in the msdb.dbo.sysmail_mailitems table? I mean, there is an error reported to an external domain, but the mail is delivered. Or if the mail is not delivered, where should I look on what is causing the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试在您的 sql 查询中添加
WAITFOR DELAY
函数吗?电子邮件。在我的例子中它工作得很好。
Can you try adding
WAITFOR DELAY
function in your sql query as per below:Although this will slow the execution but adding this function will give the SQL Server sufficient time to efficiently populate the data using cursor and to deliver the email. It worked perfectly fine in my case.