Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何通过SQL脚本设置DBMail以使用gmail帐户_Sql Server_Sp Send Dbmail - Fatal编程技术网

Sql server 如何通过SQL脚本设置DBMail以使用gmail帐户

Sql server 如何通过SQL脚本设置DBMail以使用gmail帐户,sql-server,sp-send-dbmail,Sql Server,Sp Send Dbmail,我想通过我的gmail帐户发送电子邮件 我希望完全从SQL脚本执行此设置。最好的方法是什么 我正在使用SQL Server 2014。以下是我为自己找到的答案 USE master GO sp_configure 'show advanced options',1 GO RECONFIGURE WITH OVERRIDE GO sp_configure 'Database Mail XPs',1 GO RECONFIGURE WITH OVERRIDE GO use [msdb]; /* be

我想通过我的gmail帐户发送电子邮件

我希望完全从SQL脚本执行此设置。最好的方法是什么


我正在使用SQL Server 2014。

以下是我为自己找到的答案

USE master
GO
sp_configure 'show advanced options',1
GO
RECONFIGURE WITH OVERRIDE
GO
sp_configure 'Database Mail XPs',1
GO
RECONFIGURE WITH OVERRIDE
GO
use [msdb];

/*
before you run this, replace all occurrences of
   DOMAIN\USERNAME
with your actual domain and 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 the profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
  @profile_name = @profilename
 ,@description = 'My test mail profile';

--create the account
EXECUTE msdb.dbo.sysmail_add_account_sp
  @account_name    = 'Gmail'
 ,@description     = 'My gmail account, for 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 the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
  @profile_name = @profilename
 ,@account_name = @accountname
 ,@sequence_number = 1;

--give yourself permission to use what you created
EXEC sp_addrolemember N'DatabaseMailUserRole', N'DOMAIN\USERNAME'
ALTER AUTHORIZATION ON SCHEMA::[DOMAIN\USERNAME] TO [DatabaseMailUserRole]

--send a test message
declare @msg nvarchar(max) = 'Receipt of this message confirms that SQL DBMail is working correctly on server ' + @@servername + ' as of ' + convert(nvarchar(max),getdate()) + '.';
exec msdb.dbo.sp_send_dbmail @profilename, @mygmailaccount, null, null, @msg, 'TEXT', 'Normal';