sql server - How can I set up DBMail to use a gmail account through a SQL Script -
i want send email through gmail account using sql server's dbmail.
i'd setup purely sql script. what's best way that?
i'm using sql server 2014.
here answer found myself.
use master go sp_configure 'show advanced options',1 go reconfigure override go sp_configure 'database mail xps',1 go reconfigure override go use [msdb]; /* before run this, replace occurrences of domain\username actual domain , user name --*/ declare @profilename nvarchar(max) = 'my profile'; declare @accountname nvarchar(max) = 'my gmail account'; declare @mygmailaccount nvarchar(100) = 'someone@gmail.com'; declare @mygmailpassword nvarchar(100) = 'yourpassword'; --plain text --create profile execute msdb.dbo.sysmail_add_profile_sp @profile_name = @profilename ,@description = 'my test mail profile'; --create account execute msdb.dbo.sysmail_add_account_sp @account_name = 'gmail' ,@description = 'my gmail account, testing' ,@email_address = @mygmailaccount ,@replyto_address = @mygmailaccount ,@display_name = 'test email' ,@mailserver_name = 'smtp.gmail.com' ,@mailserver_type = 'smtp' --case sensitive! ,@port = 587 ,@username = @mygmailaccount ,@password = @mygmailpassword ,@enable_ssl = 1; --tie account profile execute msdb.dbo.sysmail_add_profileaccount_sp @profile_name = @profilename ,@account_name = @accountname ,@sequence_number = 1; --give permission use created exec sp_addrolemember n'databasemailuserrole', n'domain\username' alter authorization on schema::[domain\username] [databasemailuserrole] --send test message declare @msg nvarchar(max) = 'receipt of message confirms sql dbmail working correctly on server ' + @@servername + ' of ' + convert(nvarchar(max),getdate()) + '.'; exec msdb.dbo.sp_send_dbmail @profilename, @mygmailaccount, null, null, @msg, 'text', 'normal';
Comments
Post a Comment