Loading

VB.NET 发送outLook邮件body基于Html样式

 

首先要添加

 .IsBodyHtml = True

 1  ''' <summary>
 2     ''' SFC系统发送邮件
 3     ''' </summary>
 4     ''' <param name="MailTitle">邮件标题</param>
 5     ''' <param name="MailContent">邮件正文</param>
 6     ''' <param name="SendTypeID">发送类型</param>
 7     ''' <returns>发送成功返回 True</returns>
 8     ''' <remarks></remarks>
 9     Public Function SendMail(MailTitle As String, MailContent As String, SendTypeID As Integer) As Boolean '发送邮件
10         SendMail = False
11         Try
12             Dim client As System.Net.Mail.SmtpClient
13             Dim Mailmsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
14             ' Try
15             Mailmsg.From = New System.Net.Mail.MailAddress("sfc@tsmt.com.tw")
16            Dim Sql As String = "Select f002 from OP_EMail"
17             Dim DT As DataTable = SFCGetDataTable(Sql)
18             If DT.Rows.Count > 0 Then
19                 For i As Integer = 0 To DT.Rows.Count - 1
20                     ''收件人列表
21                     Mailmsg.To.Add(DT.Rows(i).Item(0).ToString)
22                 Next
23 
24             End If
25 
26             With Mailmsg
27                 .Subject = MailTitle
28                 .Body = MailContent
29                 .IsBodyHtml = True  '不启用的话,Html格式不会显示
30             End With
31             client = New System.Net.Mail.SmtpClient("spam.tsmt.com.tw")
32 
33             client.Send(Mailmsg)
34             SendMail = True
35             client = Nothing
36             Mailmsg.Dispose()
37         Catch ex As Exception
38             SendMail = False
39         End Try
40     End Function

调用方法:

 1 Sub GetGQXX()
 2         Try
 3             Dim sql As String
 4             sql = "SELECT  t1.f002 as 工号,t2.f002 as 姓名,t2.f018 as 部门, t1.f003 as 教育岗位," &
 5             "to_char(t1.f009, 'yyyy/mm/dd') as 过期日 FROM op_info t1,OP_PERSL t2  where t2.f003= t1.f002    " &
 6            "and  t1.f009<SYSDATE +" & NumericUpDown1.Value & "order by t1.f009"
 7 
 8             Dim dt As DataTable = SFCGetDataTable(sql) '获得岗位证书过期的信息
 9 
10             If dt Is Nothing Then
11                 Exit Sub
12             End If
13             Dim MsgInfox As String = "<table border='1' cellspacing='0' cellpadding='0'" & _
14                                 " style=""font-family:'微软雅黑',Helvetica,Arial,sans-serif;font-size:14px;text-align: center;"" width='100%'>"
15             If dt.Rows.Count > 0 Then
16                 For Each o As DataColumn In dt.Columns
17                     MsgInfox += "<td  style=""font-family:'微软雅黑',Helvetica,Arial,sans-serif;font-size:14px;text-align: center;color: white;background-color: blue;"">" + o.ColumnName + "</td>"
18                 Next
19                 Dim cols As Integer = dt.Columns.Count
20                 Dim rows As Integer = dt.Rows.Count
21 
22                 For r As Integer = 0 To rows - 1 
23                     MsgInfox += "<tr>"
24                     For c As Integer = 0 To cols - 1
25                         MsgInfox += "<td style=""font-family:'微软雅黑',Helvetica,Arial,sans-serif;font-size:14px;text-align: center;"">" + dt.Rows(r).Item(c).ToString() + "</td>"
26                         If c = cols - 1 Then
27                             MsgInfox += vbCrLf
28                         End If
29                     Next
30                     MsgInfox = MsgInfox + "</tr>"
31                 Next
32                 MsgInfox = MsgInfox + "</table>"
33                 'For i As Integer = 0 To dt.Rows.Count - 1
34                 '    MsgInfox += "工号:" & dt.Rows(i).Item(1).ToString & "    姓名:" & dt.Rows(i).Item(2).ToString & "    岗位:" & dt.Rows(i).Item(7).ToString & "    过期日期:" & CDate(dt.Rows(i).Item(13).ToString) & vbCrLf
35                 'Next
36 
37                 If SendMail("上岗证过期预警通知", "各位同仁,以下人员上岗证即将过期(提前" & NumericUpDown1.Value & "天预警),请及时处理" & vbCrLf & vbCrLf & MsgInfox, 0) = True Then
38                     Label2.Text = "发送上岗证过期预警成功,共发送 " & dt.Rows.Count & ""
39                 End If
40             Else
41                 Label2.Text = "发送上岗证过期预警成功,共发送 " & dt.Rows.Count & ""
42 
43 
44             End If
45             TSJS = 0
46             K += 1
47         Catch ex As Exception
48             MsgBox(ex.Message)
49         End Try
50     End Sub

 

posted @ 2020-09-29 14:42  云辰  阅读(434)  评论(0编辑  收藏  举报