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
 General SQL Server Forums
 New to SQL Server Programming
 Send email from SQL Server using binding data

Author  Topic 

Ndthang
Starting Member

1 Post

Posted - 2009-08-24 : 10:39:47
Hi Gurus,

I am trying to create and send email from SQL SMTP Mail, my DBA sent me a script using "hard code" in email, subject and body which works fine:

exec [USTPA3TLSSQ107].PwCAdmin.dbo.Send_SMTP_Mail
@from_user=N'Thang.Nguyen10@gmail.com',
@to_user = N'Ize10@yahoo.com',
@subject = N'Testing Send SMTP Mail from Thang 2',
@message = N'Testing email from SQL SMTP'


However, when I tried to bind data by using email pulled from a table instead of "hard code",

DECLARE @HashCode int, @UserID int, @Subscribe2 int, @Email varchar(100)
DECLARE @From varchar(100), @To varchar(100), @SubjectContent varchar(100), @Body varchar(max)
DECLARE @from_user varchar(100), @to_user varchar(100), @Subject varchar(100), @message varchar(100)

DECLARE Emailcs CURSOR FAST_FORWARD FOR SELECT UserID, HashCode, Insight, Email FROM tblTemp_WNTSInsight_081709
OPEN Emailcs FETCH NEXT FROM Emailcs INTO @UserID, @HashCode, @Subscribe2, @Email

SET @Body='Testing Send email building from different parts'
SET @From='Thang.Nguyen10@gmail.com'
SET @SubjectContent='Testing Send SMTP Mail from Thang 3'
Print @Body + ' ' + @From + ' ' + @SubjectContent

WHILE @@FETCH_STATUS = 0
BEGIN

SET @To=@Email
PRINT CAST(@UserID as varchar(6))+' '+@To
exec [USTPA3TLSSQ107].PwCAdmin.dbo.Send_SMTP_Mail
@from_user=N@From,
@to_user = N@To,
@subject = N@SubjectContent,
@message = N@Body
---- The N here serve to recognize the Unicode
FETCH NEXT FROM Emailcs INTO @UserID, @HashCode, @Subscribe2, @Email
END
CLOSE Emailcs
DEALLOCATE Emailcs
------------------------*/
Testing Send email building from different parts Thang.Nguyen10@gmail.com Testing Send SMTP Mail from Thang 3
573711 Ize10@yahoo.com
(1 row(s) affected)
(0 row(s) affected)

573703 Ize10@yahoo.com
(1 row(s) affected)
(0 row(s) affected)


As you see, the execution is no fault, but I did not receive the email. Please let me know if there is anything I can do. Thanks.
   

- Advertisement -