Tuesday, June 9, 2015

Dynamics GP - How to Setup Business Alerts



For More advanced Email Alerts, you can use SQL to send mail directly

Original Solution
http://stackoverflow.com/questions/13300881/how-to-send-email-from-sql-server


  • Create SQL Mail Profile and Account
  • Run this code

-------------------------------------------------------------------------
sp_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
sp_CONFIGURE 'Database Mail XPs', 1
GO
RECONFIGURE
GO
-------------------------------------------------------------------------

  • Use this code to send a single e-mail
--------------------------------------------------------------------------

USE msdb
GO
EXEC sp_send_dbmail @profile_name='TEST',
@recipients='gregory@davyntt.com',
@subject='Test message',
@body='This is the body of the test message.',
@Query = 'select * from iv00101',
@attach_query_result_as_file = 1

----------------------------------------------------------------------------

  • Use this code to loop through a table and send multiple emails

---------------------------------------------------------------------------------
DECLARE @email_id NVARCHAR(450),@id BIGINT,@max_id BIGINT,@query NVARCHAR(1000)

SELECT @id=MIN(id),@max_id=MAX(id) FROM [email_adresses]

WHILE @id<@max_id
BEGIN
    SELECT @email_id=email_id
    FROM [email_adresses]

    set @query='sp_send_dbmail @profile_name=''yourprofilename'',
                        @recipients='''+@email_id+''',
                        @subject=''Test message'',
                        @body=''This is the body of the test message."
'

    EXEC @query
    SELECT @id=MIN(id)FROM [email_adresses] where id>@id

END
-------------------------------------------------------------------------------------

  • Use these queries to determine if your mail is sending properly
-------------------------------------------------------------------------------------
SELECT * FROM msdb.dbo.sysmail_mailitems
SELECT * FROM msdb.dbo.sysmail_sentitems
SELECT * FROM msdb.dbo.sysmail_log

No comments:

Post a Comment