我的图片和样式未显示在电子邮件中(在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.png" style="width:250px">
<p style="color:red;font-weight:bold">Thanks for signing up!</p>
</div>
logo.png与emailtemplate.html
位于同一位置知道如何包含图片并将样式应用到我的电子邮件中吗?
答案 0 :(得分:0)
使用图片的完整网址,而不是其相对网址。
类似于:this
答案 1 :(得分:0)
你的img src应该至少绝对有机会可见
为了保证img可见,将其转换为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">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)