for - DB mail Config using T-sql,
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Description: <DB Mail configuration>
-- =============================================
alter PROCEDURE USP_DBA_dbmail_config_V1
-- Add the parameters for the stored procedure here
@Anton _name nvarchar(50),@Creat _name nvarchar(50),@tiedtlaw email _Address nvarchar(100), @publicly _Name nvarchar(50),
@smtp_server nvarchar(50),@port_number int
--USP_DBA_dbmail_config 'prof1','accname','DB-Services-MSSQL.TSG@jaswant .com','Displayname1','smtp.abc.com',25
AS
BEGIN
SET NOCOUNT ON;
Declare @is_broker_enabled sql_variant
Declare @is_DBMail_Xps_Enabled sql_variant
Declare @is_advanced_options sql_variant
Declare @errormessage nvarchar(1000)
Declare @ReturnCode int
select @is_DBMail_Xps_Enabled = value_in_use FROM sys.configurations WHERE name = 'Database Mail XPs'
select @is_broker_enabled = is_broker_enabled from sys.databases where is_broker_enabled = 1 and name = 'msdb'
select @is_advanced_options = value_in_use from sys.configurations where name = 'show advanced options'
--select @is_broker_enabled, @is_DBMail_Xps_Enabled
if ( (@profile_name = '') or (@profile_name is null))
Begin
Raiserror('Profilename cannot be null or empty, supply appropriate profilename!!!!!',16,1)
Return
end
if ( @is_DBMail_Xps_Enabled = '' or (@is_DBMail_Xps_Enabled is null))
Begin
Raiserror('Check the sp_configure with Adavanced Options for Database Mail XPs',16,1)
Return
end
if (@is_broker_enabled = '' or (@is_broker_enabled is null))
Begin
Raiserror('Check can you manually enable is_broker_enabled option ',16,1)
Return
end
if (@is_advanced_options = '' or (@is_advanced_options is null))
Begin
Raiserror('Check can you manually enable @is_advanced_options option',16,1)
Return
end
if ((@is_DBMail_Xps_Enabled = 0) and (@is_advanced_options=0))
Begin
Print 'Database Mail XPs configuration Option is disabled and enabling it(Database Mail XPs)'
Exec sp_configure 'show advanced options',1
Reconfigure with override
Exec sp_configure 'Database Mail XPs',1
Reconfigure with override ;
End
else if ((@is_DBMail_Xps_Enabled = 0) and (@is_advanced_options=1))
Begin
Exec sp_configure 'Database Mail XPs',1;
Reconfigure with override ;
end
if(@is_broker_enabled=0)
Begin
alter database msdb set enable_broker with rollback immediate
end
else
Begin
Print 'is_broker_enabled on msdb......already!!!!!'
end
-- Code Starts here
Begin Try
--Creating the profile
BEGIN
IF not EXISTS (SELECT * FROM msdb.dbo.sysmail_profile WHERE name = @profile_name)
BEGIN
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = @profile_name,
@description = 'Profile.'
-- Grant access to the profile to the DBMailUsers role
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = @profile_name,
@principal_name = 'public',
@is_default = 1 ;
Print @profile_name+' profile Created successfully'
END
ELSE
Begin
Print @profile_name+ 'Profile name already there !!!!!'
end
IF not EXISTS (SELECT * FROM msdb.dbo.sysmail_account WHERE name = @account_name )
BEGIN
--Create a Database Mail account
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = @account_name,
@description = '',
@email_address = @Email_Address,
@display_name = @Display_Name,
@mailserver_name = @smtp_server,
@port = @port_number,
@use_default_credentials = 1;
-- Add the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = @profile_name,
@account_name = @account_name,
@sequence_number =1 ;
Print @account_name+' account Created successfully'
END
ELSE
Begin
Print @account_name+'Account name already there !!!!!'
end
END
--Print @profile_name+' profile Created successfully... you can test it using sending email.....'
End Try
Begin Catch
select @errormessage = ERROR_MESSAGE()
Raiserror(@errormessage,16,1)
Raiserror('Please refer to above error messgae',16,1)
End Catch
END
GO