dbmail is not working in sql localdb in visual studio - sql

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.

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.

Compilation errors to send email with TRIGGER sql

Hello i'm working on PL/SQL Developper and i've got an annoying error ... I'm trying to make a trigger to send an email when something is added in the "JDEF" database.
This is my code :
CREATE OR REPLACE TRIGGER Send_Email
AFTER INSERT
ON JDEF
BEGIN
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'me',
#recipients = 'example#ent.com',
#body = 'The stored procedure finished successfully.',
#subject = 'Automated Success Message'
END
The error compilation are for those two lines : EXEC msdb.dbo.sp_send_dbmail AND #profile_name = 'me'. They speak about "MSDB" and ",' symbols who are replaced by other one to compile.
Have you any ideas to help me ?
thx
Your code looks like mssql in oracle standard way to send email is for example:
UTL_MAIL.send(sender => 'me#ent.com',
recipients => 'example#ent.com',
subject => 'Automated Success Message',
message => 'The stored procedure finished successfully.',
mime_type => 'text; charset=us-ascii');
But it needs being enabled before you can use UTL_MAIL. Another package to send mail is UTL_TCP
This may be helpful: http://www.orafaq.com/wiki/Send_mail_from_PL/SQL

Sending an email via SQL Server causes error

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

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.

sql db mail problem while sending in bulk

I am using db mail(sql server 2005) to send bulk email(>2000).
The code tat i use is,
exec msdb..sp_send_dbmail
#profile_name = 'My Profile',
#recipients = 'raghav.cinch#gmail.com',
#subject = 'test',
#body = 'test',
#body_format = 'HTML'
If i send few emails(less than 100), all emails are sent successfully. But only bulk emails give me error.
The error I get is,
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 8 (2011-09-27T21:29:17). Exception Message: Cannot send mails to mail server. (Unable to send to all recipients.).
)
The error comes after 100 or 105 mails. The email addresses are correct and if i sent in cycles of 100, all mails are sent successfully.
I believe it should be some configuration settings tat need to be tweaked. Could someone pls help me in fixing it..
Thanks in advance.
Changed the iis6 settings, max queue length for the smtp server and this worked like charm.