简体   繁体   English

使用Header.vb.net以表格格式发送到电子邮件(Outlook)

[英]Send to Email(Outlook) in a table format with Header.vb.net

This is a continuation from my previous question How to send to email (outlook) the selected items in SQL Server database using vb.net 这是我上一个问题的延续,该问题如何使用vb.net将电子邮件发送到SQL Server数据库中(忽略)所选项目

Now I can send it but the format is not easy to understand so I want to send data to email in table format with header, So that it is easy to understand. 现在我可以发送它了,但是格式不容易理解,所以我想将数据发送到带有标头的表格格式的电子邮件中,这样很容易理解。

Here is what i have now: 这是我现在所拥有的:

'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

I edit my code in Email Methods as below: I put the value of sb inside the table. 我在“电子邮件方法”中编辑代码,如下所示:我将sb的值放在表中。

'send email method
Public Sub sendEmail()
    Dim oMail As New SmtpMail("TryIt")
    Dim oSmtp As New SmtpClient()
    oMail.HtmlBody = True
    oMail.To = New AddressCollection("emil@calcomp.co.th")
    oMail.Cc = New AddressCollection("emil@calcomp.co.th,chokchai@calcomp.co.th")
    oMail.Subject = "FIXTURE ON SCHEDULE"

    Dim sb As New StringBuilder("<table width='1000px' align='center' border='1' cellpadding='20' cellspacing='0' style='border'top:5px solid white;> " & _
                              "  <tr><th>PatientName</th> <th>Gender</th> <th>ScheduleDate</th> <th>PersonIncharge")
    For Each row As DataGridViewRow In dgvOnSchedule.Rows

        sb.Append("<tr>")
        For Each cell As DataGridViewCell In row.Cells
            sb.Append("<td>")
            sb.Append(cell.Value)
            sb.Append("</td>")
        Next
        sb.Append("</tr>")
    Next
    sb.Append("</table>")
    oMail.HtmlBody = 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM