1

我需要发送一封电子邮件,其中包含一个在每个单元格中都有变量值的表格。我可以使用任何方法(通过电子邮件的 html、excel/word 表等)来做到这一点。唯一的障碍是由于 Emailer 程序和 System.Net.Mail 导入的限制,它必须是一个字符串。

这是我到目前为止所拥有的:

Imports DelayEmailer.DelayTrackerWs
Imports System.Configuration

Public Class DelayEmailer
Public Shared Sub Main()

    Dim ws As New DelayTrackerWs.DelayUploader
    Dim delays As DelayTrackerWs.Delay()
    Dim emailer As New Emailer()
    Dim delaystring As String

    delays = ws.SearchDelaysDate(DelayTrackerWs.AreaEnum.QT, DelayTrackerWs.UnitEnum.QT, Now.AddDays(-1), Now)

    delaystring = "Delays" & vbNewLine
    delaystring &= "Facilty  Start Time       Status  Category      Reason      Comment"
    For i = 0 To delays.Length - 1
        delaystring &= vbNewLine & delays(i).Facility & "   "
        delaystring &= FormatDateTime(delays(i).DelayStartDateTime, DateFormat.ShortDate) & " "
        delaystring &= FormatDateTime(delays(i).DelayStartDateTime, DateFormat.ShortTime) & " "
        'delaystring &= delays(i).DelayDuration & " "
        delaystring &= delays(i).Status & " "
        delaystring &= delays(i).CategoryCode & " "
        delaystring &= delays(i).ReasonCode & " "
        delaystring &= delays(i).Comment
    Next

    emailer.Send(ConfigurationManager.AppSettings("EmailList"), "delays", delaystring)

End Sub

如您所见,如果每个 delays(i) 的值相同,我目前只有一堆串联的字符串。另一个问题是,这需要通过移动设备轻松查看,并且使用字符串,它会包装并且变得非常不可读。这里的一张桌子应该可以解决这个问题。

4

1 回答 1

4

您可以使用MailMessageSmtpClient类从 .NET 发送 html 电子邮件,创建电子邮件模板string并将MailMessage'IsBodyHtml属性设置为 true:

Dim strHeader As String = "<table><tbody>"
Dim strFooter As String = "</tbody></table>"
Dim sbContent As New StringBuilder()

For i As Integer = 1 To rows
    sbContent.Append("<tr>")
    For j As Integer = 1 To cols
        sbContent.Append(String.Format("<td>{0}</td>", YOUR_TD_VALUE_STRING))
    Next j
    sbContent.Append("</tr>");
Next i

Dim emailTemplate As String = strHeader & sbContent.ToString() & strFooter
...
于 2013-10-18T20:15:31.293 回答