Sending an email via SQL Server causes error - sql

I'm trying to send an e-mail via SQL Server Management Studio, but it returns an error.
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Adventure Works Administrator',
#recipients = 'example#example.com',
#body = 'The stored procedure finished successfully.',
#subject = 'Automated Success Message'
Error is:
The EXECUTE permission was denied on the object 'sp_send_dbmail', database 'msdb', schema 'dbo'.
I've surfed the internet to fix this problem, but none worked out. Need your help. Thanks

Maybe your user do not have correct rights on the SQL instance you are targeting, as suggested in the comments :
grant execute on dbo.sp_send_dbmail to <usernamehere>;
See the Troubleshooting article on this issue :
EXEC msdb.dbo.sp_addrolemember #rolename = 'DatabaseMailUserRole'
,#membername = '<user or role name>';
GO
Also, you may have not configured this feature, see documentation :
Before use, Database Mail must be enabled using the Database Mail
Configuration Wizard, or sp_configure.
And this one for configuration :
USE master
Go
EXEC sp_configure 'show advanced options', 1
Go
RECONFIGURE
Go
EXEC sp_configure 'Database Mail XPs', 1
Go
RECONFIGURE
Go
EXEC sp_configure 'show advanced options', 0
Go
RECONFIGURE
Go

Related

sp_send_dbmail blocks and causes PREEMPTIVE_OS_GETPROCADDRESS

I have a strange issue with msdb.dbo.sp_send_dbmailwithin a stored procedure (code simplified):
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- ...
SET #msg = N'...';
SET #filename = N'...';
EXEC msdb.dbo.sp_send_dbmail
#recipients = 'mailaddr#example.com'
, #blind_copy_recipients = 'another_mailaddr#example.com'
, #from_address = 'senders_mail#example.com'
, #subject = N'...'
, #body = #msg
, #query = N'SET NOCOUNT ON; SELECT <something> FROM <a_view>;'
, #execute_query_database = N'<same database the proc resides in>'
, #query_result_width = 8000
, #attach_query_result_as_file = 1
, #query_attachment_filename = #filename
, #query_result_header = 1
, #query_result_separator = ';'
, #query_result_no_padding = 1
, #exclude_query_output = 1;
I have a database user which is member of db_datareaderand EXECUTEpermissions for the stored procedure. Furthermore the assigned server login is mapped to msdband a member of the DatabaseMailUserRole. There is only 1 single mail profile which is public and flagged as default.
Everything works fine from SSMS: connect with the user's credentials and execute the stored procedure. Great!
The first oddity: if I'm logged in as sysadmin and try to EXECUTE AS it doesn't work. Okay, I found something that points out that there are issues with this.
But the main issue is this: I call the procedure from a 3rd party Java application which connects using the jTDS driver (don't know if this is important). The applicaton executes the procedure ... and nothing else happened (no log entries, the task freezes).
In the activity monitor I see the following:
Process <...>
Database master (??? I've never connected to this
db!)
Task State Running
Wait Type PREEMPTIVE_OS_GETPROCADDRESS
Head Blocker 1
To make things worse I cannot kill this process. If I try this the Command column in the activity monitor shows only KILLED/ROLLBACK.
KILL <PROCESS-ID>shows
spid <...>: Transaction rollback in progress. Estimated rollback completion: 0% Estimated time left: 0 seconds.
I have to restart the whole instance to get rid of the process.
What is happening here?
Finally I found the answer here: blocking from xp_sysmail_format_query waittype of preemptive_os_getprocaddress
It seems that the Java application opens a transaction explicitly (there are 2 calls to stored procedures consecutively). After setting the Auto-Commit option of the database adapter to on everything work's fine.

Execute stored procedure, but access denied

I tried to execute a stored procedure. But I this error below, at every stance of execution:
Msg 18456, Level 14, State 1, Line 3 Login failed for user 'NT
AUTHORITY\ANONYMOUS LOGON'.
Thank you.
This is what I try to execute:
USE [Artikelen]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[BerekenSKU]
#loc = N'AM',
#lev = N'3436',
#reset = true
SELECT 'Return Value' = #return_value
GO
I am using Microsoft SQL 2014
Error shows that there is credential issue. Please check this link.
This message indicates that you are attempting to access the linked
server by using Windows NT authentication to impersonate the client
connecting. Refer Link
Look at the stored procedure in question. The login is failing at the
linked server because whoever is calling it doesn't have mapped
credentials set up.
Refer this links:
sqlcmd: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'
http://www.sqlservercentral.com/Forums/Topic548200-146-1.aspx
http://www.sqlservercentral.com/Forums/Topic1523177-1550-1.aspx
http://www.sqlservercentral.com/Forums/Topic246883-110-1.aspx
http://www.sqlservercentral.com/Forums/Topic246883-110-1.aspx

SQL server mail function sp_send_cdontsmail

i wrote a procedure in which i tried to send mail using below command.
EXEC Sp_send_cdontsmail 'from','to','Test','TEST DATA'
After executing its showing "Command(s) completed successfully."
but i am not getting any mail.please help me on this.
You need to configure Database Mail and then use sp_send_dbmail to send mail. This is a supported procedure, part of the SQL Server.
PS. I am aware that out there some code sample circulates that advocates something along the lines of:
CREATE PROCEDURE [dbo].[sp_send_cdontsmail]
...
EXEC #hr = master..sp_OACreate 'CDONTS.NewMail', #MailID OUT
EXEC #hr = master..sp_OASetProperty #MailID, 'From',#From
EXEC #hr = master..sp_OASetProperty #MailID, 'Body', #Body
...
This is horrible code. As one can easily see there is absolutely 0 (zero, nada, zip) error checking in this code. Any failure will be reported as 'success'. COM interop will not raise T-SQL notification messages, is all in the HRESULT, which goes unchecked. Steer away from such code.

dbmail is not working in sql localdb in visual studio

I'm trying to send email with activation link whenever a new user register on my website. but I'm getting activation failure after sending the email from localdb (SQL Server Express)
This is what I've tried
--Enabling Database Mail
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'Database Mail XPs',1
reconfigure
--Creating a Profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
#profile_name = 'Send_Mail',
#description = 'Sending Mail On Register and on some other activity.' ;
-- Create a Mail account for gmail. We have to use our company mail account.
EXECUTE msdb.dbo.sysmail_add_account_sp
#account_name = 'Send_Email_Register',
#email_address = 'abc#gmail.com',
#mailserver_name = 'smtp.gmail.com',
#port=587,
#enable_ssl=1,
#username='abc#gmail.com',
#password='Emailid password'
-- Adding the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
#profile_name = 'Send_Mail',
#account_name = 'Send_Email_Register',
#sequence_number =1 ;
-- Granting access to the profile to the DatabaseMailUserRole of MSDB
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
#profile_name = 'Send_Mail',
#principal_id = 0,
#is_default = 1 ;
--Sending Test Mail
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Send_Mail',
#recipients = 'recipient#gmail.com',
#body = 'Database Mail Testing...',
#subject = 'Databas Mail from SQL Server';
--Verifying, check status column
select * from sysmail_allitems
It looks like DBMail isn't supported on any of the Express instances. The feature breakdown can be seen at http://msdn.microsoft.com/en-us/library/cc645993.aspx#Add_DBServices, and a nice writeup of limitations for the SQL Server 2012 Express edition is in an answer to this thread.

Enough permitions to login a created user

Every time I'm launching SQL server I run a script that destroys my little local database, refills it with example data, destroys some logins that were artificially created using this script.
exec sp_addlogin #loginame = 'strong', #passwd ='strong', #defdb =KornDurnDB_new;
exec sp_adduser #loginame = 'strong', #name_in_db = 'strng';
To this login I give ALL grants(according to ANSI-92 standart), but It is not enough to authorize using this login, when sql management studio launches. Sql management studio returns me error 18456.
What can a add to make a login with that account and password?
UPDATED(SOLUTION): I have just reinstalled MS SQL server with other installaton properties and its working
You might be creating a user in the master database instead of the KornDurnDB_new database. Try:
exec sp_addlogin #loginame = 'strong', #passwd = 'strong', #defdb = KornDurnDB_new;
use KornDurnDB_new;
exec sp_adduser #loginame = 'strong', #name_in_db = 'strng';
Note that both sp_addlogin and sp_adduser are obsolete. Their replacements are create login and create user.