Skip to content Skip to sidebar Skip to footer

Images And Styles Are Not Appearing In Html Email

My image and styles are not appearing in the email (in Outlook 2016). This is how I'm doing it: c#: var body = File.ReadAllText('c:/emailtemplate.html'); MailMessage msg = new Mail

Solution 1:

Use full URL to the image and not its relative URL.

Something like: http://www.example.com/logo.gif

Solution 2:

Your img src should be at least absolute to have chance to be visible. To guarantee img visible convert it to base64 string and create LinkedResource. Also create AlternateView and attach it to MailMessage instance. All these features are in System.Net.Mail namespace. Here is an example in VB.NET to help you.

<div style="border:solid 1px #000;padding:20px;">
   <img src="data:image/jpeg;base64,####" style="width:250px">
   <p style="color:red;font-weight:bold">Thanks for signing up!</p>
</div>

Dim lnkRcs AsNew List(Of Net.Mail.LinkedResource)
Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(msgBody, "src=""(?<src>data:(?<mime>.+?);base64,(?<data>.+?))""")
Dim a AsInteger = 0While match.Success
    Dim stm = Convert.FromBase64String(match.Groups("data").Value)
    Dim rc AsNew Net.Mail.LinkedResource(New System.IO.MemoryStream(stm), match.Groups("mime").Value)
    rc.ContentId = "rc" & a
    rc.TransferEncoding = Net.Mime.TransferEncoding.Base64
    msgBody = msgBody.Replace(match.Groups("src").Value, "cid:" & rc.ContentId)
    lnkRcs.Add(rc)
    a += 1
    match = match.NextMatch()
EndWhileDim altHtml As Net.Mail.AlternateView = Net.Mail.AlternateView.CreateAlternateViewFromString(msgBody, Nothing, "text/html")
ForEach rc As Net.Mail.LinkedResource In lnkRcs
    altHtml.LinkedResources.Add(rc)
Next
msg.AlternateViews.Add(altHtml)

Post a Comment for "Images And Styles Are Not Appearing In Html Email"