通过可变电子邮件将Listview内容发送到outlook

时间:2016-05-10 20:31:17

标签: sql vb.net email listview outlook

我有这个使用vb.net的窗体,我调用了一个sql存储过程,并根据输入的参数ID得到了存储过程结果的列表视图。

现在问题是我似乎无法找到有关如何将这些结果发送到电子邮件(到Outlook)的任何信息,用户可以在表单上的文本框中输入要发送的电子邮件或如何引用列表视图中的结果。

我正在考虑创建一个带有@email变量的存储过程,它执行“sp_GetDocs”并将结果发送到过程中的@email变量,我可以输入该电子邮件变量,就像我在前面的参数中所做的那样。形成。我仍然是vb.net的初学者所以我不确定这是否是额外的工作或者它根本不工作。有人能指出我正确的方向吗? vb代码粘贴在下面。

 Imports System.Data.SqlClient
Public Class Form
Dim stlCon As String = "my data source"
Dim sqlCon As SqlConnection
Dim table As New DataTable
Dim PDF As New ListViewItem
Private Sub Button1_Click() Handles Button1.Click
    sqlCon = New SqlConnection(stlCon)

    Using (sqlCon)

        Dim sqlComm As New SqlCommand


        sqlComm.Connection = sqlCon


        sqlComm.CommandText = "sp_GetDocs"
        sqlComm.CommandType = CommandType.StoredProcedure

        sqlComm.Parameters.AddWithValue("@DOCID", Integer.Parse(TextBox1.Text.Trim()))

        sqlCon.Open()
        Dim sqlReader As SqlDataReader = sqlComm.ExecuteReader()

        Using sqlReader
            Dim table As New DataTable
            table.Load(sqlReader)

            ListView1.View = View.Details
            ListView1.GridLines = True
            ListView1.Columns.Add("PDF Name", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("PDF Description", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("PDF Location", 200, HorizontalAlignment.Left)

            For Each row In table.Rows

                Dim PDF As New ListViewItem
                PDF.Text = row.Item(0)
                PDF.SubItems.Add(row.item(1))
                PDF.SubItems.Add(row.item(2))

                For i = 1 To table.Columns.Count - 1
                    PDF.SubItems.Add(row(i).ToString(0))
                    PDF.SubItems.Add(row(i).ToString(1))
                    PDF.SubItems.Add(row(i).ToString(2))

                Next
                ListView1.Items.Add(PDF)
            Next
                ListView1.EndUpdate()

        End Using

    End Using
End Sub

Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

End Sub

Private Sub Email_Click(sender As Object, e As EventArgs) Handles Email.Click

End Sub
End Class

现在所有这一切都是执行sp_GetDocs并在列表视图中显示结果,目前我的表单底部有一个按钮和一个空文本框,用于此电子邮件部分。

1 个答案:

答案 0 :(得分:0)

想出来!对于microsoft 365,我使用下面的格式

尝试             MailBody =(“test”)

        MailSubject = ("Test")
        MailFrom = New MailAddress(TextBox2.Text)
        MailTo = New MailAddress(TextBox3.Text)

        Mail.From = MailFrom
        Mail.To.Add(MailTo)
        Mail.Subject = MailSubject
        Mail.Body = MailBody



        MailClient = New SmtpClient("smtp.office365.com")
        MailClient.Port = 587
        MailClient.EnableSsl = True
        MailClient.Credentials = New System.Net.NetworkCredential(TextBox2.Text, "Password", "domain")

        MailClient.Send(Mail)
        MsgBox("Email Sent")
    Catch ex As Exception
        MsgBox("Error")
    End Try