使用vb.net在电子邮件中发送数据网格视图数据

时间:2015-03-28 16:23:57

标签: vb.net email datagridview

我试图将数据网格视图(或网格中的数据)发送到电子邮件,但我只收到一封空白的电子邮件,任何人都有任何想法如何发送修理它? 我正在使用的代码是:

Try
        SMTP.UseDefaultCredentials = False
        SMTP.Credentials = New Net.NetworkCredential("[e-mail address im sending from]", "[password for that e-mail]")

        SMTP.Port = 25
        SMTP.EnableSsl = True
        mail = New MailMessage
        mail.From = New MailAddress("[e-mail im sending from]")
        mail.To.Add(UserEmail.Text)
        mail.Body = DataTab.ToString
        mail.Subject = "Biology quiz Highscores"
        SMTP.DeliveryMethod = SmtpDeliveryMethod.Network
        SMTP.Send(mail)
        MsgBox("E-mail sent to: " & UserEmail.Text & "")
    Catch ex As Exception
        MsgBox("Unable to send e-mail. Please try again later.")
    End Try

使用mydataadpter sql语句从2003访问数据库获取数据时,数据表变量称为datatab。

1 个答案:

答案 0 :(得分:0)

Object.ToString(),除非被重写,否则与调用Object.GetType()。ToString()。

相同。

您想要在数据表中显示数据。你可以这样做:

Private Sub YourSubWhereYouSendEmail()
    '...

    Try
        Using smtp = New SmtpClient() With {
                                             .UseDefaultCredentials = False,
                                             .Credentials = New Net.NetworkCredential("[e-mail address im sending from]", "[password for that e-mail]"),
                                             .Port = 25,
                                             .EnableSsl = True,
                                             .DeliveryMethod = SmtpDeliveryMethod.Network
                                           }

            smtp.Send(New MailMessage("[e-mail im sending from]"), UserEmail.Text, "Biology quiz Highscores", DataTableToCSVString(DataTab)))
        End Using
        MsgBox("E-mail sent to: " & UserEmail.Text & "")
    Catch ex As Exception
        MsgBox("Unable to send e-mail. Please try again later.")
    End Try

    '...
End Sub

Private Function DataTableToCSVString(table As DataTable) As String
    With New Text.StringBuilder
        Dim once = False

        'headers
        For Each col As DataColumn In table.Columns
            If once = False Then
                once = True
            Else
                .Append(",")
            End If
            .Append(col.ColumnName)
        Next

        .AppendLine()

        'rows
        For Each s In table.Select.Select(Function(row) String.Join(",", row.ItemArray))
            .AppendLine(s)
        Next

        Return .ToString
    End With
End Function