kennyaidan
Programmer
Hi,
I am trying to send an email to different sets of users depending on what check boxes i choose. The users types are contained within an sql table called user1. When i try to get my code to work it doesn't give me an error however it does not send the email to anyone. When i hard code in the type of user i want to send the email to, it works fine, so i think i'm right in concluding that i have set my checkboxes up wrongly.
Here's the relevant parts of my code
'sets up connect to my sql database
Dim users As String
cn = New SqlConnection("pwd=xxx;UID=xxx;server=xxxx"
cn.Open()
'for all checkboxes selected
dim i
for i=0 to check1.Items.Count-1
'if item selected
if check1.Items(i).Selected then
'select user type from user1 table
cmd = ("select * from user1 where type='" & check1.Items(i).Text & "'"
ecmd=new SqlCommand(cmd,cn)
dr = ecmd.ExecuteReader()
'while records exist
while (dr.Read())
users=dr.GetString(1)
'send email to user "users"
Dim objMail As New MailMessage()
objMail.From = "myaddress@blah.com"
objMail.To = users
objMail.Subject = "Email"
objMail.Body = "Hi " & Request.Form("Name" &", here the newsletter info " & Request.Form("Message" &""
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = "xxx.xxxx.xx.xxx"
SmtpMail.Send(objMail)
End While
dr.close()
End If
next
cn.close()
Heres my check box code
<asp:CheckBoxList id="check1" TextAlign="Right" runat="server">
<asp:ListItem>Normal</asp:ListItem>
<asp:ListItem>Section<asp:ListItem>
<asp:ListItemSystemasp:ListItem>
</asp:CheckBoxList>
If anyone could suggest where I am going wrong it would be great help
Thanks
aidan
I am trying to send an email to different sets of users depending on what check boxes i choose. The users types are contained within an sql table called user1. When i try to get my code to work it doesn't give me an error however it does not send the email to anyone. When i hard code in the type of user i want to send the email to, it works fine, so i think i'm right in concluding that i have set my checkboxes up wrongly.
Here's the relevant parts of my code
'sets up connect to my sql database
Dim users As String
cn = New SqlConnection("pwd=xxx;UID=xxx;server=xxxx"
cn.Open()
'for all checkboxes selected
dim i
for i=0 to check1.Items.Count-1
'if item selected
if check1.Items(i).Selected then
'select user type from user1 table
cmd = ("select * from user1 where type='" & check1.Items(i).Text & "'"
ecmd=new SqlCommand(cmd,cn)
dr = ecmd.ExecuteReader()
'while records exist
while (dr.Read())
users=dr.GetString(1)
'send email to user "users"
Dim objMail As New MailMessage()
objMail.From = "myaddress@blah.com"
objMail.To = users
objMail.Subject = "Email"
objMail.Body = "Hi " & Request.Form("Name" &", here the newsletter info " & Request.Form("Message" &""
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = "xxx.xxxx.xx.xxx"
SmtpMail.Send(objMail)
End While
dr.close()
End If
next
cn.close()
Heres my check box code
<asp:CheckBoxList id="check1" TextAlign="Right" runat="server">
<asp:ListItem>Normal</asp:ListItem>
<asp:ListItem>Section<asp:ListItem>
<asp:ListItemSystemasp:ListItem>
</asp:CheckBoxList>
If anyone could suggest where I am going wrong it would be great help
Thanks
aidan