Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I have an SP which sends password recovery email to certain accounts. To implement the same I have to attach to the body, a variable which will be picked from the database and CANNOT be hardcoded. Please suggest ways to do the same.

PFB the sp I am using,


SQL
CREATE PROCEDURE [dbo].[spGetPassword](
@Username Varchar(50))
AS
BEGIN

DECLARE @Password Varchar(100)
DECLARE @RecEmail Varchar(100)
Select @Password=[Password] From [dbo].[LoginDetails] Where [UserName]=@Username

--For sending emails
Select @RecEmail= Value FROM MemberDetails WHERE EmpName=@Username AND Name='Email'
execute msdb.dbo.sp_send_dbmail
@profile_name='*******',@recipients=@RecEmail,--@copy_recipients =@CopyEmail,
@subject= 'Password Recovery', <b<big>>@body= 'Hi, Your password is',@query='Select Password Where Username=@Username'</big>




END


It is working fine but the query bit is not executing, maybe, because I am doing it wrong...Please suggest ways to successfully implement the same.

Thanks
Posted
Comments
Richard Deeming 16-Mar-15 10:42am    
You appear to be storing passwords in plain text. That is a terrible idea. You should only ever store a salted hash of the password.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

1 solution

Try this. The problem with your query is that @username local variable wont be available in the @query because it is being executed in separate session. You have already got the password in @Password so I am using it in your send mail.

SQL
CREATE PROCEDURE [dbo].[spGetPassword](
@Username Varchar(50))
AS
BEGIN

DECLARE @Password Varchar(100)
DECLARE @RecEmail Varchar(100)
DECLARE @bodyMsg nvarchar(max)
Select @Password=[Password] From [dbo].[LoginDetails] Where [UserName]=@Username
set @bodyMsg = 'Hi, Your password is' + @Password

--For sending emails
Select @RecEmail= Value FROM MemberDetails WHERE EmpName=@Username AND Name='Email'

execute msdb.dbo.sp_send_dbmail
@profile_name='*******',
@recipients=@RecEmail,
@subject= 'Password Recovery',
@body = @bodyMsg

END
 
Share this answer
 
Comments
Shivangi_K 16-Mar-15 9:46am    
Thanks that helped :)
John C Rayan 16-Mar-15 9:49am    
Glad to help! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900