sp_send_dbmail #from_address - add multiple emails? - sql-server

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.

Related

How do you send email with TSQL using a Agent Operator instead of an email address?

I have some custom audit processes that send emails with sp_send_mail. I discovered recently that testing was sending out emails to everybody because the recipient address was hard coded as a variable value. I'd rather use an Operator so I don't have to alter code moving from one environment to the next. I've googled but this doesn't seem to be a thing unless I'm just using the wrong key words.
What is the proper #recipients value to use an Operator or should I be using a different proc all together?
You're definitely on the right track but you just need to retrieve the email address of the operator based on the name of the operator like this:
DECLARE #OperatorName sysname = N'OnCallDBAs';
DECLARE #OperatorEmailAddress nvarchar(100)
= (SELECT email_address
FROM msdb.dbo.sysoperators
WHERE [name] = #OperatorName);
IF #OperatorEmailAddress IS NOT NULL
BEGIN
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Adventure Works Administrator',
#recipients = #OperatorEmailAddress,
#body = 'The stored procedure finished successfully.',
#subject = 'Automated Success Message';
END;
Hope that helps.

How to send an email to multiple recipients ?

I have a stored procedure which sent emails to few recipients. In this I want to send to two differet recipients using #copy_recipients. But I get a syntax error. How to make this work?
stored procedure code
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'mail',
#recipients = #Mail1,
#copy_recipients = #Mail2;#Mail3,
#body =#body ,
#subject =#subject
You need to add ; (semicolon) between e-mail addresses using string concatenation:
DECLARE #copy_to varchar(max)= #Mail2+';'+#Mail3
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'mail',
#recipients = #Mail1,
#copy_recipients = #copy_to,
#body =#body ,
#subject =#subject
You can read MSDN article here
[ #recipients= ] 'recipients'
Is a semicolon-delimited list of e-mail
addresses to send the message to. The recipients list is of type
varchar(max). Although this parameter is optional, at least one of
#recipients, #copy_recipients, or #blind_copy_recipients must be
specified, or sp_send_dbmail returns an error.
[ #copy_recipients= ] 'copy_recipients'
Is a semicolon-delimited list
of e-mail addresses to carbon copy the message to. The copy recipients
list is of type varchar(max). Although this parameter is optional, at
least one of #recipients, #copy_recipients, or #blind_copy_recipients
must be specified, or sp_send_dbmail returns an error.

SQL Database Mail not sending with certain profile

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.

How to create a string using concat to assign to a variable

I'm using the sp_send_dbmail stored procedure, and I'm trying to include the result from a query in the body string. I can't seem to get this to work. Any ideas would be much appreciated.
use msdb
go
exec sp_send_dbmail
#profile_name = 'WarehouseEmailer',
#recipients = 'someone#example.com',
#subject = 'Database Mail Test 1',
#Body = 'This is a test email from SQL Server. <br> This should be line 2. <br> The
subject\'s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject),
#body_format = 'HTML'
Obviously this example just a simple test, but it is exactly the type of data I'd like to pass to the stored procedure to include in the email body. I look forward to your responses and many thanks for any advice!
Dynamic SQL, or concatenated strings, cannot be passed into the procedures parameters like that.
Try this:
use msdb
go
declare #msgbody nvarchar(1000)
set #msgbody = 'This is a test email from SQL Server.' + char(10) + 'This should be line 2. <br> The
subject''s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject)
exec sp_send_dbmail
#profile_name = 'WarehouseEmailer',
#recipients = 'someone#example.com',
#subject = 'Database Mail Test 1',
#Body = #msgbody,
#body_format = 'HTML'

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