SQLTeam.com | Weblogs | Forums

Unable to Send Mail When @Recipients is a variable

Another day brings a new issue but Happy Friday!

I need to be able to loop thru rows and send an email to the appropriate recipients. I'm retrieving the recipient addresses from a table and placing them into a variable

For example @recipients = '@WhoToSendTo',.

Declare @WhoToSendTo varchar(250);
Set @WhoToSendTo = 'MyEmailAddy.com';

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MyProfile',

@recipients = '@WhoToSendTo',

.
.
.
But sp_send_dbmail queus the mail but doesn't send, it gives this error:

Cannot send mails to mail server. (A recipient must be specified.
Any ideas?

Because there is no one by that email adress of '@WhoToSendTo'. That is not a proper email adress. Remove the single quotes

1 Like

Well, of course, yosiasz. Thanks for the tip!