如何设置对 MSSQL 2005 数据库邮件的访问?

发布于 2024-07-11 05:07:32 字数 766 浏览 7 评论 0原文

我刚刚在 MSSQL 2005 中设置了数据库邮件。我已经能够从服务器上的管理员帐户并通过 SQL Server 代理作业发送电子邮件。 但是,当我尝试通过只能访问我们的 Web 应用程序数据库的登录名发送电子邮件时,我无法发送电子邮件。

设置数据库邮件访问的最佳方法是什么?

我有 2 个高级用户想要发送电子邮件。

  1. 通过 SQL Server 代理安排作业来运行执行某些处理然后发送电子邮件的存储过程。 (这不是问题)
  2. 从 Web 应用程序调用一个存储过程,该应用程序调用另一个可以发送电子邮件的存储过程。 (这是我的权限问题)

可能的解决方案

  1. 我发现向登录授予权限的唯一方法是将登录映射到msdb 数据库并授予publicDatabaseMailUserRole。 这确实让我担心,因为我认为这可以提供对 msdb 数据库的大量访问权限。

  2. 将我想要发送的电子邮件存储在一个表中,然后让 SQL Server 代理作业经常查看该表以发送任何排队的电子邮件。 这样,Web 应用程序的数据库登录就不会执行 [msdb].[dbo].[sp_send _dbmail] 调用,因此不需要 msdb 数据库的任何权限。

还有其他解决方案吗?

I've just setup Database mail within MSSQL 2005. I have been able to send email from an administrator account on the server and through a SQL Server Agent job. But, I unable to send email when I tried to send email via a login that just has access to a database for our web application.

What is the best way to set up access to Database Mail?

I have 2 senerios where I want to send email.

  1. Schedule a job through SQL Server Agent to run a Stored Procedure that does some process and then sends an email. (this hasn't been a problem)
  2. Have a stored procedure invoked from a web application that calls another stored procedure which could send an email. (this is were my permissions issue is)

Possible Solutions

  1. The only way that I have found to grant permission to a login is to map the login to the msdb database and grant the public and DatabaseMailUserRole. This really concerns me as I would assume that this gives to much access to the msdb database.

  2. Store the email I want to send in a table and then have a SQL Server Agent job look at that table every so often to send any queued emails. This way the database login for the web application does not execute the [msdb].[dbo].[sp_ send _dbmail] call and thus does not need any permission to the msdb database.

Are there any other solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-07-18 05:07:32

您应该能够创建一个存储过程WITH EXECUTE AS OWNER选项,以便它在所有者而不是调用者的上下文中运行。

CREATE PROCEDURE [dbo].[sp_SendMail]
(
    @To nvarchar(1000),
    @Subject nvarchar(100),
    @Body nvarchar(max)
)
WITH EXECUTE AS OWNER
AS
BEGIN
    exec msdb.dbo.sp_send_dbmail @profile_name = 'MailProfile', 
                @recipients = @To, 
                @subject = @Subject, 
                @body = @Body
END

You should be able to create a stored procedure WITH EXECUTE AS OWNER option so it runs in the context of the owner rather than the caller.

CREATE PROCEDURE [dbo].[sp_SendMail]
(
    @To nvarchar(1000),
    @Subject nvarchar(100),
    @Body nvarchar(max)
)
WITH EXECUTE AS OWNER
AS
BEGIN
    exec msdb.dbo.sp_send_dbmail @profile_name = 'MailProfile', 
                @recipients = @To, 
                @subject = @Subject, 
                @body = @Body
END
找个人就嫁了吧 2024-07-18 05:07:32

我正在尝试做同样的事情,但执行

GRANT EXECUTE ON dbo.sp_SendMail TO User1

并不能解决这个问题。 使此过程正常工作的唯一事情是不添加

EXECUTE AS OWNER

(我仅使用“WITH ENCRYPTION”创建它)并将运行此过程的用户添加到 msdb DatabaseMailUserRole 角色:

USE msdb
GO
EXEC sp_adduser @loginame='USERNAME', @grpname='DatabaseMailUserRole'
GO

谢谢

I'm trying to do the same but executing

GRANT EXECUTE ON dbo.sp_SendMail TO User1

is not fixing this issue. Only thing which is making this procedure to work is not adding

EXECUTE AS OWNER

(I'm creating it with "WITH ENCRYPTION" only) and adding user which will be running this procedure to msdb DatabaseMailUserRole role:

USE msdb
GO
EXEC sp_adduser @loginame='USERNAME', @grpname='DatabaseMailUserRole'
GO

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文