从listview向多个用户发送电子邮件

时间:2016-05-23 10:02:00

标签: asp.net vb.net listview

我希望在listview中获取用户的电子邮件ID,并根据查询获取listview生成的任何电子邮件。现在发送点击我希望listview生成的所有电子邮件ID都能收到该电子邮件。

我知道如何绑定listview但是如何才能收到发送邮件的电子邮件?

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 

2 个答案:

答案 0 :(得分:0)

根据我的理解,你有一些功能(或子功能)发送电子邮件,你只需要为该功能提供数据。

为简单起见,我们将其命名为:

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

我还添加了两个TextBox:

  • TextBoxTitle用于保存电子邮件标题和
  • 用于保存电子邮件正文的TextBoxBody

以下是代码:

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

答案 1 :(得分:0)

假设您在HTML部分中具有如下定义的ListView:

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

您可以枚举所有ListViewItems,从Label获取电子邮件ID,并将其加入由逗号(,)分隔的字符串str,然后可以直接用于将电子邮件发送给多个收件人。

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)

在邮件发送代码中使用str message.cc.Add(str)
其中message是System.Net.Mail.MailMessage

的对象