图片和样式不会出现在HTML电子邮件中 [英] Images and styles are not appearing in HTML email
问题描述
我的图片和样式未显示在电子邮件中(在Outlook 2016中)。这是我做的:
c#:
var body = File.ReadAllText(c:/emailtemplate.html);
MailMessage msg = new MailMessage(noreply@mycompany.com,you@outlook.com,test,body);
msg.IsBodyHtml = true;
var client = new SmtpClient(SmtpHost);
client.Send(msg);
emailtemplate.html
<
< div style =border:solid 1px#000; padding:20px;>
< img src =logo.pngstyle =width:250px>
< p style =color:red; font-weight:bold>感谢您注册!< / p>
< / div>
logo.png与emailtemplate.html位于相同的位置
任何想法如何包含图片并将样式应用到我的电子邮件中?
应该至少是绝对的,有机会可见。
为了确保img visible将其转换为base64字符串并创建 LinkedResource
。
还创建 AlternateView
并将其附加到 MailMessage
实例。所有这些功能都在 System.Net.Mail
命名空间。这里是一个VB.NET帮助你的例子。
< div style =border:solid 1px#000; padding :20px;>
< img src =data:image / jpeg; base64,####style =width:250px>
< p style =color:red; font-weight:bold>感谢您注册!< / p>
< / div>
Dim lnkRcs As New List(Net.Mail.LinkedResource)
Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(msgBody,src =(?< src>数据:(< mime。。+?); base64,(?"。+?))
Dim a As Integer = 0
虽然match.Success
Dim stm = Convert.FromBase64String(match.Groups(data)。Value)
Dim rc As New 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()
End While
Dim altHtml As Net.Mail.AlternateView = Net.Mail.AlternateView。创建AlternateViewFromString(msgBody,Nothing,text / html)
对于每个rc作为Net.Mail.LinkedResource在lnkRcs
altHtml.LinkedResources.Add(rc)
接下来
msg。 AlternateViews.Add(altHtml)
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 MailMessage("noreply@mycompany.com", "you@outlook.com", "test", body);
msg.IsBodyHtml = true;
var client = new SmtpClient(SmtpHost);
client.Send(msg);
emailtemplate.html
<div style="border:solid 1px #000;padding:20px;">
<img src="logo.png" style="width:250px">
<p style="color:red;font-weight:bold">Thanks for signing up!</p>
</div>
logo.png is in same location as emailtemplate.html
Any idea how I can include the image and apply the styles to my email message?
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 As New 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 As Integer = 0
While match.Success
Dim stm = Convert.FromBase64String(match.Groups("data").Value)
Dim rc As New 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()
End While
Dim altHtml As Net.Mail.AlternateView = Net.Mail.AlternateView.CreateAlternateViewFromString(msgBody, Nothing, "text/html")
For Each rc As Net.Mail.LinkedResource In lnkRcs
altHtml.LinkedResources.Add(rc)
Next
msg.AlternateViews.Add(altHtml)
这篇关于图片和样式不会出现在HTML电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!