【发布时间】:2018-03-26 09:47:14
【问题描述】:
这是我上一个问题How to send to email (outlook) the selected items in SQL Server database using vb.net 的延续
现在我可以发送了,但是格式不太好理解,所以我想用带有标题的表格格式发送数据到电子邮件,这样很容易理解。
这是我现在拥有的:
'load the equipment on schedule on the 2nd table
Public Sub OnSchedule()
Dim conn As New SqlConnection("SERVER=L4SMTDB01\SMTDBS02;database = SMT_IT; user=sa;pwd=qwerty; ")
conn.Open()
Dim cmd As SqlCommand = conn.CreateCommand
cmd.CommandText = String.Format("select PatientName,Gender,ScheduleDate,PersonInCharge from " _
& "Schedule where ScheduleDate = CONVERT(date,getdate()) order by ScheduleDate")
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows Then
Dim dtSerial As New DataTable
dtSerial.Load(dr)
dgvOnSchedule.DataSource = dtSerial
Else
MsgBox("no data")
End If
dr.Close()
conn.Close()
End Sub
'send email
Public Sub sendEmail()
Dim oMail As New SmtpMail("TryIt")
Dim oSmtp As New SmtpClient()
oMail.To = New AddressCollection("emil@calcomp.co.th")
oMail.Cc = New AddressCollection("emil@calcomp.co.th,chokchai@calcomp.co.th")
oMail.Subject = "Patient Schedule today"
'send to email not in the table.
Dim sb As New System.Text.StringBuilder()
For Each row As DataGridViewRow In dgvOnSchedule.Rows
For Each cell As DataGridViewCell In row.Cells
sb.Append(cell.Value)
sb.Append("||")
Next
Next
oMail.TextBody = "" & sb.ToString()
Dim oServer As New SmtpServer("mailpe.calcomp.co.th")
Try
oSmtp.SendMail(oServer, oMail)
MessageBox.Show("send to email success")
Catch ex As Exception
MessageBox.Show("no success")
End Try
End Sub
End Class
【问题讨论】:
标签: sql-server vb.net