1

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("mymail@somewhere.com", "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.

  • 1
    Always create two [MailMessage.AlternateViews](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.alternateviews?view=netcore-3.1) one for the plain text and another for HTML MIME types. As for the images. you need to create a new [LinkedResource](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.linkedresource?view=netcore-3.1) for each and add them into the LinkedResources collection of the HTML view and replace the `cid` of each in the HTML with their paths without extensions. Long story? –  May 20 '20 at 23:13
  • 1
    Check [this](https://stackoverflow.com/q/19910871/10216583) out. –  May 20 '20 at 23:15
  • I tried that way and had the same result... well I just created the alterateview for html... it could be that??? – Esteban Luques May 21 '20 at 05:04
  • You should create both to 1) support non HTML clients and 2) ensure the delivery of your email if the HTML view was not well formatted. So the client displays it as an alternative if it fails to parse the HTML view. –  May 21 '20 at 08:52
  • 1
    I will try it and post the results here today thanks – Esteban Luques May 21 '20 at 16:34
  • same thing... starting to think the problem may be a bad formatting on the html code. It does open just fine as a index.html file on my computer... I used the same code just replaced " with ' . on my php web app worked like a charm using mime mail... but on vb net I cant make it work... – Esteban Luques May 21 '20 at 19:42
  • 1
    Fixed it!!! it was both, I just corrected the ' signs which where a funny version made by word, and after adding the 2 alterateview its working!! THANKS!! – Esteban Luques May 21 '20 at 20:45

1 Answers1

1

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!

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Nice - did not realize that LinkedREsources was part of the email object. I in the past was just including the converted mine byte stream right in the HTML body, and then using CID. So, LinkedResources is a nice find. However in your 2nd code snip, you declare a var of image, but on the next line you are using IMAGEN1. Where is this coming from? (where did you declare it?). Also, what type is IMAGEN1 created as? – Albert D. Kallal May 21 '20 at 22:42
  • It should be image, I'm argentinian so I'm coding everything in Spanish. When I published here I tried to put it in english so it will be "cleaner" to read(you can see I used mismtp and mensaje and it was too much work to change them so they stayed in spanish)... that one escaped me... sorry – Esteban Luques May 22 '20 at 04:30
  • 1
    Thank you kindly. I kind of guessed, but just in case I thought I would ask. As it turns out I am just coding some emails and using "cid" for imbedded (attached) images. I did not realize that the email object had the linkedResouces method - so this is most timely and helpful to the code I am working on. – Albert D. Kallal May 22 '20 at 04:36