I am trying configure SQL database mail send email Gmail to Gmail but I got:
Message
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2021-04-14T09:21:14). Exception Message: Cannot send mails to mail server. (Failure sending mail.).
)
I do not know what I am doing wrong.
---- create the database account ----
Use MSDB
go
IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_account WHERE name = 'SQLServer Express')
BEGIN
--CREATE Account [SQLServer Express]
EXECUTE msdb.dbo.sysmail_add_account_sp
#account_name = 'SQLServer Express',
#email_address = 'sangitar23#gmail.com',
#display_name = 'Audit Partnership',
#replyto_address = '',
#description = '',
#mailserver_name = 'smtp.gmail.com',
#mailserver_type = 'SMTP',
#port = '587',
#username ='sangitar23#gmail.com',
#password = 'Atlanta2020!',
#use_default_credentials = 0 ,
#enable_ssl = 1 ;
END --IF EXISTS account
------ create the database mail profile --------
Use MSDB
go
IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_profile WHERE name = 'SQLServer Express Edition')
BEGIN
--CREATE Profile [SQLServer Express Edition]
EXECUTE msdb.dbo.sysmail_add_profile_sp
#profile_name = 'SQLServer Express Edition',
#description = 'This db mail account is used by SQL Server Express edition.';
END --IF EXISTS profile
------------ To assign a database mail account to the database mail profile--------
Use MSDB
go
IF NOT EXISTS(SELECT *
FROM msdb.dbo.sysmail_profileaccount pa
INNER JOIN msdb.dbo.sysmail_profile p ON pa.profile_id = p.profile_id
INNER JOIN msdb.dbo.sysmail_account a ON pa.account_id = a.account_id
WHERE p.name = 'SQLServer Express Edition'
AND a.name = 'SQLServer Express')
BEGIN
-- Associate Account [SQLServer Express] to Profile [SQLServer Express Edition]
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
#profile_name = 'SQLServer Express Edition',
#account_name = 'SQLServer Express',
#sequence_number = 1 ;
END
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'SQLServer Express Edition',
#recipients = 'sangitarai023#gmail.com',
#body = 'Email notification for Audit Partnership SFTP',
#subject = ' This email has been sent from SQL Server to notify, client uploaded data on the SFTP cite' ;
Related
I'm having problems with the send_dbmail procedure. I configured all with SSL enabled.
If I send through port 465, I get this error:
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2021-07-22T10:25:46). Exception Message: Cannot send mails to mail server. (Failure sending mail.)
And if I send through port 587, I get another error:
The mail could not be sent to the recipients because of the mail server failure (Sending Mail using Account 3 (2021-07-22T10:10:26).
Exception Message: Cannot send mails to mail server. (The remote certificate is invalid according to the validation procedure)
I tried to telnet and both ports are accessible from the server.
Can someone give me a hand?
That's the code to create and execute the test.
Sure, the code is the following.
-- CONFIG PROFILE & ACCOUNT --
EXEC msdb.dbo.sysmail_add_account_sp
#account_name = 'MAIL'
,#description = 'Send mail SQL Server Stored Procedure'
,#email_address = 'XXX#XXXXX.com'
,#display_name = 'XXXX'
,#replyto_address = 'XXXX#XXXXX.com'
,#mailserver_name = 'smtp.gmail.com'
,#username = 'XXXX#XXXXX.com'
,#password = 'XXXXXXXX'
,#port = 587
,#enable_ssl = 1
GO
EXEC msdb.dbo.sysmail_add_profile_sp
#profile_name = 'XXXXXXX'
,#description = 'Send mail SQL Server Stored Procedure'
GO
EXEC msdb.dbo.sysmail_add_profileaccount_sp
#profile_name = 'XXXXXX'
,#account_name = 'XXXXXX'
,#sequence_number = 1
GO
--TEST--
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'XXXXXXX'
,#recipients = 'testmail#XXXXX.com'
,#subject = 'Email from SQL Server'
,#body = 'TESTTTTT :)'
,#importance ='HIGH'
GO
Thanks
Recently, I have created an AWS RDS SQL Server database (SQL Server 2017 Express Edition, v14.00.3281.6.v1 using free tier templates). Then I can use the master user name and password to login the database server, and I can create database/login, but I cannot execute
EXEC sp_changedbowner 'testLogin'
to change the dba to the new-created login testLogin, I get an error
SQL Error [15151] [S0001]: Cannot find the principal 'testlogin', because it does not exist or you do not have permission.
The following is the whole SQL script I run. It seems that the master user doesn't have enough permission? Could anyone tell me how to fix it or how to make the script run successfully. BTW, the same script runs successfully in a traditional SQL Server database.
Thanks very much.
SET IMPLICIT_TRANSACTIONS OFF
USE master;
-- Get the SQL Server data path
DECLARE #data_path nvarchar(256)
SET #data_path = (SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
FROM master.sys.master_files
WHERE database_id = 1 AND file_id = 1)
DECLARE #model_data_size_kb int
SET #model_data_size_kb = (SELECT SUM(size) * 8
FROM master.sys.master_files
WHERE database_id = 3 and [type] = 0)
IF #model_data_size_kb < (250 * 1024)
SET #model_data_size_kb = 250 * 1024
DECLARE #model_log_size_kb int
SET #model_log_size_kb = (SELECT SUM(size) * 8
FROM master.sys.master_files
WHERE database_id = 3 and [type] = 1)
IF #model_log_size_kb < (50 * 1024)
SET #model_log_size_kb = 50 * 1024
DECLARE #schema_data_size_str nvarchar(12)
DECLARE #schema_log_size_str nvarchar(12)
SET #schema_data_size_str = CAST(#model_data_size_kb AS nvarchar(12))
SET #schema_log_size_str = CAST(#model_log_size_kb AS nvarchar(12))
-- execute the CREATE DATABASE statement
EXECUTE ('CREATE DATABASE testdb
ON
( NAME = testdb_dat1,
FILENAME = '''+ #data_path + 'testdb.mdf'',
SIZE = ' + #schema_data_size_str + 'KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 25MB )
LOG ON
( NAME = testdb_log1,
FILENAME = '''+ #data_path + 'testdb.ldf'',
SIZE = ' + #schema_log_size_str + 'KB,
MAXSIZE = 10GB,
FILEGROWTH = 5MB )
COLLATE SQL_Latin1_General_CP1_CS_AS'
);
GO
ALTER DATABASE testdb SET READ_COMMITTED_SNAPSHOT ON;
IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE type = 'S' and name = N'testlogin')
BEGIN
CREATE LOGIN testlogin WITH PASSWORD='testlogin', DEFAULT_DATABASE=testdb, CHECK_POLICY = OFF
END
USE testdb;
EXEC sp_changedbowner 'testlogin'; -- this statement reports exception
After research, I found that AWS RDS SqlServer doesn't support add more permission to master user, which means that it can not execute "sp_changedbowner" since it require "Take Ownship" permission on the database and requires the new owner has "impersonate" permission on the login or "control server" permission on the server. click this for more detail about sp_changedbowner
That is we cannot change db owner to others in RDS SqlServer, it is always a built-in "rdsa" user, but we can use ALTER ROLE db_owner ADD MEMBER testlogin to add user to the db_owner role, so that it can perform any database actions.
So I changed my script to the following and works well:
-- The above is the same
USE testdb;
IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE type = 'S' and name = N'testlogin')
BEGIN
CREATE LOGIN testlogin WITH PASSWORD='testlogin', DEFAULT_DATABASE=testdb, CHECK_POLICY = OFF
CREATE USER testlogin FOR LOGIN testlogin
END
ALTER ROLE db_owner ADD MEMBER testlogin
I am trying to send the result of a SQL query through email using SQL Server 2014. The problem is that the e-mails are getting queued, but are not delivered to the recipient. There are some issues with the connectivity to the server. The description I am getting is:
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2017-04-05T16:05:09). Exception Message: Could not connect to mail server. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.130.109:25).
My code is:
EXECUTE msdb.dbo.sysmail_add_account_sp
#account_name = 'MIS_Automation_Project',
#description = 'Mail account for office files.',
#email_address = 'my_email_address',
#display_name = 'MIS_Automation',
#mailserver_name = 'smtp.gmail.com' ;
-- Create a Database Mail profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
#profile_name = 'MIS_Automation',
#description = 'Profile used for mis automation project' ;
-- Add the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
#profile_name = 'MIS_Automation',
#account_name = 'MIS_Automation_Project',
#sequence_number =1 ;
-- Grant access to the profile to the DBMailUsers role
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
#profile_name = 'MIS_Automation',
#principal_name = 'guest',
#is_default = 1 ;
DECLARE #xml NVARCHAR(MAX)
DECLARE #body NVARCHAR(MAX)
SET #xml = CAST(( SELECT [clno] AS 'td','',[clname] AS 'td','',
[cladd] AS 'td'
FROM Client
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
SET #body ='<html><body><H3>Client Information</H3>
<table border = 1>
<tr>
<th> Client No </th> <th> Client Name </th> <th> Client Address </th>
</tr>'
SET #body = #body + #xml +'</table></body></html>'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'MIS_Automation', -- replace with your SQL Database Mail Profile
#body = #body,
#body_format ='HTML',
#recipients = 'recipient', -- replace with your email address
#subject = 'E-mail in Tabular Format' ;
How can I resolve this issue?
My best guess is you have used wrong authentication method to connect to google SMTP server (error message indicates you try to connect on port 25 but google uses secured SSL port 465 as far as I remember). Moreover there is no credentials passed neither thus it tries to use anonymous authentication which I think wouldn't work with gmail neither).
So for the troubleshooting you can start from some simple verification:
Please connect to your SQL Server via SSMS and navigate to Management -> Database Mail -> Configure Database Mail -> Manage Database Mail accounts and profiles -> View, change or delete an existing account and verify settings there. You should have SSL enabled (with port 465 specified) and basic authentication.
My other thought is that firewall blocks the connection so it would be second point worth to be verified.
If it still doesn't work following article might be useful:
https://technet.microsoft.com/en-us/library/ms187540%28v=sql.105%29.aspx?f=255&MSPPError=-2147217396
Is it possible to have a mailer profile on SQL Server Management Studio without "Database Mail"?
Here is a blog that details the process.
Below is a rough copy of the blog contents to future-proof this answer.
Enable the database mail stored procs:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE
GO
Create the sysmail account with sysmail_add_account_sp:
EXECUTE msdb.dbo.sysmail_add_account_sp
#account_name = 'MailTest',
#description = 'Sent Mail using MSDB',
#email_address = 'umashankar#queryingsql.com',
#display_name = 'umashankar',
#username='umashankar#queryingsql.com',
#password='password',
#mailserver_name = 'mail.queryingsql.com'
Create the database profile with sysmail_add_profile_sp:
EXECUTE msdb.dbo.sysmail_add_profile_sp
#profile_name = 'MailTest',
#description = 'Profile used to send mail'
Map the account to the profile with sysmail_add_profileaccount_sp:
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
#profile_name = 'MailTest',
#account_name = 'MailTest',
#sequence_number = 1
Grant a database principal (database user or role) to use the database profile:
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
#profile_name = 'MailTest',
#principal_name = 'public',
#is_default = 1 ;
--A principal_name of 'public' makes this profile a public profile, granting access to all principals in the database.
Test with sp_send_dbmail:
exec msdb.dbo.sp_send_dbmail
#profile_name = 'MailTest',
#recipients = 'receiver#queryingsql.com',
#subject = 'Mail Test',
#body = 'Mail Sent Successfully',
#body_format = 'text'
You should also look up the MSDN documentation for each stored procedure to be sure you're configuring your system correctly.
I assume you can set it up using the stored procs in MSDB. I have not tried this, but you might want to give it a shot.
Look at...
sysmail_add_account_sp
... and it's related stored procs.
sysmail_add_account_sp MSDN documentation
By default the SQL Express doesn't support database mail configuration through a GUI wizard, nevertheless it is possible to configure it through scripts.
These detailed instructions guide you.
I have configured database mail – send email ...create one account also... I used below query
EXEC msdb.dbo.sysmail_add_profile_sp
#profile_name = 'SQL 2008 Profile',
#description = 'Used for general-purpose emailing.'
The second script creates the new SMTP account:
EXEC msdb.dbo.sysmail_add_account_sp
#account_name = 'MailAcct1',
#description = 'SMTP Account.',
#email_address = 'jojin127#gmail.com',
#display_name = 'Mail Account',
#mailserver_name = 'smtp.gmail.com' ;
The third script associates this new account with the new profile:
EXEC msdb.dbo.sysmail_add_profileaccount_sp
#profile_name = 'SQL 2008 Profile',
#account_name = 'MailAcct1',
#sequence_number =1;
exec msdb.dbo.sysmail_add_principalprofile_sp
#profile_name = 'SQL 2008 Profile',
#principal_name = 'public',
#is_default = 0 ;
exec msdb.dbo.sysmail_configure_sp 'AccountRetryDelay', 1200
After all I go to sent test mail... after write to address getting error like
your test email has has been queued for processing.Depending on the network speed and the backlog of the SMTP server.it may take several minutes before the email is delivered to the receipt
please help me out...
One more sent test mail they asking one to address in there actually what I need to written
email or servername
You should use SQL Server's built-in mailer. After the wizard is set up, sending mail is trivial:
USE msdb
GO
EXEC sp_send_dbmail #profile_name='PinalProfile',
#recipients='foo#bar.com',
#subject='Hello world',
#body='Hello alien world, from ours, we come in peace.'
Try setting up database email through SSMS. There are more options in the GUI than you are using in your stored procedures.
You might need to set port number or some other small option that is in fact required.
Also, when sending through Gmail you need to enable external SMTP before you can start using it.