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

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!

Related

SQL Server stored procedure send_dbmail failure in Google Account

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

Error with automated mail

I'm working on a piece of code that will email me the results of some queries twice a day from SQL Server 2016. When I run this bit of code from my developer window, it works fine. When I run it from the job, I get this error:
Executed as user: myDB\svcAGCRM2016_PRD. Failed to initialize sqlcmd
library with error number -2147467259. [SQLSTATE 42000] (Error 22050).
The step failed.
This is the code that runs:
declare #recipient_name varchar(500)
declare #SqlQuery varchar(max)
SET #SqlQuery='exec [myDB].[dbo].[sp_DailyRejectionRate]'
set #recipient_name = 'myemail#email.com'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'SMTPProfile',
#recipients =#recipient_name,
#subject = '[INFO]'
,#query='exec [myDB].[dbo].[sp_DailyRejectionRate];
I'm running this from a service account. Weirdly, not with the name of the service account in the error. Actually, I can't even find that account.
In the code, I'm doing an exec because otherwise the query is too long for query.
The service account that runs this is an SA. Also, I wrote another query this morning that sends out email with not problems today.
Any ideas?
What if you explicitly specify the user in the query string?
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'SMTPProfile',
#recipients =#recipient_name,
#subject = '[INFO]'
,#query='execute as user='my_sa_user' exec [myDB].[dbo].[sp_DailyRejectionRate];

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

How to set up mailing profile on a Express Edition of SQL Server?

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.

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