I am trying to send an email(Auto email function) using items in a list box.
You can select one or more items to send.
The problem I am having is it keeps selecting the first item in the list box instead of what I selected.
I am setting this equal to a variable called cities where.
How do I get this to work to show the items I selected on the To = "" & cities where & "" line.
Here is my code for the email:
Code for the Dropdown list selected change:
When I click on the send button it put the first item list in the listbox in the to: email. When it should put what I select as the to: email.
For example, the listbox will have let's say 4 names and email addresses:
John(john@go.net)
Marc(marc@go.net)
Bill(bill@go.net)
Steve(steve@go.net)
I select Bill or Bill and Steve but instead of selecting Bill or Bill and Steve's email address it will select John's email address to send to when I didn't select John in the list box when i go through the for/next loop because it is the first item listed.
BTW I do have the list box set for multiple selection.
I know this is a lot to absorb but I felt I needed to include as much as I can so someone could understand what was going on.
You can select one or more items to send.
The problem I am having is it keeps selecting the first item in the list box instead of what I selected.
I am setting this equal to a variable called cities where.
How do I get this to work to show the items I selected on the To = "" & cities where & "" line.
Here is my code for the email:
VB.NET:
Function AutoEmail()
Dim objMailMessage As MailMessage
Dim strHtmlBody As String
conn = New SqlClient.SqlConnection(strconn)
conn.Open()
Dim currentAll As String
Dim strSQL = "SELECT d.group_descr, a.first_name, a.last_name, a.user_id, b.email, d.group_id, d.parent_group_id FROM icc_user_data AS a LEFT OUTER JOIN icc_users AS b ON a.user_id = b.user_id LEFT OUTER JOIN icc_users_groups AS c ON a.user_id = c.user_id LEFT OUTER JOIN icc_groups AS d ON c.group_id = d.group_id WHERE (b.user_id NOT IN (249)) AND (d.group_id IS NOT NULL) AND (d.group_id <> '8') AND (d.group_id <> '10') AND (d.group_id <> '46') AND (d.group_id <> '3') AND (d.group_id <> '47') AND (d.group_id <> '9') ORDER BY d.group_descr"
Dim Com As New System.Data.SqlClient.SqlCommand(strSQL, conn)
Dim rdr As System.Data.SqlClient.SqlDataReader = Com.ExecuteReader()
Dim citiesWhere As String
citiesWhere = String.Empty
Dim Item As ListItem
For Each Item In LBSendTo.Items
If (Item.Selected) Then
citiesWhere += "" & Item.Text + ";"
End If
Next
objMailMessage = New MailMessage
objMailMessage.From = "hsiinfo@us-hsi.com"
objMailMessage.To = "" & citiesWhere & ""
strHtmlBody = "<TABLE>"
strHtmlBody = strHtmlBody & "<TR><TD>Created By:</TD><TD>The report <strong>'" & Me.txtTitle.Text & "'</strong> has been uploaded for you and is ready for your review. <br>Please go to <a href='http://www.us-hsi.com'>www.us-hsi.com.</a><br>After logging in, click the Monthly Downloads link. You may then open or save your report.<br>If you have any questions, go to the Customer Service menu item and click New Request. <br> Type your inquiry in the space provided. A Customer Service Representative will respond shortly.<br><br><br>Health Systems International<br>5975 Castle Creek Parkway<br>Suite 100<br>Indianapolis, IN 46250<br><br>Phone: 1-800-962-6831<br>E-Mail: HSIInfo@us-icc.com</TD>"
strHtmlBody = strHtmlBody & "</TABLE>"
objMailMessage.Body = strHtmlBody
objMailMessage.BodyFormat = MailFormat.Html
SmtpMail.SmtpServer = "mail.us-hsi.com"
SmtpMail.Send(objMailMessage)
End Function
Code for the Dropdown list selected change:
VB.NET:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
conn = New SqlClient.SqlConnection(strconn)
conn.Open()
Dim strSQL = "SELECT d.group_descr, a.first_name, a.last_name, a.user_id, b.email, d.group_id, d.parent_group_id FROM icc_user_data AS a LEFT OUTER JOIN icc_users AS b ON a.user_id = b.user_id LEFT OUTER JOIN icc_users_groups AS c ON a.user_id = c.user_id LEFT OUTER JOIN icc_groups AS d ON c.group_id = d.group_id WHERE (b.user_id NOT IN (249)) AND (d.group_id IS NOT NULL) AND (d.group_id <> '8') AND (d.group_id <> '10') AND (d.group_id <> '46') AND (d.group_id <> '3') AND (d.group_id <> '47') AND (d.group_id <> '9') and d.group_id = '" & Me.DropDownList1.SelectedValue & "' ORDER BY d.group_descr"
Dim Com As New System.Data.SqlClient.SqlCommand(strSQL, conn)
Dim rdr As System.Data.SqlClient.SqlDataReader = Com.ExecuteReader
rdr.Read()
Me.DropDownList1.SelectedValue = rdr.Item(5).ToString
fillMonthlyDownloadsListBox()
LoadMonthlyDownloadsGrid()
End Sub
When I click on the send button it put the first item list in the listbox in the to: email. When it should put what I select as the to: email.
For example, the listbox will have let's say 4 names and email addresses:
John(john@go.net)
Marc(marc@go.net)
Bill(bill@go.net)
Steve(steve@go.net)
I select Bill or Bill and Steve but instead of selecting Bill or Bill and Steve's email address it will select John's email address to send to when I didn't select John in the list box when i go through the for/next loop because it is the first item listed.
BTW I do have the list box set for multiple selection.
I know this is a lot to absorb but I felt I needed to include as much as I can so someone could understand what was going on.