SQL Database Mail not sending with certain profile - sql-server

I am trying to get SQL database mail to send emails using a new db mail profile and account. The new db mail profile and account that I am creating uses the exact same server name as the old db mail profiles and accounts on our server. Whenever I execute the command:
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'OLD PROFILE NAME',
#recipients = 'myEmail#email.com',
#subject = 'Test Email',
#body = '<html><body><p>test content test content test content test content test content</p></body></html>',
#body_format = 'HTML';
And email sends with no issues. However whenever I execute the command
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'NEW PROFILE NAME',
#recipients = 'myEmail#email.com',
#subject = 'Test Email',
#body = '<html><body><p>test content test content test content test content test content</p></body></html>',
#body_format = 'HTML';
I don't get an email. Whats even weirder is that I can look at the sent items using a msdb command and its saying that the emails that I am not getting are being sent??
Any thoughts or help would be greatly appreciated

Have you tried looking at the email profiles?
(In Management Studio->Database Mail->Configure Database Mail->Manage Database Mail accounts and profiles->View, change or delete an existing profile... )
Check the two profiles. See if there is an SMTP account to the two profiles and if they are valid.
My guess is that the SMTP account associated to the new profile is not valid.

Related

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

sp_send_dbmail #from_address - add multiple emails?

I'm trying to receive bounce back emails to multiple accounts, and I have tried setting up a email box that forwards out, but that isn't working. Is there a way to add multiple email addresses into the #from_address parameter?
EXECUTE msdb.dbo.sp_send_dbmail
#profile_name = #profile_name,
#recipients = 'test#test.com',
#subject = 'test',
#body = 'works',
#body_format = 'HTML',
#from_address = #email
The above code works with an email being bounced back, but is there a way to add multiple emails to the #from_address. CCing doesn't work as it just sends them the email and doesn't include the bounce back
#from_address = 'email1; email2' - doesn't work
An email can only be from a single address, however your #recipients should be able to contain multiple email addresses:
#recipients = 'test#test.com; test2#test.com'
...
#from_address = 'from#test.com'
If you want to have multiple people see a bounce-back email, configure an email, i.e. errors#test.com and allow multiple people to access that email.

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