我正在尝试制作一个程序,将查询结果(在数据集中)通过电子邮件发送给用户......我的代码是:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Paid_Out_TbTableAdapter.Fill(Me.dataset.Paid_Out_Tb)
Me.ReportViewer1.RefreshReport()
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("Bob", "password")
SmtpServer.Port = 25
SmtpServer.Host = "server"
mail = New MailMessage()
mail.From = New MailAddress("email@email.com")
mail.To.Add("Email@email.com")
mail.Subject = "Test Mail"
mail.Body = (Me.DataSet.Paid_Out_Tb.ToString)
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
除了发送电子邮件正文外,一切正常......我怎样才能将结果作为正文发送给客户?
答案 0 :(得分:2)
您可以使用Linq和VB的新内联xml文字功能来生成html。试试这个:
Dim payOuts = _
<html>
<body>
<table>
<tr><th>My First Column Header</th><th>My Second Column Header</th></tr>
<%= From paidOut In Me.DataDeliveryServiceDataSet.Paid_Out_Tb.AsEnumerable _
Select <tr>
<td><%= paidOut.MyFirstColum %></td>
<td><%= paidOut.MySecondColum %></td>
</tr> %>
</table>
</body>
</html>
mail.IsBodyHtml = True
mail.Body = payouts.ToString
确保在项目中包含对System.Data.DataSetExtensions.dll的引用(如果它尚不存在)。你班上还需要Imports System.Linq
。有关详细说明,请参阅ÉricMoreau使用LINQ and XML Literals to transform a DataTable into a HTML table博客发布。
答案 1 :(得分:1)
您需要遍历数据集的行并提取您希望通过电子邮件发送的数据。您可以通过添加以下语句使用HTML格式化它:
mail.IsBodyHtml = True