Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 dbmail and recipient variables

Author  Topic 

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-12-01 : 10:16:20
Morning,

I am sooo close. I have the query to get the email sent to the users but when I try to execute I get the message:

Message
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2010-12-01T09:39:44). Exception Message: Cannot send mails to mail server. (Syntax error in parameters or arguments. The server response was: 5.5.4 Invalid Address).
)


Below is the code I'm using:

declare @emailAddress varchar(255),
@sql varchar(1000)
declare Maillist cursor
for

select distinct ''''+email_address+''''
FROM Psoft_sandbox1.dbo.vw_Get_Vendor_Info

OPEN Maillist
FETCH FROM Maillist into @emailAddress

WHILE NOT (@@fetch_status=-1)
BEGIN

begin
set nocount on
select vendor,po,description,duedate,amtleft,@emailAddress
from Psoft_sandbox1.dbo.vw_Get_Vendor_Info

if @@ROWCOUNT >0
begin
SET @SQL = 'select vendor,po,description,duedate,amtleft
FROM Psoft_sandbox1.dbo.vw_Get_Vendor_Info
where email_address= '+@emailAddress

EXEC msdb.dbo.sp_send_dbmail
@profile_name='AdminProfile',
@recipients = @emailAddress,
@query= @sql,
@subject = 'Unprocessed Order',
@attach_query_result_as_file = 0 ;
end
end

FETCH NEXT FROM Maillist INTO @emailAddress
END

CLOSE Maillist
DEALLOCATE Maillist


When I click execute these are the results I get:


vendor po description duedate amtleft
---------- -------- ------------------------- --------
WEST 39888 Grey Anodize & Clean 09/23/10 29 'myemail@myemail.com'
WEST 39922 Grey Anodize & Clean 09/23/10 1 'myemail@myemail.com'
WEST 40028 B22232 INTERFACE PLATE 10/07/10 49 'myemail@myemail.com'
WEST 40016 Alodine Surface Treatment 10/12/10 44 'myemail@myemail.com'
WEST 40163 10060045 CLAMP,FOCUS CELL 10/26/10 48 'myemail@myemail.com'
WEST 40137 Mask, Anodize 10/21/10 2 'myemail@myemail.com'
WEST 40174 Alodine 10/21/10 31 'myemail@myemail.com'

Mail queued.


Can anyone tell me what I need to do to make this work?

Thanks in advance.

Laura

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-01 : 10:41:59
You say you're close but have you managed to send any emails successfully?


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-12-01 : 10:50:56
Close, means it doesn't error out. :) No no emails sent.
Go to Top of Page

muralijv
Starting Member

2 Posts

Posted - 2010-12-01 : 11:05:57
1. What version of sql server are you using
2. Please make sure port 25 is not blocked by a firewall or anti virus software etc.
3. Try this statement and see the description
SELECT * FROM msdb.dbo.sysmail_event_log
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-12-01 : 11:10:17
Apologies. I meant no emails are sent using this script. When I go into database mail and say send test message that works fine. Its just the variable in the EXEC msdb.dbo.sp_send_dbmail statement that's breaking it.

Thanks

Laura
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-01 : 11:41:44
Ah - you are close then.

Can you send a message to that recipient?
Can you send a message via sp_send_dbmail at all
Can you send a message via sp_send_dbmail to that receipient without an attachment




==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-12-01 : 11:58:22
If I change the script to this:


EXEC msdb.dbo.sp_send_dbmail
@profile_name='AdminProfile',
@recipients='myemail@myemail.com',
@subject = 'Open Buy Report',
@body = @sql,
@body_format = 'HTML' ;


It works perfectly. That's what I don't understand the @emailaddress variable is sending the same thing. So when I say this:



EXEC msdb.dbo.sp_send_dbmail
@profile_name='AdminProfile',
@recipients=@emailAddress,
@subject = 'Open Buy Report',
@body = @sql,
@body_format = 'HTML' ;


Its says invalid email address. The address I'm echoing out is 'myemail@myemail.com' (obviously that's a fake email address but I do have the correct one in the script.)

Thanks

Laura
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-01 : 12:11:47
umm
where email_address= '''+ @emailAddress ''''
I would also get rid of the tabs

select @SQL = 'select vendor,po,description,duedate,amtleft'
+ ' FROM Psoft_sandbox1.dbo.vw_Get_Vendor_Info'
+ ' where email_address= ''' + @emailAddress + ''''


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-12-01 : 13:59:06
so simple when one find someone who knows what they're doing :)

Thanks for all your help that did it.

Laura
Go to Top of Page
   

- Advertisement -