VB.Net Send email to each user from MSSQL DB table.

Hi all,

I am stuck trying to loop through an SQL table, finding each user email, and then sending an email to each individual one at a time.
Do you guys have an example on how to do this?  I have the email code that works nicely.  But I need to fill the email address dynamically from the table and send to each user one at a time not as a group.

thank you!
kouts1Asked:
Who is Participating?
 
Pawan KumarDatabase ExpertCommented:
You can use the below code for loop. I have used UserEmailId as column for example.

DECLARE @UserEmailId AS VARCHAR(MAX) = ''
 
IF OBJECT_ID('tempdb..#LoopTableData') IS NOT NULL
    DROP TABLE #LoopTableData
 
SELECT * INTO #LoopTableData FROM YourTableName 

WHILE EXISTS(SELECT TOP 1 1 FROM #LoopTableData)
BEGIN
    SELECT TOP 1 @UserEmailId = UserEmailId FROM #LoopTableData 

	/* YOUR EMAIL CODE Here*/
 
    DELETE FROM #LoopTableData WHERE UserEmailId = @UserEmailId
END
 
 

Open in new window

0
 
Nitin SontakkeDeveloperCommented:
Where precisely you want to do this? If you are attempting to do this in VB.NET, you obviously will have to do connect to db. Hopefully you know all that stuff, already.

You can then get a SQL DataReader to loop through all the records and send the mail.
0
 
Pawan KumarDatabase ExpertCommented:
Provided multiple suggestions
Question abandoned.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.