4

How to send email from SQL Server Jobs in SQL Server 2014. I want to implement email notification through stored procedure instead of Windows services.

1
  • What kind of e-mail notification You are thinking about? If it's something other than db maintenance or alerts, then database mail is ok. Otherwise - stick with something else.
    – Marcin S.
    Commented Jul 4, 2016 at 12:53

3 Answers 3

6

You need to first configure DB mail in SQL Server in order to utilise this functionality. You can use the below link as a reference for DB mail configuration purposes.

https://www.brentozar.com/blitz/database-mail-configuration/

Once DB mail is set up, you can enable database mail on SQL Server Agent. The same link describes how to do that. Once it is enabled on SQL Server Agent, you need to restart the SQL Server Agent service to make that take effect.

That's it. Now you can use the msdb.dbo.sp_send_dbmail SP to send email using a SQL Server Agent job or a stored procedure.

2

First Setup a Database Mail account, Database Mail profile and use the below code in your SP.

EXEC msdb.dbo.sp_send_dbmail 
@profile_name='Mymail', 
@recipients='[email protected]',
@subject = 'Mail subject' ,
@body = 'Mail body' ,
@file_attachments  = 'G:\BACKUP\data\log.txt' --Path to attachment if any

You can always refer to MSDN for more options

https://msdn.microsoft.com/en-us/library/ms190307.aspx

1

You can send the email(s) by using the SQL Server's extended stored procedure sp_send_dbmail.
Look here for the documentation.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.