SQL Server stored procedure send_dbmail failure in Google Account - sql-server

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

Related

SQL Email configuration on SQL Server Express

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' ;

"Failed to initialize sqlcmd library with error number -2147024809" error while sending query result via sql database mail

I want to use SQL Server 2014 database mail. I have configured email profile and successfully send emails with fixed text body. Now I try to send query result with email bit I receive
Failed to initialize sqlcmd library with error number -2147024809
My query:
EXEC sp_send_dbmail
#profile_name='test mail',
#recipients='mymail#mydomain.com',
#subject = 'test',
#execute_query_database = 'mydb',
#body = 'test',
#body_format = 'HTML',
#append_query_error = 1,
#query = N'SELECT * FROM mydb..FIExchangeRates;',
#query_result_header = 0,
#attach_query_result_as_file = 1,
#query_attachment_filename = 'Test.doc',
#exclude_query_output = 0,
#query_no_truncate = 1;
What I tried to solve?
Created a job with query above.
Gave db access to SQLSERVERAGENT user.
Set #exclude_query_output = 1
Trace with SQL Profiler
Google.
Here is the screenshot of problem

Mail queued but not delivered in SQL Server 2014

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

Mail sent through SQL Server Express - Mail (Id: 17) queued

I followed a guide online, to send a mail through SQL Server Express, but it keeps saying "Mail (Id: 1) queued.", and up to 17, as I have tried 17 times now.
The email is a dummy mail, and not my real one. It is just an example. I use my real mail in the SQL Server Management Studio. SQL Server Express as my teacher called it.
First part of code (To create sysmail account)
EXECUTE msdb.dbo.sysmail_add_account_sp
#account_name = 'MailTest',
#description = 'Sent Mail using MSDB',
#email_address = 'testmail#mail.com',
#display_name = 'Landlyst',
#username='testmail#mail.com',
#password='password',
#mailserver_name = 'mail.google.com'
Second part of code (To create Database Profile)
EXECUTE msdb.dbo.sysmail_add_profile_sp
#profile_name = 'MailTest',
#description = 'Profile used to send mail'
Third part of code (To add database Mail account to profile)
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
#profile_name = 'MailTest',
#account_name = 'MailTest',
#sequence_number = 1
Fourth part of code (Grants permission to all)
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
#profile_name = 'MailTest',
#principal_name = 'public',
#is_default = 1 ;
Fifth part of code (to enable the program to send mail, and not give an error)
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE
GO
Sixth part of code (To send the test mail)
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'MailTest',
#recipients = 'receiver#queryingsql.com',
#subject = 'Mail Test',
#body = 'Mail Sent Successfully',
#body_format = 'text'
I am not very good at SQL, but I need this to work as a trigger on a user.
Could someone please help me with this?
Source
I ran all the same commands you did (with the profile name of 'admin') and my msdb.dbo.sysmail_log table was still empty. I additionally followed step 5 in this link which suggests running the command below (back up the registry first). I did a search in the registry to find where UseDatabaseMail and DatabaseMailProfile were. However, the command did not work, so I changed the values manually in the registry.
USE msdb
GO
EXEC master.dbo.xp_instance_regwrite
N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL13.SQLEXPRESS\SQLServerAgent',
N'UseDatabaseMail',
N'REG_DWORD', 1
EXEC master.dbo.xp_instance_regwrite
N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL13.SQLEXPRESS\SQLServerAgent',
N'DatabaseMailProfile',
N'REG_SZ',
N'admin'
However, this also did send my queued mail. This link suggested trying to run the Databasemail.exe directly. I searched for 'databasemail' which was in 'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\Binn'. and ran as administrator. I got the error, "The following feature couldn't be installed: .NET Framework 3.5 (includes .NET 2.0 and 3.0)"
Once .NET 3.5 was successfully installed, my queued mail was sent!

How to send e-mail from SQL Server?

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.

Resources