Send automated email - sql-server

I want to send automated emails to UserType( requester, assignee ,reviewer,admin) based on an insert and.or update in Database.
EX:
A remainder email send to requester, assignee ,reviewer and admin if the reviewer is not able to review the data in 7 days from the date it is assigned to him/her.
I know I can do this with SQL JOB or with Windows Services. But I ma looking for different way to do this if possible and/or does SSIS provide such functionality?

I think another option is to setup the trigger on insert/update of tables that need to be monitored then then called a stored procedure that use msdb.dbo.sp_send_dbmail to send out email.
You can look at the answer in this post for reference on how to send email using sp_send_dbmail.
Sending Email in SQL Server 2008 R2. I hope that helps.

Related

Net Core Identity - use current login in SQL Server query

I have .NET Core 2.1 web application which Identity authentication on and SQL Server 2017 database.
Every table in my database will contain a column (say: change_user) holding info about the user which inserted or last updated the record.
Suppose that user jsmith logins into the web app and clicks the button that deploys the stored proc which inserts a record into sometable table. So, the stored proc needs to set sometable.change_user columnn for that record.
Of course I could pass a param to the stored proc from C# level. I would prefer to handle that completely on server side and take the desired info from some session variable etc. Is such approach possible?
You cannot send the logged in user information to a stored proc without writing code, but you can make that code pretty simple.
Assume you have some service that is called by your controller. You define it as
Servicec(MyDbContext context, IHttpContextAccessor accessor)
Your dbcontext and the IHttpContext accessor are both registered in Startup as scoped services.
This in your service you can do
var userName = accessor.HttpContext.User.Identity.Name;
I think this is the easiest way to describe the process, but you can elaborate on it quite a bit. For example, you can inject the username into the DbContext and have it always add the username to entities that are being changed in an overloaded version of DbContext.SaveChangesAsync before you call base.ChangesAsync.

Send an email to current user in SQL Server Agent job when job fails

I have a SQL Job where I want to send an email alert if job fails. I know that for this functionalty I have to Configure Database Mail and than Adjust the properties within the SQL Server Agent. This article gives pretty good detail http://www.sherweb.com/blog/how-to-set-up-database-mail-for-sql-server-job-failures/.
What I want is, instead of setting up an email address to particular user like person1#test.com, I want to send an email to current user. This way who ever logged in to database and runs the job receives the email on job failure.
As I see, there are two benefits of this approach,
i. One particular user won't get flooded with emails. ii. Who ever is running the job can know that job has failed.
For example,
If I set person1#test.com and person2 logs in than on job failure person1 will receive an email. I don't want that. Is there any way that I can do this in SQL Server Agent Job settings. If no than how can I achieve this?
Like SELECT SYSTEM_USER or SELECT SUSER_NAME() gives me current user and use that as outgoing email address.
I think you are confused, if one user will start the job manually then you use a STORE PROCEDURE and there you can get the connected user information.
But pgAgent will run the process automatic and run using the USER you setup on the job.

Testing sp_send_dbmail

Does anyone know if there is a way to setup sql server to send all mail to one specific email address rather than what has been passed into sp_send_dbmail?
I ask as I need to verify what is going to be sent on a system that has customer email addresses and I don't want to actually send the email but rather check the contents.
The customer occasionally go onto this system to test certain things out which may trigger emails. At the moment these email are not sent out to anyone as the system is not setup for mail. I want this to stay the same but instead send all these mails to an internal email address. I don't know all the code that sends emails as there are approx. 3000 stored procedures in this system.
I have been searching on-line but can only find how to setup email.
If you're just looking to verify what will be sent, you can query the msdb.dbo.sysmail_mailitems table:
SELECT *
FROM msdb.dbo.sysmail_mailitems
This table holds the recipients, subject, body, format, query, sent_date, from_address, reply_to, and some other fields. I also suspect, though I haven't test, that if you updated that table to a new recipient email address that you could send them to the one address as you've requested above.
1)You can check mail is started or not using GUI.
2)you check email sending using following command.
Select * from msdb.dbo.sysmail_sentitems order by last_mod_date desc
enter image description here
unable to relay in Exchange Server :- Launch Exchange Server Manager and move to Administrative Groups. Select Administrative Group Name then choose Server>Server Name. Now click on Protocols and Select SMTP.
Right-click on Default SMTP Virtual Server and select Properties
In Access tab, click on Relay>only the list below
Now check the checkbox “Allow all systems to successfully authenticate to relay regardless of the list above” and close. ....check with internal team for permission.

I have used the database mailing system to send the email. But i could not figure our whether the email is sent or not

I have used the database mailing system to send the email. But i could not figure our whether the email is sent or not.
Is there any system table in sql server 2008 to insert the log whether the mail is sent or not. I want the sent 'email date from' , 'email date to' and Email Sent to. And is there any field that stores the mail sent like 'yes' or 'no'
Take a look at this article here. There are several tables in the msdb (system database) that are useful for tracking outgoing email status. From the article:
The Database Mail external program logs activity and displays the log
through the Windows Application Event Log and the sysmail_event_log
view in the msdb database. To check the status of an e-mail message,
run a query against this view. E-mail messages have one of four
possible statuses: sent, unsent, retrying, and failed.
Further, you can track additional info about the failures if you set the logging level to verbose when you configured your server for database mail.

SQL Server login per application user

I am looking for different ways to uniquely identify an application user in SQL Server 2008.
To give some back ground to the issue:
The ASP.NET 4 web application I work with uses an SQL Server 2008 database, there is one SQL Server login and DB User for all connections from the application to database.
We are developing a reporting solution using Crystal Reports where users can write and execute their own reports through the application. The crystal reports can only return data from Views. Each application user will have their own unique login to the server.
The View then uses SELECT SYSTEM_USER() to find the logged in user and restrict data returned from the view.
Is it bad practice to create a distinct login and DB user for each application user? This would mean > 1000 users per DB, across up to 80 databases (Creation of
each log in would be handled through T-SQL, not manually)
What other ways can the user be identified in the View in the DB?
We looked at altering the connection string to include the user id in the Workstation ID, we can use SELECT HOST_NAME() to get the ID, but this seems like abuse of the Workstation ID.
Any other suggestions about how the user can be uniquely identified would be appreciated.
Many thanks
Have you considered using Windows Authentication as apposed to SQL Authentication? That would at least reduce the number of logins & users that would need to be managed.
Y can use external database,file or other resource to store user access to any data when u use msql 2008.
http://msdn.microsoft.com/en-us/library/dd392015%28v=sql.100%29.aspx
That give y change to not mix data with audit elements for performance and security reasons.
Use profiler on your sql server and in profiler is information about ( aplication , user , and host ). So that kind of information y can easy get by audit or from custom trigger on certain database objects.
Your aplication can also have own user table and share the same connection string too all users or give users with windows auth type only public privilege. And use in stored procedures run as command ( run as another user in db).
So y can log any user and give them minimal rights.
Its my firs post here and I actually learning English lol.
There were two solutions that we came up with:
Before the Crystal report is executed, in .Net we can extract the SQL the report (and sub report) will generate, execute the SQL and put the results in a dataset and generate the report from the dataset. The advantage of this is that because we have the SQL, before the SQL executes, we can supply user information to the SQL server session that can be used to identify the user when the SQL is executing.
Have a limited number of SQL Server user logins which can be dynamically assigned to application users. We will have a table which will map the SQL Server Login to an application user just before they run a Crystal report. When they query has finished executing the Login is released. Obviously this means that at busy times we may run out of Logins, but more can be added depending on usage.

Resources