Daniel Camburn
Daniel Camburn

Reputation: 85

Email a table using VB.Net

I need to send an email with a table that has variable values in each cell. I can do this using any method (html via email, an excel/word table, etc.). The only hitch is due to the restrictions of the Emailer program and System.Net.Mail import, it has to be a string.

Here's what I have so far:

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

As you can see, I currently have just a bunch of concatenated strings that line up if the values of each delays(i) are the same. The other problem is that this needs to be easily viewable via mobile devices and with the strings, it wraps and gets really unreadable. A table here should fix this.

Upvotes: 1

Views: 6418

Answers (1)

Software Engineer
Software Engineer

Reputation: 3956

You can send html email from .NET using MailMessage and SmtpClient classes, create an email template as string and set MailMessage's IsBodyHtml property to 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
...

Upvotes: 4

Related Questions