This is the class i use to send email:
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Net.Mail
Public Class EmailRelay
Public smtpServer As String
Public userName As String
Public password As String
Public Sub new(ByVal _smtpServer As String, ByVal _userName As String, ByVal _password As String)
smtpServer = _smtpServer
userName = _userName
password = _password
End Sub
Public Shared Function ServerCertificateValidationCallback(ByVal sender As Object, _
ByVal cert As System.Security.Cryptography.X509Certificates.X509Certificate, _
ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, _
ByVal sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean
Return sslPolicyErrors = Net.Security.SslPolicyErrors.None
End Function
Public Function SendMail(ByVal fromAddress As String, ByVal toAddress as String,
ByVal subject As String, ByVal body As String,
Optional ByVal fromEmailName As String = "",
Optional Byval useSSL As Boolean = True,
Optional ByRef errMsg As String = "") As Boolean
Dim mm As New MailMessage()
Dim smtp As New SmtpClient()
Dim emailFrom as New MailAddress(fromAddress, fromEmailName)
Dim NetworkCred As New System.Net.NetworkCredential()
Try
NetworkCred.UserName = userName
NetworkCred.Password = password
With mm
.[To].Add(New MailAddress(toAddress))
.From = emailFrom
.Subject = subject
.Body = body
.IsBodyHtml = True
End With
With smtp
.Host = smtpServer
.EnableSsl = useSSL
.UseDefaultCredentials = True
.Credentials = NetworkCred
.Port = 587
.Send(mm)
End With
Catch ex As Exception
errMsg = ex.Message
Return False
End Try
Return True
End Function
End Class
Notice the
.IsBodyHtml
line? Simply use this class and format your results in HTML.
Use the class like this:
Dim errMsg as String
Dim email as new EmailRelay("smtp.gmail.com", "you@gmail.com", "YourEmailPassword")
If NOT email.SendMail("you@gmail.com", "targetEmailAddress@wherever.com", _
"Subject Line", _
"<HTML>This is where your html formatted body content will go</HTML>", _
"Your name here", True, errMsg) Then
MsgBox("There was a problem sending your email. The error returned is: " & errMsg)
End If
You may need to set the "useSSL" parameter to false, depending on how your smtp server is configured.
- Pete