Reputation: 43
I'm building a windows forms app on vb .net, one of them send a HTML email. I can send the email and the html tags and css wont show which means the program "Knows" it is html and not just a string. BUT I'm only getting the chunks of text (on any p, h or a tag) as plane text with no format one after another.
I tried to use EASendmail from Nuget packages with the same result. I searched on google and here for the problem without any success.
Here is what I have
Dim message As New MailMessage()
Dim fromAdd As MailAddress = New MailAddress("my mail here")
With message
.[To].Add("somebody email here")
.Subject = "TEST"
.From = fromAdd
.Priority = MailPriority.Normal
.IsBodyHtml = True
.BodyEncoding = System.Text.Encoding.UTF8
.Body = " <!DocType HTML>....the rest of the html code with the css..."
end with
Dim smtpClient As New SmtpClient("smtp.live.com")
With smtpClient
.EnableSsl = True
.Port = 587
.UseDefaultCredentials = False
.Credentials = New System.Net.NetworkCredential("[email protected]", "password")
.DeliveryMethod = SmtpDeliveryMethod.Network
.Send(message)
.Dispose()
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
now I expected a nice looking html email, which I saved as a html page and it shows just fine when opened in chrome. for some reason I only getting the text paragraphs on it as plane text without any formatting, any attachment is also ignored, the image files attached to the email but the mail just wont show them neither.
Upvotes: 1
Views: 1447
Reputation: 43
Here's how you can go about the HTML email
Dim mensaje01 As String = "<!docType HTML><html> ...and the rest of your html code here...</html>
Now, be sure to replace any quote marks with ' (DO NOT USE WORD OR WORPAD OR NOTEPAD TO DO IT... It will make some funny version of it and it won't work... took me 2 days to realize that. By the way notepad+ can replace all at once and it fine)
Dim VISTAHTML As AlternateView = AlternateView.CreateAlternateViewFromString(mensaje01, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)
Dim VISTATEXT As AlternateView = AlternateView.CreateAlternateViewFromString(mensaje01, Nothing, System.Net.Mime.MediaTypeNames.Text.RichText)
Try
Dim MENSAJE As MailMessage = New MailMessage
MENSAJE.AlternateViews.Add(VISTAHTML)
MENSAJE.AlternateViews.Add(VISTATEXT)
MENSAJE.From = New MailAddress("your email address here")
MENSAJE.To.Add("the recipient email")
MENSAJE.Subject = "SUBJECT"
Dim MISMTP As SmtpClient = New SmtpClient("the smptp client you are using")
MISMTP.EnableSsl = True
MISMTP.Port = "587"
MISMTP.Credentials = New Net.NetworkCredential("yor email address", "your password")
MISMT.Send(MENSAJE)
MsgBox("ENVIADO")
Catch ex As Exception
MsgBox(ex.Message)
End Try
If you need to add images to the email you can do it like this:
Dim image As LinkedResource = New LinkedResource("image.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg)
Image.ContentId = "TEMP1"
VISTAHTML.LinkedResources.Add(image)
Then you use the CID on your img tag to bring the image and you are done!
Upvotes: 1