1

I want to get users email id in listview and whatever email generated by listview according to query. Now on send click I want all email id generated by listview to get that email.

I know how to bind listview but how can I get email to send mail?

Private Sub BindListView()
        Dim constr As String = ConfigurationManager.ConnectionStrings("conio2").ConnectionString
        Using con As New MySqlConnection(constr)
            Using cmd As New MySqlCommand()
                cmd.CommandText = "SELECT email FROM users where city = 'new york' order by ID asc"
                cmd.Connection = con
                Using sda As New MySqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    sda.Fill(dt)
                    emailList.DataSource = dt
                    emailList.DataBind()
                End Using
            End Using
        End Using
    End Sub 
  • What exactly do you need help on? Extracting the email address from the bound listview OR Sending the mail to the email addresses that are bound to the listview? – Techie May 23 '16 at 11:01
  • @Spidey Sending the mail to the email addresses that are bound to the listview –  May 23 '16 at 11:13
  • Did you check [this thread](http://stackoverflow.com/questions/18326738/how-to-send-email-in-asp-net-c-sharp)? Even though this is C# you can easily convert this to VB. – Techie May 23 '16 at 11:52
  • @Spidey Sendin mail is not my problem. That I know. I just have problem where I need to send mail to multiple users & all that users have email id in my db. There is specific query I will pass each time so only related email ID will be fetched & only that users will recieve mail –  May 23 '16 at 11:59
  • Can you share the code for HTML part as well, only the ones relevant for Listview? – Techie May 23 '16 at 14:22

2 Answers2

0

From what I understand you have some function (or sub), that sends e-mails and you just need to provide data to that function.

For simplicity let's name this:

Private Sub SendEmail(address as string, title as string, body as string)

I have also added two TextBoxes:

  • TextBoxTitle for holding e-mail title and
  • TextBoxBody for holding e-mail body

Here's the code:

Private Sub SendEmail_Click(sender As Object, e As EventArgs) Handles SendEmail.Click
    For Each item As ListViewItem In emailList.Items
        SendEmail(item.SubItems.Item(0).Text, TextBoxTitle.Text, TextBoxBody.Text)
    Next
End Sub
JTO
  • 156
  • 2
  • It shows me error. SubItems is not member of ListViewItem –  May 23 '16 at 11:10
  • What about using: item.Text instead of item.SubItems.Item(0).Text? – JTO May 23 '16 at 11:16
  • I need to understand why that error is coming of using subitem. Because I have issue with that too. –  May 23 '16 at 11:18
0

Suppose if you have ListView defined as below in the HTML part:

<asp:ListView ID="emailList" runat="server" ConvertEmptyStringToNull="False">
    <ItemTemplate>
        <asp:Label Text='<%# Eval("Email") %>' runat="server" ID="lblEmail"></asp:Label>
    </ItemTemplate>
</asp:ListView>

You can enumerate over all the ListViewItems, get the Email IDs from the Label, and join it into a string str separated by comma (,) which can then be directly utilized for sending the email to multiple recipients.

Dim str As String = ""
For Each li As ListViewItem In emailList.Items
    Dim lbl As Label = CType(li.FindControl("lblEmail"), Label)
    If lbl.Text <> "" Then
        str = str + lbl.Text + ","
    End If
Next
str = str.Substring(0, str.Length - 1)

Use str within the message sending code as message.cc.Add(str)
where message is the object of System.Net.Mail.MailMessage

Techie
  • 1,491
  • 3
  • 18
  • 24