kennyaidan
Programmer
Hi, I'm a beginner asp.net developer and i am trying to learn how to send emails. I can get the emails to send when i hard code the "objMail.From" and "objMail.To" email addresses in. However what i was trying to do was to have a list of email addresses in my database and on the asp.net email form when i click on a check box the email is sent to those people within the database.
For example here's my database table
usertype | email
normal blah@blah.com
normal hello@blah.com
advanced advanced@blah.com
advanced bye@blah.com
And my check boxes set out like this
Checkbox 1 - normal
Checkbox 2 - advanced
So when i click checkbox 1 I want to send mails to "blah@blah.com" and "hello@blah.com".
Here's my code
'Database connection code
Dim cn As SqlConnection
Dim cmd As String
Dim ecmd As SqlCommand
Dim dr As SqlDataReader
Dim aidan As String
cn = New SqlConnection("pwd=*****;UID=****;server=****"
cn.Open()
dim i
'for loop for all selected items in checkbox list "check1"
for i=0 to check1.Items.Count-1
'if checkbox i has been selected
if check1.Items(i).Selected then
'assign checked the value from the checkbox
Dim checked as String = check1.Items(i).Text
'select all email addresses from table users
cmd = "select email from users where usertype = '" & checked & "'"
ecmd=new SqlCommand(cmd,cn)
ecmd.ExecuteNonQuery()
dr = ecmd.ExecuteReader()
'While there are records in the database send emails
while (dr.Read())
aidan=dr.GetString(0)
Dim objMail As New MailMessage()
objMail.From = "aidan@blah.com"
'Send to all users with type "aidan"
objMail.To = aidan
objMail.Subject = "Message
objMail.Body = "hello "
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = "****.****"
SmtpMail.Send(objMail)
End While
dr.close()
End If
next
cn.close()
Can anyone help me with where i'm going wrong. When i hardcode in the destination email addresses it works fine but it doesn't move into the while (dr.Read()) loop when i use the check boxes as the destination. Any help greatly appreciated.
Thanks
Aidan
For example here's my database table
usertype | email
normal blah@blah.com
normal hello@blah.com
advanced advanced@blah.com
advanced bye@blah.com
And my check boxes set out like this
Checkbox 1 - normal
Checkbox 2 - advanced
So when i click checkbox 1 I want to send mails to "blah@blah.com" and "hello@blah.com".
Here's my code
'Database connection code
Dim cn As SqlConnection
Dim cmd As String
Dim ecmd As SqlCommand
Dim dr As SqlDataReader
Dim aidan As String
cn = New SqlConnection("pwd=*****;UID=****;server=****"
cn.Open()
dim i
'for loop for all selected items in checkbox list "check1"
for i=0 to check1.Items.Count-1
'if checkbox i has been selected
if check1.Items(i).Selected then
'assign checked the value from the checkbox
Dim checked as String = check1.Items(i).Text
'select all email addresses from table users
cmd = "select email from users where usertype = '" & checked & "'"
ecmd=new SqlCommand(cmd,cn)
ecmd.ExecuteNonQuery()
dr = ecmd.ExecuteReader()
'While there are records in the database send emails
while (dr.Read())
aidan=dr.GetString(0)
Dim objMail As New MailMessage()
objMail.From = "aidan@blah.com"
'Send to all users with type "aidan"
objMail.To = aidan
objMail.Subject = "Message
objMail.Body = "hello "
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = "****.****"
SmtpMail.Send(objMail)
End While
dr.close()
End If
next
cn.close()
Can anyone help me with where i'm going wrong. When i hardcode in the destination email addresses it works fine but it doesn't move into the while (dr.Read()) loop when i use the check boxes as the destination. Any help greatly appreciated.
Thanks
Aidan