简体   繁体   中英

How to grant sendmail permission to sql server user?

I have a database user which is the owner of the database. The application requirement is to send mail using database mail of sql server .

Is there any way that I can add grant only send mail permission to that user?

I have a user named testuser having server roles public and is db_owner for 1 database. Please tell me the way that I don`t need to give sysadmin serverroles to that user.

Please try with the below 2 options.

USE msdb;
--add our user
CREATE USER  ClarkKent FOR LOGIN  ClarkKent; 
--give this user rights to use dbmail
exec sp_addrolemember 'DatabaseMailUserRole', 'ClarkKent'

now if we know ClarkKent is getting his authorization from a windows group, then you add that windows group as a user, and add that group to the same role;

USE msdb;
--add our user via a group we know he is in 
CREATE USER 'mydomain\BusinessGroup' FOR LOGIN   'mydomain\BusinessGroup'; 
--give this GROUP rights to use dbmail
exec sp_addrolemember 'DatabaseMailUserRole', 'mydomain\BusinessGroup'

You need to assign the login (or a role) to the mail profile after adding it to the database mail role. For the script below, a default of one may need to be set, instead of zero.

use msdb
exec sp_addrolemember 'DatabaseMailUserRole', 'sqlUser'

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp  
@principal_name = 'sqlUser',  
@profile_name = 'sqlMailProfileName',
@is_default = 0 

在此输入图像描述

you need add 'DatabasemailUserRole' for MSDB database to user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM